Codeforces 432D Prefixes and Suffixes:KMP + dp
题目链接:http://codeforces.com/problemset/problem/432/D
题意:
给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次。
题解:
先对原串求一次nex数组。
然后枚举位置i:
sub(k)表示前缀s[0 to k]
dp[i]表示sub(i)在原串s中的出现次数
假设nex[i] = a, nex[a] = b, nex[b] = c ... nex[z] = -1
则sub(a), sub(b), sub(c)...sub(z)均以s[i]结尾,dp[a...z]均+1。
然而一个一个加会T...
所以:
初始时所有的 dp = 1
每次:if(nex[i] != -1) dp[nex[i]] += dp[i]
一路传递下去就好。
最后从nex[len-1]开始往前跳nex。
对于每次跳到的nex,sub(nex)一定也是s的后缀。
此时输出它的出现次数dp[nex]。
另外因为要顺着输出,而nex是倒着跳的,所以先存到stack里再输出。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#define MAX_N 100005 using namespace std; int n;
int dp[MAX_N];
int nex[MAX_N];
char s[MAX_N]; void cal_nex(char *p,int len)
{
nex[]=-;
int k=-;
for(int i=;i<len;i++)
{
while(k>- && p[i]!=p[k+]) k=nex[k];
if(p[i]==p[k+]) k++;
nex[i]=k;
}
} int main()
{
scanf("%s",s);
n=strlen(s);
cal_nex(s,n);
for(int i=;i<n;i++) dp[i]=;
for(int i=n-;i>=;i--)
{
if(nex[i]!=-) dp[nex[i]]+=dp[i];
}
stack<int> stk;
int p=n-;
while(p!=-)
{
stk.push(p);
p=nex[p];
}
cout<<stk.size()<<endl;
while(!stk.empty())
{
int now=stk.top();
stk.pop();
cout<<now+<<" "<<dp[now]<<endl;
}
}
Codeforces 432D Prefixes and Suffixes:KMP + dp的更多相关文章
- 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去算前缀与后缀的公共缀.另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了.先分别用KMP算法与后缀自动机跑一遍,然 ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- Codeforces 461B Appleman and Tree:Tree dp
题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...
- Codeforces Beta Round #71 C【KMP+DP】
Codeforces79C 题意: 求s串的最大子串不包含任意b串: 思路: dp[i]为以i为起点的子串的最长延长距离. 我们可以想到一种情况就是这个 i 是某个子串的起点,子串的长度-1就是最短, ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
随机推荐
- Mybatis-There is no getter for property named 'id' in 'class java.lang.String'
<mapper namespace="cn.telchina.standard.mapper.SysOrgnMapper"> <!-- <![CDATA[s ...
- windows svchost.exe 引起的出现的莫名其妙的窗口失去焦点
我不知道你们遇到没,反正我是遇到了,现在我就把解决方法给你们,当然都是从网上整理下来的 这个失去焦点可以分为两种,一种是病毒,一种是系统自带的问题 首先你得知道自己的窗口被什么给挤掉了焦点 先看看这篇 ...
- jmeter java 请求 payload
1.注册页面抓包看见内容如下: POST http://test.nnzhp.cn/bbs/forum.php?mod=post&action=edit&extra=&edit ...
- junit spring 测试
http://my.oschina.net/dlpinghailinfeng/blog/336694 http://blog.csdn.net/zhangzikui/article/details/1 ...
- Linux中的提示符
root的提示符:# 一般用户的提示符:$
- 【BZOJ3060】[Poi2012]Tour de Byteotia 并查集
[BZOJ3060][Poi2012]Tour de Byteotia Description 给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上. Input ...
- nginx搭建服务
1.当然首先是安装nginx :http://nginx.org/en/download.html 2.一波安装之后 3.在nginx 文件夹打开命令行:cmd 4.开启nginx 服务命令:star ...
- 【python】-- 内置函数、软件目录开发规范(代码编码风格)
内置函数 一.内置函数表格 二.内置函数演示 1.abs(x) 功能:取数的绝对值 >>> abs(-1) #取-1的绝对值 1 ########################## ...
- MySQL时间函数-获取当前时间-时间差
MySQL中获取当前时间为now(),不同于sqlserver getdate(). SQLServer转MySQL除变化top 1 -> limit 1之后报错: limit [Err] 15 ...
- TCP协议要点和难点全解
转载自http://www.cnblogs.com/leetieniu2014/p/5771324.html TCP协议要点和难点全解 说明: 1).本文以TCP的发展历程解析容易引起混淆,误会的方方 ...