Test instructions: give you a string, ask you is both its prefix, and its suffix, and is in the middle out of the longest string is what
Problem-solving ideas: KMP deformation, not familiar with next write out still a little difficult
Problem Solving Code:
1 //File name:126b.cpp2 //Author:darkdream3 //Created time:2015 March 07 Saturday 15:23 51 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - - using namespacestd; - Charstr[1000005]; - intdp[1000005]; in intEs61000005]; - voidGetNextChar*p,intnext[]) to { + intPlen =strlen (p); -next[0] = -1; the intK =-1; * intj =1; $ while(J <Plen)Panax Notoginseng { - if(k = =-1|| P[J] = = p[k+1]) the { + if(P[j] = = p[k+1]) A { theNEXT[J] = k +1; +K + +; -J + + ; $}Else{ $NEXT[J] =-1; -J + +; - } the } - Else {WuyiK =Next[k]; the } - } Wu } - intMain () { Aboutscanf"%s", str); $ GetNext (STR,DP); - intLen =strlen (str); - for(inti =0; i < Len; i++) - { A /*if (dp[i] = = 0 && Str[i] = = str[0]) + { the - } $ Else the dp[i]--; the *///printf ("%d", Dp[i]); the if(Dp[i]! =-1) theHs[dp[i]] + +; - } in if(dp[len-1] != -1) thehs[dp[len-1]] -- ; the while(hs[dp[len-1]] ==0&& dp[len-1] != -1) About { the //printf ("dp[len-1] =%d\n", dp[len-1]); thedp[len-1] = dp[dp[len-1]]; the } + //printf ("ans =%d\n", dp[len-1]); - intan =0 ; the for(inti = len-2; I >=1; I--)Bayi if(Dp[i] = = Dp[len-1]) the { thean =1 ; - } - if(An = =0|| Dp[len-1] == -1|| (dp[len-1] ==0&& str[len-1]! = str[0])) the { theprintf"Just a legend\n"); the return 0 ; the } - for(inti =0; I <= dp[len-1];i + +) theprintf"%c", Str[i]); the the return 0;94}View Code
Codeforces 126B Password