HDU-3336 Count the string(KMP)
2018-02-05 21:18
435 查看
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12332 Accepted Submission(s): 5691
[align=left]Problem Description[/align]
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab",
it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
[align=left]Input[/align]
The 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.
[align=left]Output[/align]
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
[align=left]Sample Input[/align]
1
4
abab
[align=left]Sample Output[/align]
6
题意:求每一个前缀在字符串中出现的次数
思路:当然本身得出现一次,其次我们可以从每一个i位置开始不断取next,因为每取一次next,就代表有一个前缀出现一次,并且对于每一个i不断取next,产生的前缀串都是不会与之前重复的.还是需要深入理解next数组的含义.
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
int n,ne[maxn];
char s[maxn];
void get_next(char *s,int len)
{
ne[0] = -1;
int i = 0,j = -1;
while(i< len)
{
if(j == -1||s[i] == s[j])
{
i++;j++;
ne[i] = j;
}
else
j = ne[j];
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
mem(ne,0);
cin>>n;
scanf(" %s",s);
get_next(s,n);
int ans = n;
for(int i = 1;i<= n;i++)//遍历每一个i
{
int j = ne[i];//先取next,j非0说明有前缀出现
while(j)
{
ans++;
ans%= 10007;
j = ne[j];
}
}
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- 【HDU 3336】Count the string(KMP+DP)
- Count the string HDU - 3336 KMP+DP
- HDU 3336 Count the string(KMP+稍微DP+next数组的运用)
- HDU 3336 Count the string(KMP)||记忆化搜索
- HDU 3336 Count the string 【KMP】【dp】
- HDU - 3336 Count the string(KMP+DP)
- hdu 3336 Count the string(扩展KMP模板)
- HDU 3336 Count the string(KMP)
- hdu 3336 Count the string (kmp + dp)
- HDU 3336 Count the string(KMP:串前缀匹配自身+DP)
- hdu 3336 Count the string(KMP)
- |Hdu 3336|KMP|动态规划|Count the string
- HDU 3336 Count the string(KMP)
- HDU 3336 Count the string ( KMP next函数的应用 + DP )
- HDU 3336 Count the string / KMP
- HDU 3336 Count the string(KMP+dp)
- HDU 3336 Count the string(kmp+dp)
- hdu 3336 Count the string 【kmp + dp】
- hdu 3336 Count the string(KMP+dp)
- hdu 3336【Count the string】(KMP)