题目链接:hdu 3336 Count the string
题目大意:给出一个字符串,要求计算所有前缀串在该字符串中出现次数的总和。
解题思路:首先求出jump数组,然后对于每个位置来说,如果jump[p]!=0的话,即为存在一个前缀串,注意这里p = jump[p], 因为前缀串中含可能包含前缀串。最后要+n。
#include <stdio.h>
#include <string.h>
const int N = 1e5+5;
const int MOD = 10007;
int n, jump[N*2];
char s[N*2];
int getCnt (int p) {
int ans = 0;
while (p) {
ans++;
p = jump[p];
}
return ans;
}
int getJump () {
int p = 0, ans = n % MOD;
for (int i = 2; i <= n; i++) {
while (p > 0 && s[p+1] != s[i])
p = jump[p];
if (s[p+1] == s[i])
p++;
jump[i] = p;
ans = (ans + getCnt(p)) % MOD;
}
return ans;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%s", &n, s+1);
printf("%d\n", getJump());
}
return 0;
}
分享到:
相关推荐
HDU-1711 Number Sequence(KMP算法)For each test case, you should output one line wh
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
hdu题目分类
HDU图论题目分类