思路:

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

  1. KMP CF126B Password

    Description Asterix,Obelix和他们的临时伙伴Suffix.Prefix已经最终找到了和谐寺.然而和谐寺大门紧闭,就连Obelix的运气也没好到能打开它. 不久他们发现了一个字符 ...

  2. CF126B password&&HDU 4763 Theme Section

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...

  3. CF126B Password【KMP】By cellur925

    题目传送门 其实$Chemist$在之前写了非常棒的题解! 我长话短说,补充两句. “那么当$next[n]$>$max$时显然不能将$next[n]$作为最长子串的长度”这句话其实在说,因为一 ...

  4. CF126B

    CF126B Password 题意: 给出一个字符串 H,找一个最长的字符串 h,使得它既作为前缀出现过.又作为后缀出现过.还作为中间的子串出现过. 解法: 沿着 $ next_n $ 枚举字符串, ...

  5. 浅谈KMP算法——Chemist

    很久以前就学过KMP,不过一直没有深入理解只是背代码,今天总结一下KMP算法来加深印象. 一.KMP算法介绍 KMP解决的问题:给你两个字符串A和B(|A|=n,|B|=m,n>m),询问一个字 ...

  6. 2021.08.30 前缀函数和KMP

    2021.08.30 前缀函数和KMP KMP算法详解-彻底清楚了(转载+部分原创) - sofu6 - 博客园 (cnblogs.com) KMP算法next数组的一种理解思路 - 挠到头秃 - 博 ...

  7. 「CF126B」Password

    题目描述 给定一个字符串 \(S\),我们规定一个字符串 \(P\) 是可行解,\(P\) 需要满足: \(P\) 是 \(S\) 的前缀 \(P\) 是 \(S\) 的后缀 \(P\) 出现在 \( ...

  8. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

  9. your password has expired.to log in you must change it

    今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...

随机推荐

  1. codevs1127 接水问题

    题目描述 Description 学校里有一个水房,水房里一共装有m 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为1. 现在有n 名同学准备接水,他们的初始接水顺序已经确定.将这些同学按接 ...

  2. Extended symmetrical multiprocessor architecture

    An architecture for an extended multiprocessor (XMP) computer system is provided. The XMP computer s ...

  3. F - Piggy-Bank 完全背包问题

    Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. ...

  4. 线程间的通信----wait/notify机制

    wait/notify机制 实现多个线程之间的通信可以使用wait.notify.notifyAll三个方法.这三个方法都是Object类的方法.wait():导致当前线程等待,直到另一个线程调用此对 ...

  5. Filter过滤器机制

    tomcat内部过滤器采用了责任链的设计模式, Tomcat的过滤器主要由Filter.FilterChain组成,FilterChain包含一个Filter数组.当Wrapper执行FilterCh ...

  6. FreeMarker与Spring MVC 4结合错误:Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfiguration

    添加spring-context-support的依赖到POM: <!-- spring-context-support --> <!-- https://mvnrepository ...

  7. XA transaction ,JTA , two phase commit , GTX0-j

    本文主要是对以前一直迷惑的几个名字的学习. xa transaction 简单查了一下,大概就是 distributed  transaction. 相比传统的transaction而言,传统的tra ...

  8. 1.4-动态路由协议OSPF②

    LAB2.通过反掩码控制有哪些接口,在运行OSPF ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   step1:启动OSPF,并宣告网络: R1(config ...

  9. 2015南阳CCPC A - Secrete Master Plan A.

    D. Duff in Beach Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a ...

  10. 1245 最小的N个和(前k小ai+bi)

    1245 最小的N个和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 有两个长度为 N ...