CodeForces 126B Password
题目链接:http://codeforces.com/problemset/problem/126/B
题目大意:
多组数据
每组给定1个字符串S,问是否存在S的一个尽量长的子串,同时是S的前缀和后缀,并且在S的中间出现过(即非前缀也非后缀)。
分析:
代码如下:
#include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
const int maxN = 1e5 + ; string s; void KMP_next(string &T, vector< int > &nxt) {
nxt.resize(T.size() + );
nxt[] = -; int t = -, i = ;
while(i < T.size()) {
( > t || T[i] == T[t]) ? nxt[++i] = ++t : t = nxt[t];
}
} int main(){
while(cin >> s) {
string ans = "";
vector< int > nxt;
KMP_next(s, nxt);
unordered_set< int > si;
rep(i, nxt.size() - ) si.insert(nxt[i]); int p = nxt[nxt.size() - ];
while(p > ) {
if(si.find(p) != si.end()) {
ans = s.substr(, p);
break;
}
p = nxt[p];
} if(ans == "") cout << "Just a legend\n";
else cout << ans << endl;
} return ;
}
CodeForces 126B Password的更多相关文章
- Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...
- Codeforces 126B. Password (KMP)
<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...
- Codeforces 126B Password(Z算法)
题意 给定一个字符串 \(s\) ,求一个子串 \(t\) 满足 \(t\) 是 \(s\) 的前缀.后缀且在除前缀后缀之外的地方出现过. \(1 \leq |s| \leq 10^6\) 思路 \( ...
- 【Codeforces 126B】Password
[链接] 我是链接,点我呀:) [题意] 给你一个字符串s 让你从中选出来一个字符串t 这个字符串t是s的前缀和后缀 且在除了前缀和后缀之外的中间部位出现过. 且要求t的长度最长. 让你输出这个字符串 ...
- codeforces 126B
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. ...
- Codeforces A. Password(KMP的nxt跳转表)
题目描述: Password time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 79D - Password(状压 dp+差分转化)
Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...
- Codeforces 126B(kmp)
要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...
- 126B Password[扩展kmp学习]
题目大意 给你一个字符串,求它的一个子串使得这个子串即使前缀又是后缀又出现在不是前缀且不是后缀的地方 分析 扩展kmp就是定义z[i]表示i~n的子串与整个串的最长公共前缀的长度是z[i] 所以这个题 ...
随机推荐
- java 关于打断点
比如:前台传过来参数中文乱码,需要decode才可以使用, 判断问题. debug 在 DispatcherServlet OncePerRequestFilter 打断点, 查看前台过来的中文在哪里 ...
- UVA1152-4 Values whose Sum is 0(分块)
Problem UVA1152-4 Values whose Sum is 0 Accept: 794 Submit: 10087Time Limit: 9000 mSec Problem Desc ...
- UVA11694-Gokigen Naname(DFS进阶)
Problem UVA11694-Gokigen Naname Accept: 76 Submit: 586Time Limit: 10000 mSec Problem Description I ...
- 5、原生jdbc链接数据库实例-自动取款机
ATM自动取款机需求 一.登陆 1.界面要求:服务选择 1.老用户登陆:进入后输入卡号密码登陆 2.新用户开户:开户需要输入身份证号,记录姓名,开户时间.然后机器给出卡号,原始密码:111111. 卡 ...
- flask_socket_io中报错RuntimeError: You need to use the eventlet server. See the Deployment section of the documentation for more information.的解决办法
最新的flask_socketio 使用的是python-socketio 如果在包中安装了gevent或evenlet,在服务器上使用时,就会报错 RuntimeError: You need to ...
- Emacs 中使用中文插件 eim
在 Emacs 中输入中文,一般用 ibus 一类的输入法也能应付,但来回切换有点麻烦,这里介绍国人写的一个好插件 eim. - 安装 由于在 list-packages 中没有找到这个插件,所以不能 ...
- Oracle 迁移 序列
---删除序列select 'drop sequence ' || sequence_owner || '.' || SEQUENCE_NAME || ';'from dba_sequenceswhe ...
- springboot 创建非web项目及数据源简单使用
项目组马上要使用springboot来重构程序,首先要对几个比较小的非web项目重构,所以新手入门,简单做了个小例子 代码结构如下: dao层 package com.mysping.myboot00 ...
- [intoj#7]最短距离
190227模拟 题目描述 给定一张 N 个点的有向图,点 i 到点 j 有一条长度为 i/gcd(i,j) 的边. 有个 Q 询问,每个询问包含两个数 x, y,求从点 x 出发到点 y 的最短距离 ...
- Recurrent Neural Network[Quasi RNN]
0.背景 RNN模型,特别是包含着门控制的如LSTM等模型,近年来成了深度学习解决序列任务的标准结构.RNN层不但可以解决变长输入的问题,还能通过多层堆叠来增加网络的深度,提升表征能力和提升准确度.然 ...