D - Count the string

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
 
求解前缀数组的数目:  
代码:
 #include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn= ;
char st[maxn];
int next[maxn];
int dp[maxn];
int main()
{
int test,lenb;
scanf("%d",&test);
while(test--)
{
scanf("%d %s",&lenb,st);
int i=,j=-;
next[]=-;
memset(dp,,sizeof(int)*(lenb+));
while(i<lenb)
{
if(j==-||st[i]==st[j])
next[++i]=++j;
else j=next[j];
}
//得到了一个next数组...
for(i=;i<=lenb;i++)
dp[i]+=dp[next[i]]+; //求解所有前缀的种类 while(next[i])
int ans=;
for(i=;i<=lenb;i++)
{
ans+=dp[i];
ans%=;
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. Python学习--01入门
  2. Swift - 生成随机颜色(Extension UIColor)
  3. Linear Algebra Lecture5 note
  4. win8.1 64位安装DEV C++
  5. [AngularJS] AngularJS系列(1) 基础篇
  6. 阻抗计算(用SI9000如何计算微带线)
  7. 昂贵的聘礼---poj1062(最短路)
  8. cocos2d-x 2.2 开发手记2
  9. gcc将多个静态库链接成一个静态库
  10. 【淡墨Unity3D Shader计划】五 圣诞用品: Unity在Shader三种形式的控制&amp;amp;混合操作编译
  11. 在.Net中执行js
  12. 设计理念 : popup login 在前后台
  13. JS&amp;Jquery中的遍历
  14. python进阶(8):常用模块2+异常处理
  15. Vue源码终笔-VNode更新与diff算法初探
  16. C++之标准输入输出
  17. nsq源码阅读笔记之nsqd(三)——diskQueue
  18. Linux中jdk安装及配置
  19. java----回文序列判断java
  20. 【angularjs】使用angular搭建PC端项目,开关按钮

热门文章

  1. DataTable或者DataRow转换对象
  2. ubuntu下读取数据库中文乱码解决
  3. Spark学习(一)--RDD操作
  4. [HDOJ3714]Error Curves(三分)
  5. Mybatis+SpringMVC+Spring整合
  6. kvc kvo(摘录)
  7. MySQL子查询(SubQuery)
  8. h5移动开发css
  9. HDU1518 Square(DFS)
  10. MVC3远程验证