Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password
题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间。输出t,不存在则输出Just a legend。
思路:利用KMP算法处理出next数组,由next数组的意义可以知道i为尾的最长相同前缀后缀。则ne[n-1],ne[ne[n-1]],ne[ne[ne[n-1]]]......均为可能的解,且长度递减,为了高效的逐一验证,则需要预处理出整个字符串有多少个与该前缀相同的子串。用到DP思想,cnt[next[i]]+=cnt[i],倒着更新一遍,即可处理出上述信息。那么cnt大等于3的前缀,均为可行解
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=1e6+10,mod=1e9+7,INF=0x3f3f3f3f;
char s[maxn];
int ne[maxn],cnt[maxn];
void init()
{
ne[0]=-1;
for (int i=1;s[i];++i)
{
int j=ne[i-1];
while (s[j+1]!=s[i]&&j>=0)
j=ne[j];
if (s[j+1]==s[i])
ne[i]=j+1;
else
ne[i]=-1;
}
}
int main()
{
scanf("%s",s);
init();
int n=strlen(s);
for (int i=0;i<n;++i)
cnt[i]=1;
for (int i=n-1;i;--i)
cnt[ne[i]]+=cnt[i];
for (int i=ne[n-1];i!=-1;i=ne[i])
{
if (cnt[i]>=3)
{
for (int j=0;j<=i;++j)
printf("%c",s[j]);
return 0;
}
}
printf("Just a legend");
return 0;
}
Codeforces 126B. Password(KMP,DP)的更多相关文章
- HDU-3366-Count the string(KMP,DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)
先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...
- Codeforces A. Password(KMP的nxt跳转表)
题目描述: Password time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 1129D - Isolation(分块优化 dp)
Codeforces 题目传送门 & 洛谷题目传送门 又独立切了道 *2900( 首先考虑 \(dp\),\(dp_i\) 表示以 \(i\) 为结尾的划分的方式,那么显然有转移 \(dp_i ...
- [HIHO1300]展胜地的鲤鱼旗(栈,dp)
题目链接:http://hihocoder.com/problemset/problem/1300 给一个字符串,只包含'('和')',问存在多少个子串似的括号是匹配的. 匹配规则在题干中描(蒻)述( ...
- [NOI2014]动物园(KMP,字符串)
半年前看这题还感觉很神仙,做不动(没看题解). 现在过来看发现……这tm就是一个sb题…… 首先题面已经提示我们用 KMP 了.那 KMP 究竟能干啥呢? 看 $num$ 的定义.发现对于前缀 $i$ ...
- CodeForces–833B--The Bakery(线段树&&DP)
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 牛客练习赛24题解(搜索,DP)
A题,C题不讲,基础题(但是我要抨击一下这次比赛,卡cin,cout,卡的太狠了,根本就不让过的那种,QAQ) 链接:https://www.nowcoder.com/acm/contest/157/ ...
随机推荐
- XSS跨站攻击靶场-通关笔记
XSS攻击是Web攻击中最常见的攻击手法之一,XSS中文名跨站脚本攻击,该攻击是指攻击者在网页中嵌入恶意的客户端脚本,通常是使用JS编写的恶意代码,当正常用户访问被嵌入代码的页面时,恶意代码将会在用户 ...
- 由[].slice.call()引发的思考
由[].slice.call()引发的思考 经常看到大家用[].slice.call()或者Array.prototype.slice.call(): 我一直是一知半解的,今天算是基本弄清楚了, ...
- 3-MySQL DBA笔记-开发基础
第二部分 开发篇 本篇首先讲述数据库开发的一些基础知识,如关系数据模型.常用的SQL语法.范式.索引.事务等,然后介绍编程开发将会涉及的数据库的一些技巧,最后结合生产实际,提供一份开发规范供大家参考. ...
- Myatis中的OGNL和bind标签的结合用法
1.MyBatis常用的OGNL e1 or e2 e1 and e2 e1 == e2,e1 eq e2 e1 != e2,e1 neq e2 e1 lt e2:小于 e1 lte e2:小于等于, ...
- VBA精彩代码分享-3
在开发VBA程序中,我们可能会需要用代码处理VBA工程,包括启用VBA工程访问,启用所有宏,动态插入代码,动态删除代码,动态添加引用和自动创建模块等等,本次的分享内容便以这些为主. 启用VBA工程访问 ...
- DPDK latencystats库使用方案
初始化 注意务必调用 rte_metrics_init /* init latency stats */ /* @TODO should we remove this in product env? ...
- SPOJ-MobileService--线性DP
题目链接 https://www.luogu.org/problemnew/show/SP703 方法一 分析 很显然可以用一个四维的状态\(f[n][a][b][c]\)表示完成第i个任务时且三人 ...
- [转载]什么是白化(whitening)?
[转载]什么是白化(whitening)? 来源:https://blog.csdn.net/hjimce/article/details/50864602 白化whitening 原文地址:http ...
- 买条Vineyard Vines裙子为啥子那么难?因为能遮胖?因为英国王子穿过?
为了这件vineyard vines, 我周六冒雨,去斯坦福shopping center说卖完了,我冒雨赶回家,上网买到了,今天早上发email说没货了,自动取消我的订单.我下午又打了40分钟电话给 ...
- WeakReference 与 SoftReference 区别
装载自:http://flyneil.iteye.com/blog/1345177 WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. Wea ...