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

题意:

求字符串的前缀在字符串里出现多少次。

思路:

主要就是要知道next数组的作用,就可以计算每个i结尾的满足题意的串个数,相加即可。

#include<cstdio>
#include<cstdlib>
#include<memory>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=;
const int Mod=;
int Next[maxn],ans,n;
char c[maxn];
void KMP()
{
ans=;Next[]=;
for(int i=,k=;i<=n;i++){
while(k&&c[i]!=c[k+]) k=Next[k];
if(c[i]==c[k+]) k++;
Next[i]=k;
int tmp=Next[i];
while(tmp){
ans++;
tmp=Next[tmp];
}
}
printf("%d\n",(ans+n)%Mod);
}
int main()
{
int T;scanf("%d",&T);
while(T--){
scanf("%d",&n);
scanf("%s",c+);
KMP();
}
return ;
}

HDU3336 Count the string的更多相关文章

  1. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  2. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  4. HDU3336 Count the string(kmp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  5. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  6. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  7. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  8. [KMP][HDU3336][Count the string]

    题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...

  9. Count the string[HDU3336]

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. hdu 3342 拓扑排序 水

    好久没切题  先上水题! 拓扑排序! 代码: #include<iostream> #include<cstdio> #include<cstring> using ...

  2. 字符编码笔记:ASCII,Unicode和UTF-8(转)

    字符编码笔记:ASCII,Unicode和UTF-8 作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个 ...

  3. 201521123081《Java程序设计》 第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 参考资料:XMIND 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集 集合 Q1. Li ...

  4. 201521123063 《Java程序设计》 第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boole ...

  5. 201521044091 《Java学习笔记》 第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结.注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖面 ...

  6. 201521123023《java程序设计》第三周学习总结

    1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...

  7. 201521123013 《Java程序设计》第13周学习总结

    1. 本周学习总结 2. 书面作业 Q1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn,分析返回结果有何不同?为什么会有这样的不同? ping值 ...

  8. 201521123019 《java程序设计》 第14周学习总结

    1. 本章学习总结 2. 书面作业 Q1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) ...

  9. 201521123121 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 进程:每个进程都有独立的代码和数据空间,进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程 ...

  10. 201521123012 《Java程序设计》第九周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 1.本次PTA作业题集异常 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...