CF126B Password
思路:
kmp略作修改。
实现:
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = ;
int neXt[MAXN]; void getNext(string s)
{
int n = s.length();
neXt[] = -;
int k = -, j = ;
while (j < n)
{
if (k == - || s[j] == s[k])
{
j++; k++;
neXt[j] = k;
}
else
{
k = neXt[k];
}
}
} bool kmp(string s, string p)
{
int i = , j = ;
int m = s.length(), n = p.length();
while (i < m && j < n)
{
if (j == - || s[i] == p[j]) i++, j++;
else j = neXt[j];
}
if (j == n) return true;
return false;
} int main()
{
string str;
cin >> str;
str += ' ';
int n = str.length();
getNext(str);
int tmp = neXt[n - ];
bool flg = false;
while (tmp)
{
if (kmp(str.substr(, n - ), str.substr(, tmp)))
{
cout << str.substr(, tmp) << endl;
flg = true;
break;
}
tmp = neXt[tmp];
}
if (!flg) cout << "Just a legend" << endl;
return ;
}
CF126B Password的更多相关文章
- KMP CF126B Password
Description Asterix,Obelix和他们的临时伙伴Suffix.Prefix已经最终找到了和谐寺.然而和谐寺大门紧闭,就连Obelix的运气也没好到能打开它. 不久他们发现了一个字符 ...
- CF126B password&&HDU 4763 Theme Section
http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...
- CF126B Password【KMP】By cellur925
题目传送门 其实$Chemist$在之前写了非常棒的题解! 我长话短说,补充两句. “那么当$next[n]$>$max$时显然不能将$next[n]$作为最长子串的长度”这句话其实在说,因为一 ...
- CF126B
CF126B Password 题意: 给出一个字符串 H,找一个最长的字符串 h,使得它既作为前缀出现过.又作为后缀出现过.还作为中间的子串出现过. 解法: 沿着 $ next_n $ 枚举字符串, ...
- 浅谈KMP算法——Chemist
很久以前就学过KMP,不过一直没有深入理解只是背代码,今天总结一下KMP算法来加深印象. 一.KMP算法介绍 KMP解决的问题:给你两个字符串A和B(|A|=n,|B|=m,n>m),询问一个字 ...
- 2021.08.30 前缀函数和KMP
2021.08.30 前缀函数和KMP KMP算法详解-彻底清楚了(转载+部分原创) - sofu6 - 博客园 (cnblogs.com) KMP算法next数组的一种理解思路 - 挠到头秃 - 博 ...
- 「CF126B」Password
题目描述 给定一个字符串 \(S\),我们规定一个字符串 \(P\) 是可行解,\(P\) 需要满足: \(P\) 是 \(S\) 的前缀 \(P\) 是 \(S\) 的后缀 \(P\) 出现在 \( ...
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
- your password has expired.to log in you must change it
今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...
随机推荐
- Codeforces 263B. Appleman and Card Game
B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #232 (Div. 2) On Sum of Fractions
Let's assume that v(n) is the largest prime number, that does not exceed n; u(n) is the smallest pri ...
- Bellman_ford 算法 Currency Exchange POJ1860
Bellman_ford算法用于寻找正环或者负环! 算法导论: 24.1 The Bellman-Ford algorithm The Bellman-Ford algorithm solves th ...
- Java AOP 获取HttpSevletRequest、HttpSevletResponse、HttpSession对象
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) ((ServletRequestAttributes) ...
- Ext.data.JsonStore的使用
最近在维护一个Ext.js写的贷前服务系统,Ext.data.JsonStore相当于前台页面的一个数据仓库,负责保存后台传过来的Json数据,具体用法如下: var store12=new Ext. ...
- Ubuntu下常规方法安装软件
一.通过apt-get 搜索: #搜索 apt-cache searche 7zip 安装: #安装 sudo apt-get install 7zip 更新: #查看特定软件的版本,前提是要安装ap ...
- SiteMesh2-sitemesh.xml的ConfigDecoratorMapper映射器的用法
继上一次的示例工程http://www.cnblogs.com/EasonJim/p/7083165.html,使用的就是ConfigDecoratorMapper映射器,通过指定目录下的页面,都同一 ...
- MyBatis3-缓存使用
一级缓存和二级缓存的区别: 1.一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为同一个SqlSession,当Session flush或close之后,该Sessi ...
- GNS3和Cisco IOU搭建路由交换实验-IOU篇
http://www.mamicode.com/info-detail-605879.html
- angularjs 过滤多组数据
<html> <head> <script src="angular.min.js"></script> <script ty ...