Count the string
来源:互联网 发布:一对一辅导软件免费 编辑:程序博客网 时间:2024/06/05 19:09
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.
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.
14abab
6
#include<cstdio>#include<cstring>#include<cstdlib>char a[200005];int b[200005];int next[200005];void makenext(char *b,int m){next[0]=0;int k=0,i=1;while(i<m){while(k>0&&b[k]!=b[i])k=next[k-1];if(b[i]==b[k])k++;next[i++]=k;}}int main(){int T,i,j,l1,l2,t,sum,k;scanf("%d",&T);while(T--){sum=0;memset(b,0,sizeof(b));scanf("%d",&k);scanf("%s",a);makenext(a,k);for(i=1;i<=k;i++){b[i]=b[next[i-1]]+1;}for(i=1;i<=k;i++)sum=(sum+b[i])%10007;printf("%d\n",sum);} return 0;}
阅读全文
0 0
- hdu3336 Count the string
- hdu3336 Count the string
- HDU3336:Count the string
- hdu3336-Count the string
- Count the string + KMP
- hdu3336 Count the string
- Count the string (KMP)
- Count the string
- hdu3336 Count the string
- hdu3336----Count the string
- hdu3336 Count the string
- Count the string
- 【HDU3336】【Count the string】
- Count the string
- Count the string
- HDU3336 Count the string
- hdu3666 Count the string
- 3336 Count the string
- Linux下网卡eth1如何修改为eth0
- iOS UITextField禁止键盘弹出,同时又实现代理方法的实现方式!
- Java学习笔记(九)
- socket编程(一),实现服务器与客户端简单通信
- 算法题目---不能被继承的类
- Count the string
- ZJU夏令营面试记录
- Java面试题全集(上)
- 决策树
- ajax实现的传统方式以及jquery方式,jquery方式的五种方法,json对象的转换方法, JSON类型的Result
- android 如何在线程缓存实体类
- 文件输入/输出流
- Scrapy实例1-爬取天气预报存储到Json
- SPRING 事务