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 1045: [HAOI2008] 糖果传递【瞎搞】
感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...
- bzoj 1603: [Usaco2008 Oct]打谷机【瞎搞】
一棵树,碰到改变转向的边就异或一下,从1dfs一遍 #include<iostream> #include<cstdio> using namespace std; const ...
- 在CentOS下安装VMware tool
VMware tools是虚拟机VMware Workstation自带的一款工具.它的作用就是使用户可以从物理主机直接往虚拟机里面拖文件.如果不安装它,我们是无法进行虚拟机和物理机之间的文件传输的. ...
- 汇编程序49:实验14 访问CMOS RAM(显示系统时间)
assume cs:code ;安装程序,使用指令out和in指令 code segment start: mov ax,cs mov ds,ax mov si,offset sub1 mov ax, ...
- [转]C语言/C++中如何产生随机数
C语言/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,和time()函数. 需要说明的是,iostream头文件中就有srand函数的定义,不需要再额外引入stdlib. ...
- SugarCRM安装过程——PHP文件上传限制问题
找到D:\xampp\php目录下,php文件中的php.ini文件,用写字板打开: 1.查找post_max_size,指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值,默认为8M, ...
- UVM基础之------uvm_port_base
Port Base Classes uvm_port_component_base This class defines an interface for obtaining a port ...
- 7z.exe 命令行压缩文件排除文件(exclude filenames) 手记
命令行使用格式:Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] ...
- 【译】x86程序员手册28-7.7任务地址空间
7.7 Task Address Space 任务地址空间 The LDT selector and PDBR fields of the TSS give software systems desi ...
- MySql学习笔记(三) —— 聚集函数的使用
1.AVG() 求平均数 select avg(prod_price) as avg_price from products; --返回商品价格的平均值 ; --返回生产商id为1003的商品价格平均 ...