Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15437 Accepted Submission(s): 7046 Problem Description 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:
Input The first line is a single integer T, indicating the number of test cases.
Output For 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
Author foreverlin@HNU
Source HDOJ Monthly Contest – 2010.03.06
Recommend lcy |
题目大意:现在给你一个长度为N的字符串,现在让你求出每一个前缀在这个字符串中出现的次数
思路:这个题目的话,就是把next数组的求出来,然后按照a[i]=(a[nxt[i]]+1)求出每一位上的加和,
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
const int maxn=200000+5;
const int mod=10007;
int nxt[maxn],a[maxn];
void getnext(char p[])
{
nxt[0]=-1;
int j=0,k=-1;
int len=strlen(p);
while(j<len)
{
if(k==-1||p[k]==p[j])
nxt[++j]=++k;
else
k=nxt[k];
}
}
char str[maxn];
int main()
{
int t,len;scanf("%d",&t);
while(t--)
{
scanf("%d",&len);
scanf("%s",&str);
getnext(str);
int ans=0;
for(int i=1;i<=len;i++)
{
a[i]=(a[nxt[i]]+1)%mod;
ans+=a[i];
ans%=mod;
//cout<<a[i]<<" ";
}
//cout<<endl;
printf("%d\n",ans);
}
}