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中出现了 ...
随机推荐
- bzoj 1725: [Usaco2006 Nov]Corn Fields牧场的安排【状压dp】
压一维状态,转移时把符合条件的上一行加上 #include<iostream> #include<cstdio> using namespace std; const int ...
- lodop 打印
使用Lodop打印: 一.在官网下载http://www.lodop.net/download.html 若是安装还是提示未安装,就按转这个 二.准备两个js 三.需要在页面最上面加入 <htm ...
- less新手入门(四)—— Mixin Guards
八.Mixin Guards 有条件的 mixin 当您想要匹配表达式时,相对于简单的值或特性,Guards是有用的.如果您熟悉函数式编程,您可能已经遇到过它们. 为了尽可能地保持CSS的声明性质,在 ...
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...
- NHibernate3.2学习笔记
一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.涉及第三方程序集 NHibernate.dll:版本3.2 Iesi.C ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- html与html5 总结
时间:于2017年12月3日 19:35:18开始 用于: 这文章用于个人技术总结(全文转发请标明出处,小段摘抄随意) HTML笔记1.html标签不区分大小写,但是建议小写2.常用标签: 可用在he ...
- python模块中的__all__属性
转自:http://blog.csdn.net/sxingming/article/details/52903377 python模块中的__all__属性,可用于模块导入时限制,如:from mod ...
- UVM基础之---------uvm report 机制分析
uvm 中的信息报告机制相对来说比较简单,功能上来说主要分为两部分: 第一通过ID对component的信息报告冗余级别进行控制,针对每个冗余级别进行不同的行为控制.这部分工作主要由uvm_repor ...
- quartz在集群环境下的最终解决方案
在集群环境下,大家会碰到一直困扰的问题,即多个 APP 下如何用 quartz 协调处理自动化 JOB . 大家想象一下,现在有 A , B , C3 台机器同时作为集群服务器对外统一提供 SERVI ...