Codeforces Beta Round #71 C【KMP+DP】
Codeforces79C
题意:
求s串的最大子串不包含任意b串;
思路:
dp[i]为以i为起点的子串的最长延长距离。
我们可以想到一种情况就是这个 i 是某个子串的起点,子串的长度-1就是最短,
或者这个 前面的长度 +这个i 就是答案。
所以预处理一下,以 i 为起点的最短子串距离就好了,这里利用KMP比较方便。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int INF=0x3f3f3f3f; const int N=1e5+10;
int vis[N];
char s[N];
int Next[N],lens,lenp;
int dp[N]; //dp[i]为以i为起点的最长距离。 void GetNext(char *str)
{
int i=0,j=-1;
int len=strlen(str);
Next[0]=-1;
while(i<len)
{
if(j==-1||str[i]==str[j])
Next[++i]=++j;
else
j=Next[j];
}
} void KMP(char *p,int x)
{
int i=0,j=0;
lenp=strlen(p);
while(i<lens&&j<lenp)
{
if(j==-1||p[j]==s[i])
{
i++;j++;
}
else
j=Next[j];
if(j==lenp)
{
vis[i-lenp]=min(vis[i-lenp],lenp);
j=Next[j];
}
}
} int main()
{
int n;
char b[15];
scanf("%s",s);
lens=strlen(s);
scanf("%d",&n);
memset(vis,INF,sizeof(vis)); for(int i=1;i<=n;i++)
{
scanf("%s",b);
GetNext(b);
KMP(b,i);
}
/*
for(int i=0;i<lens;i++)
printf("%d\n",vis[i]);
puts("");
*/
int pos=lens;
dp[n]=0;
for(int i=lens-1;i>=0;i--)
{
dp[i]=min(vis[i]-1,dp[i+1]+1);
if(dp[i]>dp[pos]) pos=i;
} if(pos==lens)
{
puts("0 0");
return 0;
}
printf("%d %d\n",dp[pos],pos);
return 0;
}
Codeforces Beta Round #71 C【KMP+DP】的更多相关文章
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- 【KMP+DP】Count the string
KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...
- Codeforces Beta Round #10 D. LCIS(DP&LCIS)
D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】
Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- P5404-[CTS2019]重复【KMP,dp】
正题 题目链接:https://www.luogu.com.cn/problem/P5404 题目大意 给出一个字符串\(S\),然后求有多少个长度为\(m\)的串\(T\)满足.无限多个串\(T\) ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- Codeforces 815C. Karen and Supermarket【树形DP】
LINK 思路 首先发现依赖关系是一个树形的结构 然后因为直接算花多少钱来统计贡献不是很好 因为数组开不下 那就可以算一个子树里面选多少个的最小代价就可以了 注意统计贡献的时候用优惠券的答案只能在1号 ...
随机推荐
- 英语发音规则---Z字母
英语发音规则---Z字母 一.总结 一句话总结:字母Z的名称zed /zed/,美式英语的称zee /zi:/,少数方言(如香港)读izzard /'izəɹd/. 1.字母Z在单词中发[z]? pu ...
- Java之 将程序打包成jar包
准备材料: 1.java文件: Helloworld.java package com.ray; public class HelloWorld{ public static void main(St ...
- django--mysql配置
配置mysql的过程中出现了许多错误,通过以下配置,解决问题: python3不能直接调用MySQLdb,所以需要安装pymysql,并在jango项目文件__init__.py中加入: import ...
- 2018-06-07 RF test 1 :TX Power test
Test item: 1.Output power: 屏蔽网房-同轴线-频谱仪 The radio circuitry, generally referred to as the Device U ...
- FFmpeg 基本用法
FFmpeg FFmpeg 基本用法 本课要解决的问题 1.FFmpeg的转码流程是什么? 2.常见的视频格式包含哪些内容吗? 3.如何把这些内容从视频文件中抽取出来? 4.如何从一种格式转换为另一种 ...
- VBS错误代码释义
VBScript 语法错误 如果 VBScript 语句结构违反了一个或多个 VBScript 脚本语言语法规则,就会产生 VBScript 语法错误.VBScript 语法错误通常在执行程序前,编译 ...
- Python 写文件时的Unicode设置
今天在把Evenote的笔记内容写为文件时出错: f.write(content) UnicodeEncodeError: 'gbk' codec can& ...
- LVS实战1
(一).NAT模式:NAT模型:地址转换类型,主要是做地址转换,类似于iptables的DNAT类型,它通过多目标地址转换,来实现负载均衡:特点和要求: 1.LVS(Director)上面需要双网卡: ...
- cmake opencv,dlib 编译静态库 1
无论windows,linux 所有的库 ,dlib,opencv 通过cmake-gui 设置好静态库, 动态库,和其他各个选项 Tips: cmake 优先级用cmake-gui,因为命令太多,容 ...
- mount总结
挂载分区 mount基本语法 mount [参数] /dev/sdb1(需要挂载的分区) /sdb1(挂载目录) 参数是可选的,也可以不带参数,参数的使用方法(-o ro,sync,atime). 参 ...