题目链接:http://codeforces.com/contest/432/problem/D

题意:对一个长度不超过10^5的字符串。按长度输出和后缀全然匹配的的前缀的长度,及该前缀在整个串中出现的次数。(可重叠)

#include <cstdio>
#include <cstring> const int N=100005; char str[N];
int next[N],cnt[N],ansp[N],ansn[N],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 (全部前缀的出现次数)的更多相关文章

  1. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes

                                                        D. Prefixes and Suffixes You have a string s = s ...

  2. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)

    D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  4. Codeforces 432D Prefixes and Suffixes(KMP+dp)

    题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...

  5. Codeforces 432 D. Prefixes and Suffixes

    用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...

  6. Codeforces 1092C Prefixes and Suffixes(思维)

    题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...

  7. CodeForces Round #527 (Div3) C. Prefixes and Suffixes

    http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...

  8. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  9. Codeforces 432D Prefixes and Suffixes kmp

    手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...

随机推荐

  1. 131.typename在嵌套类中的作用

    #include <iostream> using namespace std; class myit { public: static int num; class itit { }; ...

  2. java9新特性-11-String存储结构变更

    1. 官方Feature JEP254: Compact Strings 2. 产生背景 Motivation The current implementation of the String cla ...

  3. The Future Of ReactiveCocoa by Justin Spahr-Summers

    https://www.bilibili.com/video/av9783052?from=search&seid=14165903430339282774

  4. 将毫秒时间转换为yyyy-MM-dd HH:mm:ss格式

    /** * 将毫秒时间转换为yyyy-MM-dd HH:mm:ss格式 */ public static String getDateFromTimeMillis(Long timeMillis) { ...

  5. BZOJ 3637: Query on a tree VI LCT_维护子树信息_点权转边权_好题

    非常喜欢这道题. 点权转边权,这样每次在切断一个点的所有儿子的时候只断掉一条边即可. Code: #include <cstring> #include <cstdio> #i ...

  6. PHP7内核剖析之执行流程

    以fpm为例: 1.fpm启动时,会先执行 module_startup, 并随着fpm进程常驻 2.当一个请求到达之后,会执行 request_startup, 进行一些请求初始化工作,然后执行代码 ...

  7. 会变得ActionBar,让你的ActionBar与众不同

    话不多说先看两张图: github地址:https://github.com/Smalinuxer/android-SlideActionBar 原理什么的有时间再讲,或者自行看代码; 兴许还会补充新 ...

  8. Codeforces Round #313 C. Gerald&#39;s Hexagon(放三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. hdu 1695 GCD (欧拉函数、容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  10. 鸟哥的Linux私房菜-----15、例行性命令at与crontab