[KMP][字符串Hash] #93 div1 cf 126B Password
来源:互联网 发布:回收站数据恢复 免费 编辑:程序博客网 时间:2024/06/06 05:38
[KMP][Hash] #93 div1 cf 126B Password
@(ACM题目)[KMP,字符串Hash]
Description
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.
Sample
Input
fixprefixsuffix
Output
fix
Input
abcdabc
Output
Just a legend
题目分析
本题给定一个字符串
此题利用KMP中的失配函数,通过可以得到以当前位置的“前缀与后缀相同”的最大长度,见下图:
字符串开始的下标定为1。以最后一个字母d为例,当d失配后,其前方的后缀“ab”是整个字符串的前缀,故失配函数
遍历字符串的每个位置
代码
#include<bits/stdc++.h>typedef long long LL;typedef unsigned long long ull;using namespace std;const int maxn = 1e6+7;const ull base = 163;char s[maxn];ull hah[maxn];ull pw[maxn];int n, lost[maxn];void getFail(char* P){ int m = strlen(P+1); lost[1] = lost[2] = 1; for(int i = 2; i <= m; i++) { int j = lost[i]; while(j > 1 && P[i] != P[j]) j = lost[j]; lost[i+1] = P[i] == P[j] ? j+1 : 1; }}void calcHash(char* s){ pw[0] = 1; hah[0] = 0; for(int i = 1; i <= n; i++) { pw[i] = pw[i-1] * base; hah[i] = hah[i-1] * base + s[i]; }}ull getHash(int l, int r){ return hah[r] - hah[l-1] * pw[r-l+1];}int main(){ scanf("%s", s+1); n = strlen(s+1); calcHash(s); getFail(s); int p = 0; for(int i = 1; i <= n; ++i) { int l = lost[i] - 1; if(l > p && getHash(1, l) == getHash(n-l+1, n)) p = l; } if(p > 0) { s[p+1] = '\0'; puts(s+1); } else puts("Just a legend"); return 0;}
- [KMP][字符串Hash] #93 div1 cf 126B Password
- cf 126B-Password(KMP)
- CF 126B Password (KMP,利用next数组)
- codeforces 126B Password KMP
- Codeforces 126B Password(KMP)
- codeforces 126B password KMP
- CodeForces 126B Password 【kmp】
- CodeForces 126B password KMP
- cf 80 div1 B(graph)
- CodeForces 113B Petr#(KMP + 字符串hash)
- codeforces 126B Password DP KMP
- CodeForces 126B - Password 利用KMP
- codeforces 126B Password (kmp,next应用)
- CF 134 DIV1 B.Blackboard Fibonacci
- cf #345div1 B ----------div2 D
- codeforces 126B B. Password(kmp+dp)
- 字符串hash kmp算法
- Codeforces Beta Round #93 (Div. 1 Only)-B. Password(KMP)
- 蓝牙知识【协议栈简图】 【 UUID解析 】 【属性、Characteris、描述符和Service以及profile的关系】【蓝牙无线通道】【缩写词】
- nginx之php,及在lnmp环境下的论坛
- E-POJ-3087 Shuffle'm Up
- J
- matlab之cell的清空
- [KMP][字符串Hash] #93 div1 cf 126B Password
- #bzoj1515#盖房子(DP经典)
- Oh, my goddess
- Python运维之路——协程、事件驱动与异步IO
- Java中状态模式和策略模式的区别
- 线段树: CDOJ1598-加帕里公园的friends(区间合并,单点更新)
- 山峰 2016暑假集训结训赛 by JueChen 栈的维护
- [日常训练] 秀姿势
- CodeForces