隐藏页面特效

hdu3336

1|0Count the string


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8594    Accepted Submission(s): 3969


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:
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.
 

 

Input
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.
 

 

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
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1711 1686 3746 1358 3341 
TLE代码:
#include<cstdio> #include<iostream> using namespace std; #define N 200010 int T,lens,lenp,nextt[N]; char s[N],p[N]; inline void get_next(){ int i=0,j=-1; nextt[i]=j; while(i<lenp){ if(j==-1||p[i]==p[j]){ i++;j++; nextt[i]=j; } else j=nextt[j]; } } inline int kmp(){ get_next(); int i(0),j(0),ans(0); while(i<lens&&j<lenp){ if(j==-1||s[i]==p[j]){ i++;j++; } else j=nextt[j]; if(j==lenp) ans++,j=nextt[j]; } return ans%10007; } int main(){ scanf("%d",&T); while(T--){ scanf("%d",&lens); scanf("%s",s); int sum=0; for(int i=0;i<lens;i++){ fill(p,p+i+1,0); for(int j=0;j<=i;j++) p[j]=s[j]; lenp=i+1; sum+=kmp(); sum%=10007; } printf("%d\n",sum%10007); } return 0; }

 

题解:
依次求字符串匹配数,求和,取模
水水的KMP
 
AC代码:
#include<cstdio> #include<iostream> using namespace std; #define N 200010//数组开的大大的 #define mod 10007//注意取模 int T,len,Next[N]; char s[N],p[N]; inline void get_next(){//next是关键字,所以用Next[] int i=0,j=-1; Next[i]=j; while(i<len){ if(j==-1||s[i]==s[j]){ i++;j++; Next[i]=j; } else j=Next[j]; } } int main(){ scanf("%d",&T); while(T--){ scanf("%d",&len); scanf("%s",s); get_next();//每组数据做一次求好了,否则TLE int sum=0; for(int i=1,j;i<=len;i++){ j=i; while(j) sum=(sum+1)%mod,j=Next[j]; } printf("%d\n",sum); } return 0; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/5748208.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(183)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示