标签:integer deb output include div 就是 let += queue
InputThe first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
OutputFor each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.Sample Input
1 4 abab
Sample Output
6
题意:
问所有前缀出现的次数.
思路:
求出扩展kmp的Next数组,然后把Next的值全部加起来就是答案.
扩展kmp的Next[i]表示的是,以i为开头的后缀,与整个字符串的lcp的长度,那么显而易见,整个字符串长度为1,2,...,Next[i]的前缀在这里都出现了一次,所以此时答案加上Next[i];
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl; #define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl; #define ls (t<<1) #define rs ((t<<1)|1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 200086; const int maxm = 100086; const int inf = 0x3f3f3f3f; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int Next[maxn],extend[maxn]; int lens ,lent; void GetNext(char *t){ lent = strlen(t); int a = 0, p = 0; Next[0] = lent; for (int i = 1; i < lent; i++){ if (i >= p || i + Next[i - a] >= p){ if (i >= p){ p = i; } while (p < lent && t[p] == t[p - i]) { p++; } Next[i] = p - i; a = i; } else { Next[i] = Next[i - a]; } } } void GetExtend(char *t,char *s){ int a = 0, p = 0; lens=strlen(s); GetNext(t); for (int i = 0; i < lens; i++){ if (i >= p || i + Next[i - a] >= p) {// i >= p 的作用:举个典型例子,S 和 T 无一字符相同 if (i >= p) { p = i; } while (p < lens && p - i < lent && s[p] == t[p - i]) { p++; } extend[i] = p - i; a = i; } else { extend[i] = Next[i - a]; } } } char s[maxn],t[maxn]; int main() { // ios::sync_with_stdio(false); // freopen("in.txt", "r", stdin); int T; scanf("%d",&T); while (T--){ int n; scanf("%d",&n); scanf("%s",t); GetNext(t); ll sum = 0; for(int i=0;i<n;i++){ sum+=Next[i]; sum%=10007; }printf("%d\n",sum); } }
HDU - 3336 Count the string (扩展kmp)
标签:integer deb output include div 就是 let += queue
原文地址:https://www.cnblogs.com/ZGQblogs/p/11254254.html