HDU - 3336 Count the string
https://vjudge.net/problem/HDU-3336
HDU - 3336 Count the string
#include<iostream>
#include<string>
using namespace std;
const int N=2e5+10;
int ne[N];
string s;
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n>>s;
int ans=0;
ne[0]=-1;
for(int i=0,j=-1;i<n;)
{
if(j==-1 || s[i]==s[j])
{
i++,j++;
ne[i]=j;
if(ne[i]>0)
ans++; //不为零则有前缀匹配
}
else
j=ne[j];
}
ans+=n; //0~n 每个前缀都出现一次
cout<<ans%10007<<endl; //别忘了取模
}
return 0;
}
本文来自博客园,作者:斯文~,转载请注明原文链接:https://www.cnblogs.com/zhiweb/p/15483314.html

浙公网安备 33010602011771号