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. oracle DB 使用注意点小结

    1.DDL 后不需要添加commit;语句,因为Oracle数据库的DDL不支持transaction,执行即commit: DDL(Data Definition Language): 数据定义语言 ...

  2. 刷题78. Subsets

    一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...

  3. sql查询如何将A表数据name字段对应B表name字段得到对应的B表id主键然后添加A到表usel_id中

    1.写这个的原因 最近在写公司项目的时候一个功能很是让我头疼如标题看到的一样,平时我们一般都只负责数据表的查询或者连表查询某一个字段和A表字段一起显示出来. 但是添加到A表还是头一次,第一天想了很久都 ...

  4. c# 使用T4模板生成实体类(sqlserver)

    新建类库,右键添加 "文本模板" 添加完成之后生成如下后缀为 tt的文件: 双击文件:TextTemplate_Test.tt 文件打开,替换代码如下 <#@ templat ...

  5. 【pattern】设计模式(2) - 模版方法模式

    前言 一晃一年又过了,还是一样的渣. 一晃2周又过去了,还是没有坚持写博客. 本来前2天说填一下SQL注入攻击的坑,结果看下去发现还是ojdbc.jar中的代码,看不懂啊.这坑暂时填不动,强迫在元旦最 ...

  6. 网络共享服务(一)之FTP

    网络共享服务:ftp,nfs,samba比较 从跨平台角度说, samba, ftp差不多, 而nfs不支持windows平台 从挂载角度说, samba, nfs可以把远程目录挂载到本地目录上, 对 ...

  7. 二分查找 python实现

    欢迎回来 [^first blood]. 要求A是升序数组 递归 只能查 数据存不存在,不能返回下标 def binary_find(A, m): if len(A) == 0: return -1 ...

  8. mac屏幕录制

    屏幕录制 shift+command+5 录制完成后将文件拖拽到要保存的文件中

  9. UVA - 12333 Revenge of Fibonacci (大数 字典树)

    The well-known Fibonacci sequence is defined as following: F(0) = F(1) = 1 F(n) = F(n − 1) + F(n − 2 ...

  10. Wannafly Camp 2020 Day 2I 堡堡的宝藏 - 费用流

    感谢这道题告诉我KM求的是 完备 最大权匹配 :( #include <bits/stdc++.h> using namespace std; #define reset(x) memse ...