首页 > 其他分享 >D. Non-Palindromic Substring

D. Non-Palindromic Substring

时间:2024-04-04 19:55:06浏览次数:16  
标签:Non string Palindromic int text len Substring test mathtt

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

标签:Non,string,Palindromic,int,text,len,Substring,test,mathtt
From: https://www.cnblogs.com/onlyblues/p/18114535

相关文章

  • 从 String.prototype.substring 的区间开始
    因为使用String.prototype.substring(start,end)或者Array.prototype.slice(start,end)的时候偶尔会想不起来这些函数的区间代表的是什么。在这里记录一下。不同函数的差异这些区间都是[start,end),即是包括start,但是不包括end(当没有传入end时,end视为数组或者字符串......
  • 多目标应用:基于非支配排序的蜣螂优化算法(Non-Dominated Sorting Dung beetle optimize
    一、柔性作业车间调度问题柔性作业车间调度问题(FlexibleJobSchedulingProblem,FJSP)的描述如下:n个工件{J,J......
  • @NotNull和@NonNull的区别和使用
    区别@NotNull在类字段中使用,表示该字段不能为空。它是JSR303(Bean的校验框架)的注解。在调用controller的方法中加入@Valid就可以验证该方法参数中该类的对应属性是否为空,如果为空,注解中的提示信息会保存在result中。@NonNull在方法或构造函数的参数上使用,表示该参数不能为空。@N......
  • E. Nearly Shortest Repeating Substring
    #include<iostream>#include<algorithm>#include<cstring>#include<cmath>usingnamespacestd;intn,m;intmain(){ cin>>n; while(n--) { //strings; cin>>m; strings; cin>>s; intres=m; f......
  • E. Nearly Shortest Repeating Substring
    原题链接题解1.模拟题,注意细节2.时间复杂度\(O(n·sqrt(n))\)code#include<bits/stdc++.h>usingnamespacestd;intn;strings;intcheck(intlen){intflag=0;for(intk=0;k<len;k++){inta[26]={0};for(inti=k;i<n;i+=len)......
  • Codeforces Round 937 (Div. 4)----->E. Nearly Shortest Repeating Substring
    一,思路:1.这题很容易想到枚举n的因数(时间复杂度n^(1/2)),然后根据这个长度枚举字符串,看是否满足最多只有一个不相同(时间复杂度n)。总的时间复杂度是(n根号n)的级别。又n是1e5级别所以可以过。但是当n是1e6就不行了。2.难点在于如何判断,一个字符串的不同字符数量,主要是hshah......
  • Extraneous non-props attributes (title) were passed to component but could not b
    大概意思就是给子组件传递的属性,由于子组件呈现片段或文本根节点,无法自动继承;就是"透传Attributes"。对于多根节点的组件没有自动attribute透传行为;如果$attrs没有被显式绑定,将会抛出一个运行时警告。解决方式:手动显示绑定$attrs(1)模板 <template> <h1>多根节点的At......
  • 关于使用IconData时flutter build apk 打包报错Target aot_android_asset_bundle fail
    flutter项目中引入了iconfont.ttf之后,调试时正常,打包就报错。 网上有的说法是:使用了iconfont.ttf里面不存在的icon,但是我使用的都是在iconfont.tt文件中的icon。 我的情况是使用了switch  case给IconData的codePoint动态赋值,下面这种情况就是打包报错的 解决办法是......
  • 【数据库】PostgreSQL中使用`SELECT DISTINCT`和`SUBSTRING`函数实现去重查询
    在PostgreSQL中,我们可以使用SELECTDISTINCT和SUBSTRING函数来实现对某个字段进行去重查询。本文将介绍如何使用这两个函数来实现对resource_version字段的去重查询。1.SELECTDISTINCT语句SELECTDISTINCT语句用于从表中选择不重复的记录。如果没有指定列名,则会选择所有列。在......
  • non constant or forward reference address expression for section .ARM.extab 错误
    编译时报错:FAILED:STM32F103RET6_Test001.elfcmd.exe/C"cd.&&D:\ProgramFiles\gcc-arm-none-eabi\bin\arm-none-eabi-gcc.exe-g-Wl,-gc-sections,--print-memory-usage,-Map=D:/ProjectCode/CLion/test/STM32F103RET6_Test001/cmake-build-debug-arm-......