432D Prefixes and Suffixes
题目大意
给你一个串
对于一个子串如果它既是前缀又是后缀
输出它的长度以及它在原串中一共出现了多少次
分析
对于既是前缀又是后缀的判断和126B相同
然后我们只需要记录每个不同的z[i]出现了多少次
然后对于每个合法z[i]输出所有大于z[i]的数的出现次数即可
因为如果长度为z[i]的前缀是最长前缀
那么小于z[i]的前缀一定合法
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
char s[];
int n,z[],sum[],cnt,ans[];
inline void get_z(){
int i,j,k,l=,r=;
for(i=;i<n;i++){
if(i<=r)z[i]=min(r-i+,z[i-l]);
while(i+z[i]<n&&s[z[i]]==s[z[i]+i])z[i]++;
if(i+z[i]->r)r=i+z[i]-,l=i;
}
}
int main(){
int i,j,k;
scanf("%s",s);
n=strlen(s);
get_z();
for(i=;i<n;i++){
sum[z[i]]++;
if(z[i]==n-i)ans[++cnt]=z[i];
}
printf("%d\n",cnt+);
sort(ans+,ans+cnt+);
for(i=n-;i>;i--)sum[i]+=sum[i+];
for(i=;i<=cnt;i++)printf("%d %d\n",ans[i],sum[ans[i]]+);
printf("%d %d\n",n,);
return ;
}
432D Prefixes and Suffixes的更多相关文章
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- codeforces 432D Prefixes and Suffixes
由于包含了前缀与后缀,很容易想到用KMP去算前缀与后缀的公共缀.另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了.先分别用KMP算法与后缀自动机跑一遍,然 ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
- Codeforces 432D Prefixes and Suffixes:KMP + dp
题目链接:http://codeforces.com/problemset/problem/432/D 题意: 给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次. 题解 ...
- Codeforces 432D Prefixes and Suffixes (KMP、后缀数组)
题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- 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 432 D. Prefixes and Suffixes
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...
随机推荐
- C#里sqlDataAdapter.fill(DataSet,String)的用法
第二个参数 String是指定DataSet 里表的名字,例如 sqlDataAdapter.fill(DataSet,"学生表") 指定后,以后就可以这样调用这张表 DataSe ...
- python基础-5 冒泡排序、递归
上节总结 一.上节内容补充回顾 1.lambda func = lambda x,y: 9+x 参数: x,y 函数体:9+x ==> return 9+x func: 函数名 def func ...
- [Web 前端] 013 css 内外边距
1. css 内间距 也称:"内补白"或"内补丁" 参数 释义 padding 检索或设置对象四边的内部边距,如 padding:10px; padding:5 ...
- vscode怎么修改颜色主题里的某种颜色
我习惯用深色主题, 齿轮--->颜色主题 ---->monokai是我的菜. 比较精神,又不刺眼. 但是这个主题的注释的颜色太浅了. 几乎和背景重合. 注释很重要, 能体现和记录自己的代码 ...
- Codeforces 840C 题解(DP+组合数学)
题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...
- Codeforces 601B(贪心+斜率+组合数学+单调栈)
题面 传送门 题目大意: L(h)的值是区间[L,R]内,abs(h[i]-h[j])/(i-j)的最大值.现在有q个询问,每个询问表示询问区间[L,R]内,所有子序列的L(h)的值的和 分析 将|h ...
- Fiddler用法整理
目 录 1 Fiddler的基本介绍 1.1 下载安装 1.2 适用平台 2 Fiddler的工作原理 3 同类工具 4 捕获非IE浏览器的会话 5 捕获不同请求的设置方法 5.1 Web HTTPS ...
- jmeter 线程数—请求数详解
一个性能测试请求负载是基于一个线程组完成的.一个测试计划必须有一个线程组.测试计划添加线程组非常简单.在测试计划右键弹出下拉菜单(添加-->Threads(Users)--->线程组)中选 ...
- HTML创建链接框
使用CSS样式创建一个漂亮的链接框吧 <!DOCTYPE html> <html> <head> <style> a:link,a:visited { ...
- 【记录】mybatis mapper.xml 基础
<choose> <when test=""> //... </when> <otherwise> //... </other ...