http://acm.hdu.edu.cn/showproblem.php?pid=3336

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

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:  1686 3746 1358 3341 2222 
 
题意:求出每个前缀在串中出现的次数
每次统计答案时 增加 此串next[i]位置的次数+1即  f[i]=f[next[i]]+1,ans+=f[i]
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int mod();
int l,ans,p[N],f[N];
char s[N]; inline void Get_next()
{
for(int i=,j=;i<=l;i++)
{
for(;s[j+]!=s[i]&&j>;) j=p[j];
if(s[i]==s[j+]) j++;
p[i]=j;
}
} inline void init()
{
ans=;
memset(p,,sizeof(p));
memset(f,,sizeof(f));
} int main()
{
int t; scanf("%d",&t);
for(;t--;init())
{
scanf("%d%s",&l,s+);
Get_next();
for(int i=;i<=l;i++)
{
f[i]=(f[p[i]]+)%mod;
ans=(ans+f[i])%mod;
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. PHP 正则表达式 修饰符
  2. opencv 基础语法
  3. 我和Markdown故事
  4. javascript自定义滚动条插件,几行代码的事儿
  5. x-code快捷键
  6. Arduino 学习
  7. (C/C++) Interview in English. - Memory Allocation/Deallocation.
  8. SAP MRP的计算步骤
  9. Codeforces Round #181 (Div. 2) B. Coach 带权并查集
  10. 关于Linux的总结(三)
  11. jQuery 定时局部刷新(setInterval)方法总结
  12. iOS textfield实现一行的数字限制,超出进行弹框
  13. 转:推荐!国外程序员整理的 C++ 资源大全
  14. Redshift扩容及踩到的坑
  15. 制作jar包
  16. JavaScript Math(数学对象)
  17. [TFRecord文件格式]基本介绍
  18. linux-shell系列8 netstat用法
  19. Nginx+Flume+Hadoop日志分析,Ngram+AutoComplete
  20. skimage 安装和子模块

热门文章

  1. updatedb---创建或更新slocate命令所必需的数据库文件
  2. Java基础学习总结(19)——Java环境变量配置
  3. 关于android studio几种常见的错误解决
  4. Firefox访问https的网站,一直提示不安全
  5. ssh 免密及加密远程脚本实现
  6. 冒泡排序算法 C#版
  7. .net数字转换成汉字大写
  8. oracle 高水位线问题
  9. Ionic2集成ArcGIS JavaScript API.md
  10. 兼容IE浏览器的canvas画线和圆圈