CF432D Prefixes and Suffixes

时间:2022-09-05 13:08:27

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的更多相关文章

  1. codeforces432D Prefixes and Suffixes&lpar;kmp&plus;dp&rpar;

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  2. Codeforces 432D Prefixes and Suffixes&lpar;KMP&plus;dp&rpar;

    题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...

  3. Codeforces Round &num;246 &lpar;Div&period; 2&rpar; D&period; Prefixes and Suffixes&lpar;后缀数组orKMP&rpar;

    D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. Codeforces 432 D&period; Prefixes and Suffixes

    用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...

  5. Codeforces 1092C Prefixes and Suffixes(思维)

    题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...

  6. CodeForces Round &num;527 &lpar;Div3&rpar; C&period; Prefixes and Suffixes

    http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...

  7. Codeforces 432D Prefixes and Suffixes kmp

    手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...

  8. Codeforces Round &num;246 &lpar;Div&period; 2&rpar; D&period; Prefixes and Suffixes

                                                        D. Prefixes and Suffixes You have a string s = s ...

  9. BZOJ3483 &colon; SGU505 Prefixes and suffixes(询问在线版)

    将每个串正着插入Trie A中,倒着插入Trie B中. 并求出每个串在A,B中的dfs序. 每次查询等价于查询在A中dfs序在[la,ra]之间,在B中dfs序在[lb,rb]之间的串的个数,用主席 ...

随机推荐

  1. log4net配置与初始化

    1.配置文件: <?xml version="1.0" encoding="utf-8" ?> <configuration>   &l ...

  2. &lbrack;C&num;&rsqb; 走进异步编程的世界 - 开始接触 async&sol;await&lpar;转&rpar;

    原文链接:http://www.cnblogs.com/liqingwen/p/5831951.html 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 ...

  3. iOS 进阶 第二天&lpar;0324&rpar;

    0324 创建transform transform 是形变属性. 如下图: 如果按照上面的方法来创建的话是这样解释:是相对初始状态来说的,不会在变化后的基础上进行形变.如果要持续变化就要自己去不断改 ...

  4. Eclipse自动生成文档注释

    /** *这种格式的注释就是文档注释 */ 快捷键是alt+shift+j,将光标放在类名,变量名,方法名上,按快捷键.

  5. Convert Sorted List to Balanced Binary Search Tree &lpar;BST&rpar;

    (http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...

  6. 第三篇——第二部分——第二文 计划搭建SQL Server镜像

    原文:第三篇--第二部分--第二文 计划搭建SQL Server镜像 本文紧跟上一章:SQL Server镜像简介 本文出处:http://blog.csdn.net/dba_huangzj/arti ...

  7. C&plus;&plus;数据结构之二叉查找树(BST)

    C++数据结构之二叉查找树(BST) 二分查找法在算法家族大类中属于“分治法”,二分查找的过程比较简单,代码见我的另一篇日志,戳这里!因二分查找所涉及的有序表是一个向量,若有插入和删除结点的操作,则维 ...

  8. BNU Online Judge-34978-汉诺塔

    题目链接 http://www.bnuoj.com/bnuoj/problem_show.php?pid=34978 这题在比赛时AC了不过那时是根据测试数据  抱着来试一下的想法,没想就AC了,其实 ...

  9. nginx反向代理uwsgi django服务器搭建总结

    1.安装python.django.虚拟环境 参考帖子:安装python django https://blog.csdn.net/a249900679/article/details/5152720 ...

  10. 2017乌鲁木齐区域赛D题Fence Building-平面图的欧拉公式

    这个题B站上面有这题很完整的分析和证明,你实在不懂,可以看看这个视频  https://www.bilibili.com/video/av19849697?share_medium=android&a ...