codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D
转自:https://blog.csdn.net/tc_to_top/article/details/38793973
题意
给出一个字符串,求有多少种长度的前缀和后缀相等,并且这种形式的子串在原字符串中出现的次数。
分析
#include <cstdio>
#include <cstring>
int const MAX = 1e5 + ;
char s[MAX];
int next[MAX];
int len, pos, num;
int l[MAX], cnt[MAX], re[MAX]; void get_next(){
int i = , j = -;
next[] = -;
while(s[i] != '\0') {
if(j == - || s[i] == s[j]){
i ++;
j ++;
next[i] = j;
}else
j = next[j];
}
} void solve(){
for(int i = ; i <= len; i++)
re[i] = ;
for(int i = len; i >= ; i --)
if(next[i] != -)
re[next[i]] += re[i];
int pos = len;
while(pos){
cnt[num] = re[pos];//次数
l[num ++] = pos;//长度
pos = next[pos];
}
} int main(){
scanf("%s", s);
len = strlen(s);
get_next();
solve();
printf("%d\n", num);
for(int i = num - ; i >= ; i--)
printf("%d %d\n", l[i], cnt[i]);
}
codeforces - 432D Prefixes and Suffixes (next数组)的更多相关文章
- 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、后缀数组)
题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...
- Codeforces 432D Prefixes and Suffixes:KMP + dp
题目链接:http://codeforces.com/problemset/problem/432/D 题意: 给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次. 题解 ...
- codeforces 432D Prefixes and Suffixes
由于包含了前缀与后缀,很容易想到用KMP去算前缀与后缀的公共缀.另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了.先分别用KMP算法与后缀自动机跑一遍,然 ...
- 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 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- codeforces Round 246 D. Prefixes and Suffixes (后缀数组 || KMP)
题目大意: 求一个子串,子串既是前缀又是后缀. 然后再求出它在整个串中出现的次数. 思路分析: 能够非常easy想到怎样推断一个串既是前缀又是后缀. 仅仅须要它与 sa[0] 的lcp 等于 整个串的 ...
- 432D Prefixes and Suffixes
题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 ...
随机推荐
- 原生js作用域(红宝书)
function fn(){ ; alert(a); // 2; } alert(a);//未被定义: alert(b);//全局变量:b=2: ; function fn1(){ ; functio ...
- Docker安装指定版本
今天新增一个Docker服务器,Docker安装顺利,启动hello-world测试的时候却出现了问题: $ docker run hello-worldUnable to find image 'h ...
- Vue 初识Vue
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- 使用 jstack 查询线程死锁错误日志 定位问题
定位问题 (1) 首先 找到相应的进程 使用 ps -ef | grep 'com.sankuai.qcs.regulation.dispatch' 找到进程的ID;==>21980 (2) t ...
- delphi 通过事务插入数据
orsn1.StartTransaction; try qry1.Sql.Clear; qry1.Sql.Text:=' select * from log '; qry1.Open; qry1.In ...
- python之tkinter使用-文件系统遍历
# tkinter:文件系统遍历 import tkinter as tk, os from time import sleep class DirList(object): def __init__ ...
- Windows 下vim的配置文件_vimrc
set nocompatible source $VIMRUNTIME/vimrc_example.vim source $VIMRUNTIME/mswin.vim behave mswin set ...
- Linux 配置Samba服务
查看系统下是否已经安装了sambarpm -qa |grep samba 安装sambayum -y install samba 配置samba创建目录sambamkdir -p /home/samb ...
- 【题解】 bzoj3450 JoyOI1952 Easy (期望dp)
题面戳我 Solution 期望的题目真心不太会 定义状态\(f[i]\)表示到第\(i\)期望长度,\(dp[i]\)表示期望分数 如果上一步的持续\(o\)长度为\(L\),那么贡献是\(L^2\ ...
- 自学Zabbix3.11-宏Macros
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.11-宏Macros zabbix宏变量让zabbix变得更灵活,它根据一系列 ...