Time Limit: 4000MS |
|
Memory Limit: 131072K |
Total Submissions: 24756 |
|
Accepted: 10130 |
Case Time Limit: 1000MS |
Description
The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days:his mother is getting ill. Being worried about spending so much in railway tickets (Byterland is such a big country, and he have to spend all shours on Train to him hometown), he decided only to send SMS with his mother.
The little cat lives in a Unrich family, so he frequently comes to the Mobile service center, to check how much money he Have spent on SMS. Yesterday, the computer of service center was broken, and printed, very long messages. The brilliant little cat soon found out:
1. All characters in messages is lowercase Latin letters, without punctuations and spaces.
2. All SMS have been appended other– (i+1)-th SMS comes directly after the i-th one–that are why those Es is quite long.
3. His own SMS have been appended together, but possibly a great many redundancy characters appear leftwards and rightwards Due to the broken computer.
E.g:if his SMS was "motheriloveyou", either long message printed by, the would possibly be one of "Hahamotherilov Eyou "," motheriloveyoureally "," Motheriloveyouornot "," bbbmotheriloveyouaaa ", etc.
4. For these broken issues, the little cat have printed his original text twice (so there appears, very long messages). Even though the original text remains the same in both printed messages, the redundancy characters on both sides would is P Ossibly different.
You are given those-very long messages, and you had to output the length of the longest possible original text Writte n by the little cat.
Background:
The SMS in Byterland Mobile service is charging in Dollars-per-byte. That's why the little cat was worrying about how long could the longest original text being.
Why do I ask you to write a program? There is four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother Seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade he mother to see the Doctor:(
Input
The strings with lowercase letters on the input lines individually. Number of characters in each one would never exceed 100000.
Output
A number–what is the maximum length of the original text written by the little cat.
Sample Input
Yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother
Sample Output
27
Source
POJ Monthly--2006.03.26,zeyuan Zhu, "Dedicate to my great beloved mother." Thinking: Again, the application of the Heigh array, we link two strings, set the length of the first string is LS, Will connect the string s to do a suffix array, and then is a two-point answer m, by scanning through the Heigh array to determine whether M is legal, will heigh by greater than equal grouping, if the group's smallest SA value mi with the largest SA value mx satisfied: Mi + M < ls && mx > ls is m-value valid (same as hdu3518)
#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intINF =0x3f3f3f3f;Const intMAXN =1000000+Ten;intT1[MAXN], T2[MAXN], C[MAXN];BOOLcmpint*r,intAintBintl) {returnR[a] = = R[b] && r[a + l] = = R[b +l];}voidDaCharStr[],intSa[],intRank[],intHeigh[],intNintm) {N++; intI, J, p, *x = t1, *y =T2; for(i =0; I < m; ++i) C[i] =0; for(i =0; I < n; ++i) c[x[i] = str[i]]++; for(inti =1; I < m; ++i) C[i] + = c[i-1]; for(inti = n-1; I >=0; -i) sa[--c[x[i]] =i; for(intj =1; J <= N; J <<=1) {p=0; for(i = n-j; i < n; ++i) y[p++] =i; for(i =0; I < n; ++i)if(Sa[i] >= j) y[p++] = Sa[i]-J; for(i =0; I < m; ++i) C[i] =0; for(i =0; I < n; ++i) c[x[y[i]]]++; for(i =1; I < m; ++i) C[i] + = c[i-1]; for(i = n-1; I >=0; -i) sa[--c[x[y[i] []] =Y[i]; Swap (x, y); P=1; x[sa[0] ] =0; for(i =1; I < n; ++i) x[Sa[i]]= CMP (y, Sa[i-1], Sa[i], j)? P-1: p++; if(P >= N) Break; M=p; } intK =0; N--; for(i =0; I <= N; ++i) rank[Sa[i]] =i; for(i =0; I < n; ++i) {if(k) k--; J= Sa[rank[i]-1]; while(Str[i + K] = = Str[j + K]) k++; heigh[Rank[i]]=K; }}intRANK[MAXN], HEIGH[MAXN], SA[MAXN];CharS[MAXN], S2[MAXN];void out(intN) {puts ("rank[]"); ///the valid range for the rank array is 0~n-1, and the value is 1~n for(inti =0; I <= N; ++i) printf ("%d", Rank[i]); Puts ("sa[]"); ///the valid range for the SA array is 1~n, and the value is 0~n-1 for(inti =0; I <= N; ++i) printf ("%d", Sa[i]); Puts ("heigh[]"); ///the valid range of the Heigh array is 2~n for(inti =0; I <= N; ++i) printf ("%d", Heigh[i]);}intls;BOOLCheckintXintN) {intMi = INF, mx =-INF; for(inti =2; I <= N; ++i) {if(Heigh[i] >=x) {mi= Min (mi, min (sa[i-1], sa[i]); MX= max (MX, max (Sa[i-1], sa[i]); }Else { if(mi + x < ls && mx >= ls)return true; Mi= INF, mx =-INF; } } if(mi + x < ls && mx > ls)return true; return false;}intSolveintN) {intL =0, R = n +1; while(R-l >1) { intM = (L + R) >>1; if(Check (M, n)) L =M; ElseR =M; } returnL;}intMain () { while(~SCANF ("%s%s", S, S2)) {ls=strlen (s); strcat (s, S2); intn =strlen (s); Da (S, SA, Rank, Heigh, N, -); printf ("%d\n", Solve (n)); } return 0;}
View Code
POJ 2774 Long long Message suffix array base problem