SPOJ220---Relevant phrases of annihilation (suffix array + binary, group suffix)

Source: Internet
Author: User

You are the King of Byteland. Your agents has just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on Your are Land. You immedietaly send to the Bytelandian cryptographer, but he's currently busy eating popcorn and claims that he may onl Y decrypt the most important part of the text (since the rest would is a waste of his time). You decide to select the fragment of the text which the enemy have strongly emphasised, evidently regarding it as the most Important. So, you is looking for a fragment of text which appears in all the messages disjointly at least twice. Since you is not overfond of the cryptographer, try and make this fragment as long as possible.
Input

The first line of input contains a single positive integer t<=10, the number of test cases. T test cases follow. Each test case is begins with an integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters ' a '-' Z ', possibly with some addit ional trailing white space which should be ignored.
Output

For each test case output, the length of longest string which appears disjointly at least twice in all of the messages.
Example

Input:
1
4
Abbabba
Dabddkababa
Bacaba
Baba

Output:
2

(in the example above, the longest substring which fulfills the requirements is ' BA ')

Simple suffix array application, first or the old routine, the string are connected together, in the middle with not appearing and different characters separated, to find the suffix array, and then two answers, the suffix group, with 3 arrays, the first record each group of suffixes, each string in the number of suffixes appear, The remaining two are the starting positions of the most forward and trailing suffixes in each string.

/************************************************************************* > File name:spoj-220.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 07 Tuesday 21:21 41 seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intpos[122000];classsuffixarray{ Public:Static Const intN =122000;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intcnt[ -];intmaxs[ -];intmins[ -];intLog[n];intdp[n][ -];intSizevoidClear () {size =0; }voidInsertintN) {init[size++] = n; }BOOLcmpint(RNintAintBintL) {return(R[a] = = R[b] && r[a + l] = = R[b + L]); }voidGetsa (intm = the)//m is generally the maximum value of +1{Init[size] =0;intL, p, *x = x, *y = y, n = size +1; for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;            ++i) {++buc[x[i] = init[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;            -i) {sa[--buc[x[i]] = i; } for(L =1, p =1; L <= N && p < n; m = p, l *=2) {p =0; for(inti = n-l; I < n;                ++i) {y[p++] = i; } for(inti =0; I < n; ++i) {if(Sa[i] >= L)                    {y[p++] = sa[i]-l; }                } for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;                ++i) {++buc[x[y[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;                -i) {sa[--buc[x[y[i]]] = y[i]; }intI for(Swap (x, y), x[sa[0]] =0, p =1, i =1; I < n; ++i) {X[sa[i]] = cmp (y, Sa[i-1], Sa[i], L)? P-1: p++; }            }        }voidGetHeight () {inth =0, n = size; for(inti =0; I <= N;            ++i) {Rank[sa[i]] = i; } height[0] =0; for(inti =0; I < n; ++i) {if(H >0) {--h; }intJ =sa[rank[i]-1]; for(; i + H < n && j + H < n && init[i + h] = = Init[j + h]; ++h); Height[rank[i]-1] = h; }        }//preprocessing logarithm of each number for RMQ, constant optimization        voidInitlog () {log[0] = -1; for(inti =1; i < N; ++i) {Log[i] = (I & (i-1)) ? Log[i-1]: Log[i-1] +1; }        }voidINITRMQ () {initlog ();intn = size;intLimit for(inti =0; I < n; ++i) {dp[i][0] = Height[i]; } for(intj =1; J <= Log[n]; ++J) {limit = (N-(1<< j)); for(inti =0; I <= limit; ++i) {Dp[i][j] = min (Dp[i][j-1], Dp[i + (1<< (J-1))][j-1]); }            }        }intLCP (intAintb) {intT            A = Rank[a]; b = Rank[b];if(A > B)            {Swap (A, b);            }--b; t = log[b-a +1];returnMin (dp[a][t], dp[b-(1<< T) +1][t]); }BOOLCheckintKintN) {memset(CNT,0,sizeof(CNT));memset(Maxs,-1,sizeof(MAXS));memset(MINS, INF,sizeof(MINS)); cnt[pos[sa[1]]] =1; maxs[pos[sa[1]] = mins[pos[sa[1]] = sa[1]; for(inti =1; i < size; ++i) {if(Height[i] >= k) {++cnt[pos[sa[i +1]]]; Maxs[pos[sa[i +1]] = max (maxs[pos[sa[i +1]], Sa[i +1]); Mins[pos[sa[i +1]] = min (mins[pos[sa[i +1]], Sa[i +1]); }Else{BOOLFlag =1; for(intj =1; J <= N; ++J) {if(Cnt[j] <2) {flag =0; Break; }Else if(Maxs[j]-mins[j] < K) {flag =0; Break; }                    }if(flag) {return 1; }memset(CNT,0,sizeof(CNT));memset(Maxs,-1,sizeof(MAXS));memset(MINS, INF,sizeof(MINS)); Cnt[pos[sa[i +1]]] =1; Maxs[pos[sa[i +1]] = Mins[pos[sa[i +1]] = Sa[i +1]; }            }return 0; }voidSolveintN) {intL =1, r = size, Mid, ans =0; while(L <= R) {mid = (L + r) >>1;if(Check (Mid, N))                    {ans = mid; L = mid +1; }Else{R = mid-1; }            }printf("%d\n", ans); }}sa;Charbuf[10010];intMain () {intTscanf("%d", &t); while(t--) {intNscanf("%d", &n); Sa.clear ();intLen, maxs =0, cnt =0; for(inti =1; I <= N; ++i) {scanf('%s ', buf); Len =strlen(BUF); for(intj =0; J < Len; ++J) {maxs = max (Maxs, (int) buf[j]); Sa.insert ((int) buf[j]);            pos[cnt++] = i; } Sa.insert ((int)' Z '+ i); pos[cnt++] =0; MAXS = Max (Maxs, (int)' Z '+ i); } Sa.getsa (Maxs +1);        Sa.getheight ();    Sa.solve (n); }return 0;}

SPOJ220---Relevant phrases of annihilation (suffix array + binary, group suffix)

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.