cf 432D Prefixes and Suffixes kmp
2014-05-16 19:46
330 查看
题目给出一个字符串,合法的子串是该字符串的前缀,且存在一个后缀与他匹配。求一共有几个合法的子串,并求出每个合法的子串在字符串中出现的次数。
第一问可以直接用kmp的p数组解决,除了原字符串本身外最长的合法子串是1----p[ len ],第二长的是1----p[
p[ len ] ],直到为0,累加就好。统计次数递推来做。
第一问可以直接用kmp的p数组解决,除了原字符串本身外最长的合法子串是1----p[ len ],第二长的是1----p[
p[ len ] ],直到为0,累加就好。统计次数递推来做。
#include <cstdio> #include <cstring> using namespace std; #define maxn 100010 int p[maxn]; char a[maxn]; int pos[maxn]; int cnt[maxn]; int main() { int i,j; int len; int tot=0; scanf("%s",a+1); len=strlen(a+1); j=0;p[1]=0; for(i=2;i<=len;i++) { while(j>0&&a[i]!=a[j+1]) j=p[j]; if(a[i]==a[j+1]) j++; p[i]=j; } i=len; while(p[i]!=0) { tot++; pos[tot]=p[i]; i=p[i]; } tot++;pos[tot]=len; j=len; for(i=1;i<=len;i++) cnt[i]=1; for(i=len;i>0;i--) cnt[p[i]]+=cnt[i]; printf("%d\n",tot); for(i=tot-1;i>0;i--) printf("%d %d\n",pos[i],cnt[pos[i]]); printf("%d 1\n",len); return 0; }
相关文章推荐
- cf 432D Prefixes and Suffixes kmp
- cf 432D Prefixes and Suffixes kmp
- CodeForces 432D|Prefixes and Suffixes|KMP|动态规划
- codeforces 432D Prefixes and Suffixes (kmp+dp)
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
- Codeforces 432D Prefixes and Suffixes
- codeforces Round 246 D. Prefixes and Suffixes (后缀数组 || KMP)
- (CodeForces 432D)Prefixes and Suffixes
- codeforces Round 246 D. Prefixes and Suffixes (后缀数组 || KMP)
- Codeforces Round #246 (Div. 2)-D. Prefixes and Suffixes(KMP+DP)
- CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)
- CF 246 div2 D Prefixes and Suffixes (所有前缀的出现次数)
- Codeforces 432D Prefixes and Suffixes (next数组的应用)
- CF 149E Martian Strings(KMP)
- 【转】cf 825F - 19 String Compression 【kmp+dp】
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
- cf 126B-Password(KMP)
- CF 182D Common Divisors(KMP最短循环节,循环周期)