CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)
题目链接: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 (全部前缀的出现次数)的更多相关文章
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
- 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 ...
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Codeforces 432 D. Prefixes and Suffixes
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- 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 ...
- 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, ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
随机推荐
- 16.允许重复的multimap
#include <iostream> #include <map> #include <cstring> using namespace std; void ma ...
- android页面布局(listview填充中间)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- 使用Chrome浏览器,让我们远离(所有)广告
你是否还在为浏览网页时各种广告霸屏而急躁不安?这里分享一个小技巧,如何自动屏蔽各大广告. 这里使用的浏览器是Chrome,直接在Chrome网上应用商店搜索下载安装AdBlock插件(不知道其它浏览器 ...
- 日志文件支持unicode字符的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 开发的程序兼容多字节字符集和unicode字符集,最近发现一个问题,在unicode字符集下输出的日志文件是乱码的.显 ...
- SQL Server在用户自定义函数(UDF)中使用临时表
SQL Server在用户自定义函数中UDF使用临时表,这是不允许的. 有时是为了某些特殊的场景, 我们可以这样的实现: CREATE TABLE #temp (id INT) GO INSERT I ...
- Slimming Paint (a.k.a. Redesigning Painting and Compositing)
Slimming Paint is a Paint team project to re-implement the Blink<->cc picture recording API to ...
- Vue总结(二)
原始引用:开发时使用开发版本,线上使用生产版本. 原始引用到html中,在浏览器中控制台输入Vue,输出一个函数就可以. defineProperties实现的数据绑定. //defineProper ...
- vue中使用console.log无效
webpack开发环境下,在vue中使用console.log无效,一直以为webpack出了问题. 使用window.console.log()就能够顺利在浏览器控制台输出了. 以及 在axios请 ...
- numpy基础篇-简单入门教程2
import numpy as np Array 数组 print(np.zeros((2, 2))) # [[0. 0.] [0. 0.]] print(np.ones((2, 2))) # [[1 ...
- *Mapper.xml文件头
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-// ...