Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character.
Let's introduce several definitions:
- A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1...sj.
- The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
- The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].
Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.
The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.
In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.
ABACABA
3
1 4
3 2
7 1
AAA
3
1 3
2 2
3 1
提意:求出满足前缀等于后缀的所有的子串的个数
sl:开始我是用的hash扫了下满足条件的前缀 然后 构造了一个ac自动机 超时 太大意了,搞得C题没敲完都。
原来此题 是KMP的应用,其实就是两道原题的组合,关键是如何分类求出所有前缀的个数,首先用一个vis标记满足
条件的前缀,然后用next数组扫一边所有前缀此次扫描都是扫的以当前字符为结尾的最大的前缀,所以最后还要扫描一次
又叫我长见识了。。。。。 >_<
1 //codeforces Codeforces Round #246 (Div. 2) D
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #include <cmath>
6 #include <vector>
7 #include <list>
8 #include <queue>
9 using namespace std;
const int MAX = 1e6+;
const int inf = 0x3f3f3f3f;
vector<pair<int,int> > ans;
int dp[MAX],next[MAX],vis[MAX],cnt[MAX];
char str[MAX];
int main()
{
//freopen("in","r",stdin);
//freopen("out","w",stdout);
while(scanf("%s",str+)==) {
int n=strlen(str+);
next[]=; int j=;
for(int i=;i<=n;i++) {
while(str[i]!=str[j+]&&j) j=next[j];
if(str[i]==str[j+]) j++;
next[i]=j;
}
int x=n;
while(x) {
vis[x]=; x=next[x];
}
for(int i=n;i>=;i--) cnt[next[i]]++;
// for(int i=1;i<=n;i++) printf("%d ",cnt[i]); printf("\n");
for(int i=n;i>=;i--) cnt[next[i]]+=cnt[i];
for(int i=;i<=n;i++) if(vis[i]) ans.push_back(make_pair(i,cnt[i]));
printf("%d\n",(int)ans.size());
for(int i=;i<ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second+);
}
return ;
}
Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes的更多相关文章
- 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 ...
- Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes
题目链接 题意:给你一个长度n,还有2*n-2个字符串,长度相同的字符串一个数前缀一个是后缀,让你把每个串标一下是前缀还是后缀,输出任意解即可. 思路;因为不知道前缀还是后缀所以只能搜,但可以肯定的是 ...
- Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (思维,字符串)
题意:给你某个字符串的\(n-1\)个前缀和\(n-1\)个后缀,保证每个所给的前缀后缀长度从\([1,n-1]\)都有,问你所给的子串是前缀还是后缀. 题解:这题最关键的是那两个长度为\(n-1\) ...
- Codeforces Round #246 (Div. 2)
题目链接:Codeforces Round #246 (Div. 2) A:直接找满足的人数,然后整除3就是答案 B:开一个vis数组记录每一个衣服的主场和客场出现次数.然后输出的时候主场数量加上反复 ...
- Codeforces Round #587 (Div. 3) A. Prefixes
链接: https://codeforces.com/contest/1216/problem/A 题意: Nikolay got a string s of even length n, which ...
- Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)
题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...
- Codeforces Round #246 (Div. 2) B. Football Kit
题目的意思是求出每个队穿主场衣服和客场衣服的次数 每个队作为主场的次数是n-1,作为客场的次数是n-1 当每个队打主场的时候肯定穿的主场衣服 当每个队打客场时,如果客场与主场的衣服不同,则穿客场衣服 ...
- Codeforces Round #246 (Div. 2) A. Choosing Teams
给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...
- Codeforces Round #246 (Div. 2)——D题
KMP算法,没写出来,完全不理解NEXT数组.现在理解了很多 答案都在程序中 ,不过这个思想真的很神奇, 还有毛语不好,一直没看懂题目,现在懂了, 大概是:S中前缀等于后缀,求其长度,和其在S中出现了 ...
随机推荐
- SP1557 GSS2 - Can you answer these queries II(线段树)
传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...
- 2017 Pycharm激活码
BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- 洛谷 P2090 数字对
发现如果给定两个数(a,b),可以用类似辗转相除法在logn的时间内计算出(反向)变到(1,1)的最小步数. 然而并不知道另一个数是多少? 暴力嘛,枚举一下另一个数,反正1000000的nlogn不虚 ...
- CentOS6.5磁盘分区和挂载操作记录
CentOS6.5磁盘分区和挂载操作记录. [root@CentOS ~]# fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders ...
- python程序展现图片
突然想写一个python程序能够显示图片的 ,展示文字的已经实现了 现在就搞一搞这个吧 相信也是很简单 首先是放一张图片在e盘下面 等会程序打包的时候将会用到 就决定是你啦 皮卡丘: 然后就写代码吧:
- SimpleDataFormat详解
[转]SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...
- CF817A Treasure Hunt
思路: 起点(x1, y1),终点(x2, y2),步长(dx, -dy),(dx, dy),(-dx, -dy),(-dx, dy).只要满足abs(x1 - x2) % dx == 0 并且 ab ...
- Python批量下载电视剧电影--自己动手丰衣足食
前言 为了看美剧<天蝎>,在电影天堂找到了,于是就想下载下来好好欣赏. 废话不说了,直接上代码. 代码 import requests,re,os,time url = "htt ...
- MySQL 帮助类 MySQLHelper
/// <summary> /// MySqlHelper操作类 /// </summary> public sealed partial class MySQLHelper ...
- Vue.js——router-link阻止click事件
router-link 只能单纯做路由跳转 https://segmentfault.com/q/1010000007896386