CF 246 div2 D Prefixes and Suffixes (所有前缀的出现次数)
2014-06-13 09:36
399 查看
题目链接:http://codeforces.com/contest/432/problem/D
题意:对一个长度不超过10^5的字符串,按长度输出和后缀完全匹配的的前缀的长度,及该前缀在整个串中出现的次数。(可重叠)
题意:对一个长度不超过10^5的字符串,按长度输出和后缀完全匹配的的前缀的长度,及该前缀在整个串中出现的次数。(可重叠)
#include <cstdio> #include <cstring> const int N=100005; char str ; int next ,cnt ,ansp ,ansn ,n; void getNext (char s[],int len) { next[0]=-1; int i=0,j=-1; while (i<len) { if (j==-1 || s[i]==s[j]) next[++i]=++j; else j=next[j]; } } void KMP () { int i=0,j=0; while (i<n) if (j == -1 || str[i] == str[j]) i++,j++,cnt[j]++; else j=next[j]; for (i=n;i>0;i--) //统计每个前缀出现次数,cnt[i]表示长度为i的前缀出现了cnt[i]次 if (next[i] != -1) cnt[next[i]] += cnt[i]; } int main () { scanf("%s",str); n=strlen(str); memset(cnt,0,sizeof(cnt)); getNext(str,n); KMP(); int t=n,sum=0; while (t) //前缀匹配后缀 { ansp[sum]=t; ansn[sum++]=cnt[t]; // t=next[t]; } printf("%d\n",sum); for (int i=sum-1;i>=0;i--) printf("%d %d\n",ansp[i],ansn[i]); return 0; }
相关文章推荐
- CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)
- hdoj 3336 Count the string 【kmp求 所有前缀 在原串中出现的次数 总和】
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
- hdoj 4552 怪盗基德的挑战书 【KMP 求所有前缀在原串种出现的次数之和】
- hdu 4552 求所有字符串前缀在字符串中出现的总次数
- Hdu 4552 怪盗基德的挑战书 (所有前缀出现次数和 后缀数组或KMP)
- 所有前缀在串中出现的次数之和 kmp hdu 3336 count the string
- HDU 3336 Count the string 所有前缀在串中的出现总次数
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
- HDU 3336 KMP 求所有前缀在母串中出现的次数
- 统计字符串中:各个数字、空白字符、以及其他所有字符出现的次数
- PHP统计所有字符在字符串中出现的次数
- Trie树的编程实现,用于计算字符串出现次数,求公共前缀字符串等问题
- php统计所有字符在字符串中出现的次数
- B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)
- 统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- PHP实现统计所有字符在字符串中出现次数的方法
- HDU 3336 Count the string (求前缀在字符串中出现的次数和)
- hdu 1251 统计难题 (统计前缀出现次数)
- Zoj 3587 Marlon's String (KMP 字符串拼接 前缀出现次数)