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中出现了 ...
随机推荐
- bzoj1770: [Usaco2009 Nov]lights 燈(折半搜索)
1770: [Usaco2009 Nov]lights 燈 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1153 Solved: 564[Submi ...
- springboot(二)整合mybatis,多数据源和事务管理
-- 1.整合mybatis -- 2.整合多数据源 -- 3. 整合事务 代码地址:https://github.com/showkawa/springBoot_2017/tree/master/ ...
- SpringBoot集成MybatisPlus解决Mapper文件修改后动态刷新的问题
很多人在使用SpringBoot集成Mybatis或者MybatisPlus的时候在查询复杂的情况下会写mapper文件,虽然说MyBatisPlus提供了常用的增删查改,但还是难以应付复杂的查询.关 ...
- (DP)51NOD 1085 背包问题
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数 ...
- 讯搜问题排查xunsearch
mysql导入数据不成功,开始重建索引后提示 [XSException] ../local/xunsearch/sdk/php/lib/XS.php(1898): DB- 可打印的版本 开始重建索引 ...
- printf的整型
参 数 说 明 %d 输出数字长为变量数值的实际长度 %md 输出m位(不足补空格,大于m位时按实际长度输出) %-md m含义同上.左对齐输出 %ld l(小写字母)表示输出“长整型”数据 %m1 ...
- [C陷阱和缺陷] 第3章 语义“陷阱”
第3章 语义"陷阱" 一个句子哪怕其中的每个单词都拼写正确,而且语法也无懈可击,仍然可能有歧义或者并非书写者希望表达的意思.程序也有可能表面上是一个意思,而实际上的意思却相 ...
- 一个Velocity Template Language学习的框架
Velocity Template Language(VTL)使得数据展示和后台代码的开发分离开来,最初用在基于servlet的网站开发上,它的这一特性使得它在应付MVC Web开发模式时显得尤其合适 ...
- python网络爬虫。第一次测试-有道翻译
2018-03-0720:53:56 成功的效果如下 代码备份 # -*- coding: UTF-8 -*- from urllib import request from urllib impor ...
- spark web ui
spark UI 界面:http://www.cnblogs.com/xing901022/p/6445254.html 几个概念的解释:http://blog.csdn.net/jiangwlee/ ...