Count the string HDU
来源:互联网 发布:mac pro13 retina壁纸 编辑:程序博客网 时间:2024/06/05 03:38
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<stdio.h>#include<string.h>using namespace std;const int M=2e5+10;char str[M];int dp[M],next[M];void getnext(int len){ next[0]=-1; for(int i=1; i<len; i++) { int j=next[i-1]; while(j>=0&&str[i]!=str[j+1]) j=next[j]; if(str[i]==str[j+1]) next[i]=j+1; else next[i]=-1; }}int main(){ int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); scanf("%s",str); getnext(n); dp[0]=1; int sum=0; for(int i=1; i<n; i++) { int j=next[i]; if(j==-1) dp[i]=1; else dp[i]=dp[j]+1; sum+=dp[i]; sum%=10007; } printf("%d\n",sum+1); }}
阅读全文
0 0
- Count the string HDU
- Count the string HDU
- 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
- 如何用消息系统避免分布式事务
- Dukemann International
- $.each(){} 和<c:forEach>的用法
- Java内部类的使用小结
- sparkR介绍及安装
- Count the string HDU
- PHP运行时强制显示出错信息的代码
- oracle rac 生产库添加udev注意事项和步骤
- 全局/局部变量
- Oracle中的SQL基础查询语句--上(值得一看,例题非常多,也很清晰)
- java笔记之线程间通信
- windows蓝屏故障解决
- Region/Hfile/Datanode的关系和区分
- HTTP请求与响应-get和post