hdu 3336 Count the string(kmp应用)
来源:互联网 发布:软件测试工程师工资 编辑:程序博客网 时间:2024/04/30 09:21
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 <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int Next[200010],dp[200010]; //dp[i]=以第i个字符结尾的串中所有前缀的计数和
char str1[200010];
void GetNext(int len,char *s)
{
Next[1]=0;
int i=1;
int j=0;
dp[0]=0;
while (i<=len)
{
if(j==0 || s[i]==s[j])
{
dp[i]=(dp[j]+1)%10007;
i++;
j++;
if(s[i]==s[j])
Next[i]=Next[j];
else
Next[i]=j;
}
else
j=Next[j];
}
}
int main()
{
int t,n,ans;
scanf("%d",&t);
while (t--)
{
ans=0;
scanf("%d",&n);
getchar();
scanf("%s",str1+1);
GetNext(n,str1);
for (int i=1; i<=n; i++)
{
ans=(ans+dp[i])%10007;
}
printf("%d\n",ans);
}
return 0;
}
- hdu 3336 Count the string(kmp应用)
- 【HDU 3336】Count the string 【KMP next数组巧妙应用】
- hdu 3336 Count the string KMP
- KMP Hdu-3336 Count the string
- HDU 3336 Count the string(KMP 理解)
- HDU 3336 Count the string KMP
- HDU 3336 Count the string(KMP+dp)
- [HDU 3336]Count the String[kmp]
- [HDU 3336]Count the String[kmp][DP]
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string(KMP)
- hdu 3336 Count the string KMP+DP
- HDU 3336 Count the string(KMP)
- hdu 3336 Count the string(KMP)
- hdu 3336 Count the string(KMP)
- HDU 3336 Count the string / KMP
- HDU 3336 Count the string kmp+dp
- 【HDU】3336 Count the string KMP
- Install Theano in Linux without root authority (sodu)
- Gradle构建语言手册
- iOS开发系列--无限循环的图片浏览器
- UITextFild功能大全
- 空手道14个基本站姿
- hdu 3336 Count the string(kmp应用)
- Linux时间子系统之二:表示时间的单位和结构
- 有序链表的建立
- ACM1064:
- mysql 创建分区表注意事项,每一个唯一约束必须包含在Mysql分区表的分区键(也包括主键约束)。
- error: Attribute "buttonBarStyle" has already been defined
- 负载测试工具
- POJ1004
- 枚举生成排列的方法总结