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(kmp+dp)

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

  2. Codeforces 432D Prefixes and Suffixes(KMP+dp)

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

  3. 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 ...

  4. Codeforces 432 D. 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 #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 ...

  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 #246 (Div. 2) D. Prefixes and Suffixes

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

  9. BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)

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

随机推荐

  1. 客户端负载均衡框架:Spring Cloud Ribbon

    最近在学习Spring Cloud的知识,现将客户端负载均衡框架 Spring Cloud Ribbon 的相关知识笔记整理如下.[采用 oneNote格式排版]

  2. 怎么在IDEA中给方法添加分割线?

    方法中间分割不清晰 怎么在IDEA中给方法添加分割线呢? 效果如图 方法上有一条分割线,比较明了 按照下列顺序点击修改设置即可 File→Settings→Editor→General→Appeara ...

  3. git文件冲突合并的报错:Your local changes to the following files would be overwritten by merge

    记录一下在项目里使用git遇到代码冲突时的解决方法 问题:当我和我同事两个人改了相同的一个文件,他在我提交前提交了,这时候我就提交不了了,并且也pull不下来他的代码 会报错: Your local ...

  4. Linux设置文件权限和归属

    前言:在Linux文件系统的安全模型中,为系统中的文件(或目录)赋予了两个属性:访问权限和文件所有者,简称为“权限”和“归属”.其中,访问权限包括读取.写入.可执行三种基本类型,归属包括属主(拥有该文 ...

  5. 剑指offer-面试题11-旋转数组的最小数字-二分法

    /* 题目: 把一个数组最开始的若干个元素搬到数组末尾,我们称之为数组的旋转. 输入一个递增排序的数组的旋转,输出旋转数组中的最小元素. */ /* 思路: 采用二分的方法,旋转数组实质上是上左半部分 ...

  6. ASP.NET Identity系列教程-4【Identity高级技术】

    https://www.cnblogs.com/r01cn/p/5194257.html 15 ASP.NET Identity高级技术 In this chapter, I finish my de ...

  7. Java知识点题集

    一.红黑树的特性 (1)每个节点或者是黑色,或者是红色. (2)根节点是黑色. (3)每个叶子节点(NIL)是黑色. [注意:这里叶子节点,是指为空(NIL或NULL)的叶子节点!] (4)如果一个节 ...

  8. python递归删除目录本身以及目录下文件

    import os def local_rm(dirpath): if os.path.exists(dirpath): files = os.listdir(dirpath) for file in ...

  9. java学习笔记之IO编程—对象序列化

    对象序列化就是将内存中保存的对象以二进制数据流的形式进行处理,可以实现对象的保存或网络传输. 并不是所有的对象都可以被序列化,如果要序列化的对象,那么对象所在的类一定要实现java.io.Serial ...

  10. react-发表评论案例

    评论列表组件 import React from 'react' import CMTItem from './CmtItem.jsx' import CMTBox from './CmtBox.js ...