Codeforces A. Password(KMP的nxt跳转表)
题目描述:
Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.
Output
Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes.
Examples
Input
Copy
fixprefixsuffix
Output
Copy
fix
Input
Copy
abcdabc
Output
Copy
Just a legend
思路:
题目的意思是给一个字符串,看是否存在这样一个满足条件的最长字符子串:字串为前缀,后缀和中间的位置出现。刚开始时就把跟字符串的首字母相同的位置全部拿出来,因为只有这些位置可能会有满足条件的子串。然后从前往后,看从该位置出发到末尾的字串是否满足条件1是一种前缀2中间出现过。结果妥妥的在第52个测试点超时(一开始还以为能够过呢)。然后把string类全部改为c字符数组,写了字符串的比较函数,vector改用数组,加了读入优化,输出用puts,甚至吸了O2,还是超时。于是我动起了不好的念头,字符串的比较改用随机函数,只要每次随机的位置都相等,我就认为两个字符串相等,就不用每次线性的时间扫描了。嘿嘿,果然还是在第52个测试点T了。
此路不通,换条路。其实当题目提到前后缀时我就不自觉联想到了KMP算法,因为我隐约记得这是字符串的模式匹配(废话,当然是╭(╯^╰)╮),而且里面有个步骤就是跟前后缀有关的,还是线性时间复杂度,但是我已经不记得了(不,你根本就不知道(* ^ ▽^ *))。其实这道题就和KMP算法中的next表有关。next[i]表示的是模式串第i个位置之前的相同前后缀的最大长度,也是i个位置失效后要跳转的位置。那么我们再求出next后就可以根据它来判断最长相同前后缀是否在中间出现过,不是的话看次长相同前后缀在中间出现过没,再不是看次次长,...,直到跳到字符串外的-1
那么怎么判断是否在中间出现过呢?我们只需要在求出nxt表时同时用一个数组标记出nxt[i]的值是否出现。最后从末尾的末尾(len)位置开始,看nxt[i]值是否已被标记出现,出现就说明中间有这个长度的相同前后缀,没有标记就nxt[nxt[i]]地看次长位置中间标记没有,直到找到标记,记录长度后退出或者都不满足结束。
代码:
#include <iostream>
#include <string>
#define max_n 1000005
using namespace std;
string s;
int len;
int nxt[max_n];
int cnt[max_n];
int length = 0;
int get_nxt()
{
int j = 0;
int k = -1;
nxt[0] = -1;
while(j<len)
{
if(k==-1||s[k]==s[j])
{
++k;
++j;
nxt[j] = k;
}
else
{
k=nxt[k];
}
}
for(int i = 0;i<len;i++)
{
cnt[nxt[i]] = 1;
//cout << nxt[i] << " ";
}
//cout << endl;
}
int main()
{
cin >> s;
len = s.size();
get_nxt();
int k = nxt[len];
int flag = 0;
while(k>0)
{
if(cnt[k])
{
flag = 1;
length = k;
break;
}
k = nxt[k];
}
//cout << "k " << k << endl;
if(flag)
{
for(int i = len-length;i<len;i++)
{
cout << s[i];
}
cout << endl;
}
else
{
cout << "Just a legend" << endl;
}
return 0;
}
Codeforces A. Password(KMP的nxt跳转表)的更多相关文章
- Codeforces 126B. Password (KMP)
<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...
- Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...
- oracle exp imp 导入 正在跳过表 plsql 导入表 成功终止 数据 被导入
http://blog.csdn.net/agileclipse/article/details/12968011 .导入过程中,所有表导入都出现提示, 正在跳过表...某某表名 最后提示成功终止导入 ...
- 词典(一) 跳转表(Skip table)
词典,顾名思义,就是通过关键码来查询的结构.二叉搜索树也可以作为词典,不过各种BST,如AVL树.B-树.红黑树.伸展树,结构和操作比较复杂,而且理论上插入和删除都需要O(logn)的复杂度. 在词典 ...
- CodeForces 126B Password
题目链接:http://codeforces.com/problemset/problem/126/B 题目大意: 多组数据每组给定1个字符串S,问是否存在S的一个尽量长的子串,同时是S的前缀和后缀, ...
- Ubuntu输入password登陆后又跳回到登录界面
现象:在Ubuntu登陆界面输入password之后.黑屏一闪而且出现了check battery state之类的文字之后,又跳转到登录界面. 原因:主文件夹下的.Xauthority文件拥有者变成 ...
- CF #93 div1 B. Password KMP/Z
题目链接:http://codeforces.com/problemset/problem/126/B 大意:给一个字符串,问最长的既是前缀又是后缀又是中缀(这里指在内部出现)的子串. 我自己的做法是 ...
- [CF30E]Tricky and Clever Password(KMP+manacher)
首先枚举回文中心,然后显然中心两边要尽量扩展作为middle,这个用manacher实现. 然后注意到suffix的结尾位置是固定的(串尾),那么预处理出以每个位置结尾的串与原串后缀至多能匹配多长,然 ...
- Codeforces126B - Password(KMP)
题目大意 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 KMP的失配函数f[i]的非零值就是前i个字符的一个最长前缀且也是后缀的字符 ...
随机推荐
- suse12.2构建samba
1:添加用户 useradd wangjunhui -d /home/wangjunhuipasswd wangjunhui 2:配置samba smbpasswd -a wangjunhui vi ...
- spring boot 启动遇到报错:Failed to configure a DataSource
spring boot 启动遇到报错,具体如下 Description: Failed to configure a DataSource: 'url' attribute is not speci ...
- 修改mysql端口后重启mysql报错:Can't start server: Bind on TCP/IP port. Got error...n denied
1:错误信息:如下 [root@host ~]# systemctl status mariadb ● mariadb.service - MariaDB database server Loaded ...
- .NetCore快速搭建ELK分布式日志中心
懒人必备:.NetCore快速搭建ELK分布式日志中心 该篇内容由个人博客点击跳转同步更新!转载请注明出处! 前言 ELK是什么 它是一个分布式日志解决方案,是Logstash.Elastaics ...
- C++ getline()的两种用法
getline():用于读入一整行的数据.在C++中,有两种getline函数.第一种定义在头文件<istream>中,是istream类的成员函数:第二种定义在头文件<string ...
- php扩展模块 opcache安装教程
php扩展模块 opcache安装教程PHP5.5.0以后版本自带Opcache加速器,但默认情况下木有启用.所以编译PHP的时候 我们想要启用该PHP加速器就应该添加参数 : –enable-opc ...
- html5 audio标签切换播放音乐的方法
html5 audio标签切换播放音乐的方法<pre><audio id="music1" preload loop="loop">&l ...
- kafka参数解析+启动参数解析
Kafka参数详解 每个kafka broker中配置文件server.properties默认必须配置的属性如下: broker.id=0 num.network.threads=2 num.io. ...
- python的JSON库
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.json库的使用 使用 JSON 函数需要导入 json 库:import jso ...
- FusionInsight大数据开发---Oozie应用开发
Oozie应用开发 要求: 了解Oozie应用开发适用场景 掌握Oozie应用开发 熟悉并使用Oozie常用API Oozie简介 Oozie是一个Hadoop作业的工作流调度管理系统 Oozie工作 ...