D. Non-Palindromic Substring

D. Non-Palindromic Substring

A string $t$ is said to be $k$-good if there exists at least one substring$^\dagger$ of length $k$ which is not a palindrome$^\ddagger$. Let $f(t)$ denote the sum of all values of $k$ such that the string $t$ is $k$-good.

You are given a string $s$ of length $n$. You will have to answer $q$ of the following queries:

  • Given $l$ and $r$ ($l < r$), find the value of $f(s_ls_{l + 1}\ldots s_r)$.

$^\dagger$ A substring of a string $z$ is a contiguous segment of characters from $z$. For example, "$\mathtt{defor}$", "$\mathtt{code}$" and "$\mathtt{o}$" are all substrings of "$\mathtt{codeforces}$" while "$\mathtt{codes}$" and "$\mathtt{aaa}$" are not.

$^\ddagger$ A palindrome is a string that reads the same backwards as forwards. For example, the strings "$\texttt{z}$", "$\texttt{aa}$" and "$\texttt{tacocat}$" are palindromes while "$\texttt{codeforces}$" and "$\texttt{ab}$" are not.

Input

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $q$ ($2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5$), the size of the string and the number of queries respectively.

The second line of each test case contains the string $s$. It is guaranteed the string $s$ only contains lowercase English characters.

The next $q$ lines each contain two integers, $l$ and $r$ ($1 \le l < r \le n$).

It is guaranteed the sum of $n$ and the sum of $q$ both do not exceed $2 \cdot 10^5$.

Output

For each query, output $f(s_ls_{l + 1}\ldots s_r)$.

Example

input

5
4 4
aaab
1 4
1 3
3 4
2 4
3 2
abc
1 3
1 2
5 4
pqpcc
1 5
4 5
1 3
2 4
2 1
aa
1 2
12 1
steponnopets
1 12

output

9
0
2
5
5
2
14
0
2
5
0
65

Note

In the first query of the first test case, the string is $\mathtt{aaab}$. $\mathtt{aaab}$, $\mathtt{aab}$ and $\mathtt{ab}$ are all substrings that are not palindromes, and they have lengths $4$, $3$ and $2$ respectively. Thus, the string is $2$-good, $3$-good and $4$-good. Hence, $f(\mathtt{aaab}) = 2 + 3 + 4 = 9$.

In the second query of the first test case, the string is $\mathtt{aaa}$. There are no non-palindromic substrings. Hence, $f(\mathtt{aaa}) = 0$.

In the first query of the second test case, the string is $\mathtt{abc}$. $\mathtt{ab}$, $\mathtt{bc}$ and $\mathtt{abc}$ are all substrings that are not palindromes, and they have lengths $2$, $2$ and $3$ respectively. Thus, the string is $2$-good and $3$-good. Hence, $f(\mathtt{abc}) = 2 + 3 = 5$. Note that even though there are $2$ non-palindromic substrings of length $2$, we count it only once.

 

解题思路

  为了方便规定字符串的下标从 $0$ 开始,询问的 $l$ 和 $r$ 也相应的映射到 $0 \sim n-1$ 范围内。

  对于询问 $l$ 和 $r$,子串的长度为 $\text{len} = r-l+1$。先考虑 $k \in [2, len-1]$ 的情况。命题存在长度为 $k$ 的非回文串,对应的反命题就是不存在长度为 $k$ 的非回文串,等价于所有长度为 $k$ 的子串都是回文串。如果 $k$ 能作为答案,意味着原命题要成立,反命题不成立;否则反命题成立。所以我们现在要验证反命题是否成立(反命题会比原命题好验证)。

  如果所有长度为 $k$ 的子串都是回文串,如果 $k$ 是奇数,可以推出 $s_{l} = s_{l+2} = s_{l+4} = \cdots$,$s_{l+1} = s_{l+3} = s_{l+5} = \cdots$,即下标奇偶性相同的字符都相同。此时反命题成立,所有奇数的 $k$ 都无法作为答案。如果 $k$ 是偶数,可以推出 $s_{l} = s_{l+1} = \cdots = s_{r}$,即所有字符都相同,此时反命题成立,所有偶数 $k$ 都无法作为答案。

  要判断上述两种情况是否成立,只需预处理出 $f(i)$ 表示左边所有下标中第一个与 $s_i$ 不同的下标,$g(i)$ 表示左边所有与 $i$ 奇偶性相同的下标中第一个与 $s_i$ 不同的下标。如果 $f(r) < l$ 说明第二种情况成立,否则假设不超过 $\text{len}-1$ 的最大偶数为 $t = \text{len} - 1 - (\text{len} - 1 \bmod 2)$,答案加上 $2 + 4 + \cdots + t = \frac{t}{2} \left( \frac{t}{2}+1 \right)$。如果 $g(r) < l$ 且 $g(r-1) < l$ 说明第一种情况成立,否则假设不超过 $\text{len}-1$ 的最大奇数为 $t = \text{len} - 1 - (\text{len} \bmod 2)$,答案加上 $3 + 5 + \cdots + t = \left( \frac{t+1}{2} \right)^2-1$。

  剩下就是 $k=1$ 和 $k=\text{len}$ 的情况。显然 $k=1$ 不可能作为答案,因为长度为 $1$ 的串必定是回文串。而长度为 $\text{len}$ 的整个串则需要单独判断是否为回文串,可以用 Manacher 算法(字符串哈希貌似会被卡自然溢出,或者用双哈希也可以)。

  AC 代码如下,时间复杂度为 $O(n+m)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5, M = N * 2;

int n, m;
char s[N], t[M];
int d[M];
int f[N], g[N];

void manacher() {
    int m = 0;
    t[m++] = '#';
    for (int i = 0; i < n; i++) {
        t[m++] = s[i];
        t[m++] = '#';
    }
    for (int i = 0, j = 0; i < m; i++) {
        if (i < j + d[j]) d[i] = min(d[2 * j - i], j + d[j] - i);
        else d[i] = 1;
        while (i - d[i] >= 0 && i + d[i] < m && t[i - d[i]] == t[i + d[i]]) {
            d[i]++;
        }
        if (i + d[i] > j + d[j]) j = i;
    }
}

void solve() {
    scanf("%d %d %s", &n, &m, s);
    for (int i = 0; i < n; i++) {
        if (i - 1 >= 0 && s[i] == s[i - 1]) f[i] = f[i - 1];
        else f[i] = i - 1;
        if (i - 2 >= 0 && s[i] == s[i - 2]) g[i] = g[i - 2];
        else g[i] = i - 2;
    }
    manacher();
    while (m--) {
        int l, r;
        scanf("%d %d", &l, &r);
        l--, r--;
        LL ret = 0;
        if (f[r] >= l) {
            LL t = r - l - (r - l) % 2;
            ret += t * (t + 2) / 4;
        }
        if (g[r] >= l || g[r - 1] >= l) {
            LL t = r - l - (r - l + 1) % 2;
            ret += (t + 1 >> 1) * (t + 1 >> 1) - 1;
        }
        if (d[l + r + 1] <= r - l + 1) ret += r - l + 1;
        printf("%lld\n", ret);
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round #934 (Div1, Div2) Editorial:https://codeforces.com/blog/entry/127195

posted @ 2024-04-04 19:50  onlyblues  阅读(11)  评论(0编辑  收藏  举报
Web Analytics