Codeforces(Round #93) 126 B. Password
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.
A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s.
Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end.
Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened.
You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend.
You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.
Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes.
input
fixprefixsuffix
output
fix
input
abcdabc
output
Just a legend
题目大意:
给你一个字符串,让你在里面找到一个最长的公共的前缀后缀,并且在 字符串中间也出现过一次的子串。
解题思路:
这个题KMP的next数组的理解还是要有的,next[i]表示在i之前,最长的 公共前缀后缀的长度。
所以说,我们首先要看看是否存在公共前缀后缀, 如果有,这只是保证了可能有解,因为我们还要看中间是否出现过,
这个时候,我们让i=next[i],继续看这个next[i]是否出现过,为什么呢? 因为你此往前移动是,就相当于产生了一个可能的答案,
但是我们需要中间 也出现过,所以就要判断这个next[i]值是否出现过,当出现过的就是答案。
#include <stdio.h>
#include <string.h> char s[];
int next_[],vis[]; void getnext() // 得到next数组
{
int i = , j = -;
int len = strlen(s);
next_[] = -;
while (i < len){
if (j == - || s[i] == s[j])
next_[++ i] = ++ j;
else
j = next_[j];
}
} int main ()
{
int i;
while (~scanf("%s",s)){
int len = strlen(s);
getnext();
memset(vis,,sizeof(vis));
for (i = ; i < len; i ++) //将各个位置的最长前后缀标记
vis[next_[i]] = ; int j = len,flag = ;
while (next_[j]>){
if (vis[next_[j]]){
for (i = ; i < next_[j]; i ++)
printf("%c",s[i]);
printf("\n");
flag = ; break;
}
j = next_[j];
}
if (!flag)
printf("Just a legend\n");
}
return ;
}
Codeforces(Round #93) 126 B. Password的更多相关文章
- Educational Codeforces Round 93 (Rated for Div. 2)题解
A. Bad Triangle 题目:https://codeforces.com/contest/1398/problem/A 题解:一道计算几何题,只要观察数组的第1,2,n个,判断他们能否构成三 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #370 - #379 (Div. 2)
题意: 思路: Codeforces Round #370(Solved: 4 out of 5) A - Memory and Crow 题意:有一个序列,然后对每一个进行ai = bi - bi ...
随机推荐
- 分享到QQ空间和新浪微博功能
分享到QQ空间 http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://campus.51job.com/cmbnt ...
- Python开发转盘小游戏
Python开发转盘小游戏 Python 一 原理分析 Python开发一个图形界面 有12个选项和2个功能键 确定每个按钮的位置 每个按钮的间隔相同 点击开始时转动,当前选项的背景颜色为红色,其他 ...
- 高阶篇:4.1.2.3)产品零件级别的QFDII
本章目的:介绍产品零件级别的QFDII编写方法. 1.前言 这章接前面部件级别的QFDII. 产品零件级别的QFDII,其实就是将零件QFDII所得到的设计要求,进一步分配零件的特征(Part Cha ...
- Bi-shoe and Phi-shoe(欧拉筛)
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...
- Pycharm与github的秘密
GIT介绍 GIT文章请看老男孩教育-银角大王的博客: http://www.cnblogs.com/wupeiqi/articles/7295372.html Git 是一个开源的分布式版本控制软件 ...
- Dynamics CRM 365常用js记录。
var entityname =window.parent.Xrm.Page.data.entity.getEntityName();//获取实体名称 var sampid = window.pare ...
- 【python】Scrapy爬虫框架入门
说明: 本文主要学习Scrapy框架入门,介绍如何使用Scrapy框架爬取页面信息. 项目案例:爬取腾讯招聘页面 https://hr.tencent.com/position.php?&st ...
- 4:Median of Two Sorted Arrays
here are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- spark第八篇:与Phoenix整合
spark sql可以与hbase交互,比如说通过jdbc,但是实际使用时,一般是利用phoenix操作hbase.此时,需要在项目中引入phoenix-core-4.10.0-HBase-1.2.j ...
- 文件IO(存取.txt文件)
//存文件方法 public void Save_File_Info(string Save_Path) { //根据路径,创建文件和数据流 FileStream FS = new FileStrea ...