Poj 3294 life forms (suffix array)

Source: Internet
Author: User

Http://poj.org/problem? Id = 3294

The maximum length must be found first, and then the output must be determined based on the length. If the length is the same, use tags to avoid duplication.

Code:

# Include <cstdio> // The longest public substring.
# Include <cstring>
# Define max (A, B) A> B? A: B
Const int maxn = 101001;
Int wa [maxn], WB [maxn], WV [maxn], WS [maxn], rank [maxn], height [maxn], sa [maxn], s [maxn], Loc [maxn];
Bool vis [1001];
Char STR [1001], ANS [1001];
Int N;
Int CMP (int * r, int A, int B, int L ){
Return R [a] = R [B] & R [A + L] = R [B + L];
}
Int ABS (int ){
Return a <0? -A:;
}
Void da (int * r, int N, int m ){
Int I, j, P, * x = wa, * Y = WB, * t;
For (I = 0; I <m; I ++) WS [I] = 0;
For (I = 0; I <n; I ++) WS [x [I] = R [I] ++;
For (I = 1; I <m; I ++) WS [I] + = ws [I-1];
For (I = n-1; I> = 0; I --) SA [-- ws [x [I] = I;
For (j = 1, P = 1; P <n; j * = 2, M = P ){
For (P = 0, 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 <n; I ++) WV [I] = x [Y [I];
For (I = 0; I <m; I ++) WS [I] = 0;
For (I = 0; I <n; I ++) WS [wv [I] ++;
For (I = 1; I <m; I ++) WS [I] + = ws [I-1];
For (I = n-1; I> = 0; I --) SA [-- ws [wv [I] = Y [I];
For (t = x, x = Y, y = T, P = 1, x [SA [0] = 0, I = 1; I <n; I ++)
X [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++;
}
Return;
}
Void calheight (int * r, int N ){
Int I, j, k = 0;
For (I = 1; I <= N; I ++) rank [SA [I] = I; // obtain the rank value O (N)
For (I = 0; I <n; height [rank [I ++] = K)
For (K? K --: 0, j = sa [rank [I]-1]; R [I + k] = R [J + k]; k ++ );
Return;
}
Int check (INT mid, int Len ){
Int COUNT = 0;
Memset (VIS, false, sizeof (VIS ));
For (INT I = 2; I <= Len; I ++ ){
If (height [I] <mid ){
Memset (VIS, false, sizeof (VIS ));
Count = 0;
Continue;
}
If (! Vis [loc [SA [I-1]) vis [loc [SA [I-1] = true, Count ++;
If (! Vis [loc [SA [I]) vis [loc [SA [I] = true, Count ++;
If (count> n/2) return 1;
}
Return 0;
}
Void print (INT mid, int Len ){
Int COUNT = 0, tag = 0;
Memset (VIS, false, sizeof (VIS ));
For (INT I = 2; I <= Len; I ++ ){
If (height [I] <mid ){
Memset (VIS, false, sizeof (VIS ));
Count = 0;
Tag = 0;
Continue;
}
If (! Vis [loc [SA [I-1]) vis [loc [SA [I-1] = true, Count ++;
If (! Vis [loc [SA [I]) vis [loc [SA [I] = true, Count ++;
If (count> n/2 &&! Tag ){
For (Int J = 0; j <mid; j ++)
Ans [J] = s [SA [I] + J] + 'a'-1;
Ans [Mid] = '\ 0 ';
Printf ("% s \ n", ANS );
Tag = 1;
}
}
}
Int main (){
Int I, j, M, Max, T, flag, X, temp, min;
While (~ Scanf ("% d", & N ){
Flag = 0;
M = 0;
Min = maxn;
Temp = 30;
For (I = 0; I <n; I ++ ){
Scanf ("% s", STR );
If (strlen (STR) <min) min = strlen (STR );
For (j = 0; STR [J]; j ++ ){
S [m] = STR [J]-'A' + 1;
Loc [M ++] = I;
}
S [m] = temp;
Loc [M ++] = temp ++;
}
S [m] = 0;
Da (S, m + 1, temp );
Calheight (S, M );
Int left = 0, Right = min, mid;
While (Right> = left ){
Mid = (right + Left)/2;
If (check (MID, m )){
Left = Mid + 1;
Flag = mid;
}
Else
Right = mid-1;
}
If (FLAG ){
Print (flag, M );
Printf ("\ n ");
}
Else
Printf ("? \ N ");
}
Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.