您的位置:首页
CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)
2017-07-02 16:43
447 查看
题目链接: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 (所有前缀的出现次数)
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
- Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )
- HDOJ-1251 统计难题 字典树的应用-----统计一组字符串中某前缀出现次数(字典树第一类应用),
- codeforces Round 246 D. Prefixes and Suffixes (后缀数组 || KMP)
- HDU 3336 Count the string (求前缀在字符串中出现的次数和)
- hdoj 4552 怪盗基德的挑战书 【KMP 求所有前缀在原串种出现的次数之和】
- 在js里通过id控制表或div的隐藏和显示,以及把id前缀相同的div全部隐藏起来
- hdoj 3336 Count the string 【kmp求 所有前缀 在原串中出现的次数 总和】
- HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)
- Hdu 4552 怪盗基德的挑战书 (所有前缀出现次数和 后缀数组或KMP)
- Trie树的编程实现,用于计算字符串出现次数,求公共前缀字符串等问题
- hdu1671 字典树记录前缀出现次数
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
- hdu 1251 Trie入门(统计前缀出现次数)
- Codeforces Round #246 (Div. 2)-D. Prefixes and Suffixes(KMP+DP)
- Hdu 3336 Count the string (KMP+DP 前缀出现次数和)
- HDU 3336 Count the string 所有前缀在串中的出现总次数
- hdu 4552 求所有字符串前缀在字符串中出现的总次数