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

  1. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  2. 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]中有多少个数 ...

  3. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  4. 【KMP+DP】Count the string

    KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...

  5. Codeforces Beta Round #10 D. LCIS(DP&amp;LCIS)

    D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  6. Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】

     Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. P5404-[CTS2019]重复【KMP,dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P5404 题目大意 给出一个字符串\(S\),然后求有多少个长度为\(m\)的串\(T\)满足.无限多个串\(T\) ...

  8. CodeForces 161D Distance in Tree【树形DP】

    <题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...

  9. Codeforces 815C. Karen and Supermarket【树形DP】

    LINK 思路 首先发现依赖关系是一个树形的结构 然后因为直接算花多少钱来统计贡献不是很好 因为数组开不下 那就可以算一个子树里面选多少个的最小代价就可以了 注意统计贡献的时候用优惠券的答案只能在1号 ...

随机推荐

  1. asp.net中异步调用WebService(异步页)[转]

    由于asp2.0提供了异步页的支持使异步调用WebService的性能有了真正的提升.使用异步页,首先要设置Async="true",异步页是在Prerender和Prerende ...

  2. hibernate复习第(三)天

    今日要点: 1.继承关系映射 一个表对应一个映射树(subclass) 一个子类一个表,子类只有父类没有的字段(joined-subclass) 鉴别器和内连接结合使用(subclass join) ...

  3. django--mysql配置

    配置mysql的过程中出现了许多错误,通过以下配置,解决问题: python3不能直接调用MySQLdb,所以需要安装pymysql,并在jango项目文件__init__.py中加入: import ...

  4. 判断iOS系统的Model

    获取iOS系统的Model   (参考网址:https://www.theiphonewiki.com/wiki/Models) + (NSString *)getModel{ struct utsn ...

  5. bzoj 2648 SJY摆棋子 —— K-D树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2648 学习资料:https://blog.csdn.net/zhl30041839/arti ...

  6. poj 2390 Bank Interest(计算本利和)

    一.Description Farmer John made a profit last year! He would like to invest it well but wonders how m ...

  7. [转]unity3d中创建双面材质

    在其它三维软件中设置好的双面材质导入到unity3d中就失去了效果,不过我们可以通过自定义材质来在unity3d中实现双面材质的效果.步骤如下:1.在资源库中新建一新shader:代码如下: Shad ...

  8. puppet前端管理工具foreman-proxy bind 127.0.0.1:8443问题解决

    最近在玩foreman,发现部署foreman-proxy的时候,总是出现8443bind在127.0.0.1端口,导致无法访问的情况. 如下图: 经过strace -o log.txt bin/sm ...

  9. 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条

    目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...

  10. Linux编程里getopt_long_only函数用法详解

    在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...