CF432D Prefixes and Suffixes
CF432D Prefixes and Suffixes
题意
给你一个长度为n的长字符串,“完美子串”既是它的前缀也是它的后缀,求“完美子串”的个数且统计这些子串的在长字符串中出现的次数
分析
求出nex数组 , 在求出每个前缀出现的次数 , 从nex[n] 往下走就行了
其实这道题是 , KMP 求每个前缀出现次数的模板题
求前缀出现次数的写法
for(int i = 1 ; i <= n ; ++i) num[i]++;
for(int i = n ; i >= 1 ; --i) num[nex[i]] += num[i];
这个的意思是 , 首先长度为 i 的前缀有大自己 , 也就是 1
在考虑 长的为 i 的前缀可以把它的次数转移给谁呢?前缀本身已经计算过了, 接下来只要计算不是前缀本身的 , nex[i] 是他的最长公共前后缀,也就是有一个以 i 为右端点, 长为 nex[i] 的子串 和前缀相同 , 所以 i 出现多少次 , nex[i] 的前缀也应该加上他出现的次数
for(int i = 1 ; i <= n ; ++i) num[nex[i]]++;
for(int i = n ; i >= 1 ; --i) num[nex[i]] += num[i];
for(int i = 1 ; i <= n ; ++i) num[i]++;
这个的意思是 , 先刨除他本身 ,再在最后加上;
个人推荐第一种好理解 , 还短
上本题代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e5+100;
int n;
int nex[N] , num[N] , cnt[N];
char c[N];
int main()
{
scanf("%s",c+1); n = strlen(c+1);
for(int i = 2 , k = 0 ; i <= n ; ++i)
{
while(k && c[i] != c[k + 1]) k = nex[k];
if(c[i] == c[k + 1]) k++; nex[i] = k;
}
int tot = 0 , k = nex[n];
while(k) cnt[++tot] = k , k = nex[k];
printf("%d\n" , tot + 1);
// for(int i = 1 ; i <= n ; ++i) num[i]++;
// for(int i = n ; i >= 1 ; --i) num[nex[i]] += num[i];
for(int i = 1 ; i <= n ; ++i) num[nex[i]]++;
for(int i = n ; i >= 1 ; --i) num[nex[i]] += num[i];
for(int i = 1 ; i <= n ; ++i) num[i]++;
for(int i = tot ; i >= 1 ; --i)
printf("%d %d\n" , cnt[i] , num[cnt[i]]);
printf("%d 1\n" , n);
return 0;
}
CF432D Prefixes and Suffixes的更多相关文章
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据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 432 D. Prefixes and Suffixes
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- CodeForces Round #527 (Div3) C. Prefixes and Suffixes
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
- BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)
将每个串正着插入Trie A中,倒着插入Trie B中. 并求出每个串在A,B中的dfs序. 每次查询等价于查询在A中dfs序在[la,ra]之间,在B中dfs序在[lb,rb]之间的串的个数,用主席 ...
随机推荐
- SURF特征检测
SERF(speed up robust feature )特征的关键特性: 特征检测 尺度空间:缩放到不同的大小或分辨率仍能检测 选择不变性:光照不变,旋转不变 特征向量:描述为一个特征向量 DDN ...
- vue自学入门-6(vue jsx)
目录: vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5( ...
- 在javascript编程语言中,数据类型boolean的相关知识
一. 1.字符串类型: 空字符串返回false,非空字符串均返回true; 2.数值类型: 0或NaN返回false,其他数值返回true; 3.布尔类型: false返回false,true返回tr ...
- html颜色名颜色代码对照表
- R 分析回归(一元回归)
x <- c(,,,,,,,,,) # build X(predictor) y <- c(,,,,,,,,,) # build Y(dependent variable) mode(x) ...
- mybatis入门案例2
1. 笔记:1.配置了typeAlias之后,在其他需要写com.itheima.domain.User的地方都可以用user代替 2.先用properties指定了jdbcConfig.proper ...
- 积分题1之来自G.Han的一道积分题
今天,收到G.Han的提问,第一个是计算积分 \[\int_0^{\infty}{\frac{\ln x}{(x^2+1)^n}dx}\]顿时不明觉厉,然后在宝典<Table of Integr ...
- 【你不知道的javaScript 中卷 笔记1】javaScript中的类型与值
一.类型与值 1.0 javaScript 七种内置类型: 空值(null) 未定义(undefined) 布尔值( boolean) 数字(number) 字符串(string) 对象(object ...
- WPF Dispatcher.BeginInvoke子线程更新UI
在开发WPF应用时出现:”调用线程无法访问此对象,因为另一个线程拥有该对象.“ 是因为UI线程是WPF应用的主线程,若尝试子线程更新UI线程应使用Dispatcher.BeginInvoke()或者I ...
- JavaScript仿计算器案例源代码
效果图 index.html <!DOCTYPE html> <html> <head> <title></title> <link ...