http://acm.hdu.edu.cn/showproblem.php?pid=3336
/*
KMP中next数组的使用,可以加深对next数组的理解。
学习KMP时看到两篇不错的博客,推荐给大家
http://www.cnblogs.com/wuyiqi/archive/2012/01/05/2313746.html
http://blog.csdn.net/niushuai666/article/details/6960985
*/
题目:求给定字符串中包含的前缀总数。
#include"iostream"
#include"string"
using namespace std;
#define Max 200005
char s[Max];
int next[Max],ans;
void GetNext(int n)
{
int i,j;
for(i=0,j=-1,next[0]=-1;i<n;){
if(j==-1||s[i]==s[j]){
i++;
j++;
next[i]=j;
}
else{
j=next[j];
}
}
}
int main()
{
int t,n,i,j;
cin>>t;
while(t--){
cin>>n>>s;
GetNext(n);
for(i=1,ans=n;i<=n;i++){//next数组的值填充到n而不是n-1;
j=i;
while(next[j]!=0){
ans++;
j=next[j];
ans%=10007;
}
}
cout<<ans<<endl;
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
HDU-1711 Number Sequence(KMP算法)For each test case, you should output one line wh
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
HDU最全ac代码
hdu 1166线段树代码
hdu动态规划算法集锦
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu题目分类
自己做的HDU ACM已经AC的题目
HDU图论题目分类