• Source
    1. /**************************************************************
    2.   Problem: 1535
    3.   User: zrts
    4.   Language: C++
    5.   Result: Accepted
    6.   Time:104 ms
    7.   Memory:5288 kb
    8. ****************************************************************/
    9.  
    10. #include<cstdio>
    11. #include<cstring>
    12. #include<algorithm>
    13. //by zrt
    14. //problem:
    15. //无论你在什么时候开始,重要的是开始以后就不要停止。
    16. using namespace std;
    17. typedef long long ll;
    18. const double eps(1e-10);
    19. const int inf(0x7fffffff);
    20. #define N 510000
    21. char s[N];
    22. int nxt[N];
    23. int L[N],l,tot;
    24. bool judge(int x){
    25. int j=nxt[x];
    26. for(int i=x+1;i<l;i++){
    27. while(~j&&s[j+1]!=s[i]) j=nxt[j];
    28. if(s[j+1]==s[i]) j++;
    29. if(!~j) return false;
    30. if(j==x) j=nxt[j];
    31. }
    32. return true;
    33. }
    34. int main(){
    35. #ifdef LOCAL
    36. freopen("in.txt","r",stdin);
    37. freopen("out.txt","w",stdout);
    38. #endif
    39. scanf("%s",s);
    40. l = strlen(s) ;
    41. int j ;
    42. nxt[0]=j=-1;
    43. for(int i=1; i<l; i++){
    44. while(~j&&s[j+1]!=s[i]) j=nxt[j];
    45. if(s[j+1]==s[i]) j++;
    46. nxt[i]=j;
    47. }
    48. for(int i=l-1;~i;i=nxt[i]) L[tot++] = i;
    49. int l=0,r=tot,m ;
    50. while(l+1<r){
    51. m=(l+r)>>1;
    52. if(judge(L[m])){
    53. l=m;
    54. }else r=m;
    55. }
    56. printf("%d\n",L[l]+1);
    57. return 0;
    58. }