hdu 3336 Count the string
来源:互联网 发布:手机电影特效软件 编辑:程序博客网 时间:2024/06/01 17:27
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7435 Accepted Submission(s): 3439
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.
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.
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
14abab
Sample Output
6
Author
foreverlin@HNU
Source
HDOJ Monthly Contest – 2010.03.06
Recommend
lcy | We have carefully selected several similar problems for you: 1711 1686 3746 1358 3341
#include<iostream>#include<cstring>#include<cstdio>#define N 200003using namespace std;int T,n;char s[N];int t[N],c[N];int len;void calc(){ t[0]=-1; int j; for (int i=0;i<n;i++) { j=t[i]; while (j!=-1&&s[i]!=s[j]) j=t[j]; t[i+1]=++j; }}int main(){ scanf("%d",&T); for (int i=1;i<=T;i++) { scanf("%d\n",&n); scanf("%s",s); memset(t,0,sizeof(t)); calc(); int num=0; for (int j=1;j<=n;j++) { int k=t[j]; while (k!=-1) k=t[k],num=(num+1)%10007; } printf("%d\n",num%10007); }}
0 0
- HDU 3336 Count the string
- hdu 3336 Count the string
- hdu 3336 Count the string
- hdu 3336 Count the string
- hdu 3336 Count the string
- hdu 3336 Count the string
- hdu 3336 Count the string
- HDU-Count the string -3336
- hdu 3336 Count the string
- HDU 3336 Count the string
- HDU 3336Count the string
- HDU 3336Count the string
- hdu 3336 Count the string
- hdu 3336 count the string
- HDU 3336 Count the string
- HDU - 3336 Count the string
- hdu 3336 Count the string
- hdu 3336 Count the string
- Express.js路由详解
- Android Studio配置三 gradle项目构建
- Java的异常类
- Android之TextView
- 程序员的十个经典算法
- hdu 3336 Count the string
- linux 修改启动脚本导致的错误:not a valid identifier
- 四月计划
- android sdk 文件目录含义介绍
- 记录EditText获取焦点后无法编辑 的解决方案
- extjs使用OpenLayer3
- springmvc @ResponseBody 返回json数据错误
- Android4.4的init进程详解
- 反射---基础方法概要笔记