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. POJ 3349 Snowflake Snow Snowflakes (哈希表)

    题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...

  2. python-多线程(一)

    一.Python中的线程使用: Python中使用线程有两种方式:函数或者用类来包装线程对象. 1.  函数式:调用thread模块中的start_new_thread()函数来产生新线程.如下例: ...

  3. [原]NYOJ-公约数和公倍数 -40

    大学生程序代写 //http://acm.nyist.net/JudgeOnline/problem.php?pid=40 公约数和公倍数 时间限制:1000 ms  |  内存限制:65535 KB ...

  4. luogu1353 Running

    dp[i][j]表示走i分钟疲劳值为j时的最远距离 然后搞一下就好啦 #include <iostream> #include <cstdio> #include <al ...

  5. URAL1517Freedom of Choice(后缀数组)

    Background Before Albanian people could bear with the freedom of speech (this story is fully describ ...

  6. sqlite表结构动态读取工具(Chole ORM框架)

    Chole ORM框架 sqlIte于嵌入式数据库读取比较有利,不需要安装office也可以进行,可以在服务器系统当中使用. 所以我开发了这款工具,然后就是为了动态的读取表结构,然后根据表结构加载所有 ...

  7. Access中一句查询代码实现Excel数据导入导出

    摘 要:用一句查询代码,写到vba中实现Excel数据导入导出,也可把引号中的SQL语句直接放到查询分析器中执行正 文: 导入数据(导入数据时第一行必须是字段名): DoCmd.RunSQL &quo ...

  8. HDU1114(完全背包装满问题)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. IDEA常用快捷键整理

    快速定位文件:   Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Alt+Shift+C,最近打开的文件 Ctrl+N,快速打开类 Ctrl+Shift+N,快速打开文件   当 ...

  10. 二 Flask快速入门

    1: 外部可访问的服务器: 如果你运行了这个服务器,你会发现它只能从你自己的计算机上访问,网络中其它任何的地方都不能访问.在调试模式下,用户可以在你的计算机上执行任意 Python 代码.因此,这个行 ...