Problem Description
Given, strings, you had to tell the length of the longest Common Substring of them.
For example:
STR1 = Banana
STR2 = Cianaic
So the longest Common Substring are "Ana", and the length is 3.
Input
The input contains several test cases. Each of the test case contains the strings, each string would have the at most 100000 characters. All the characters is in lower-case.
Process to the end of file.
Output
For the all test case, the length of the longest Common Substring of them.
Sample Input
Banana
Cianaic
Sample Output
3
Author
Ignatius.l
Recommend
We have carefully selected several similar problems for you:1404 1418 1422 1509 1409
Statistic | Submit | Discuss | Note
The 2 strings are joined together and the suffix array is obtained, and the maximum value of a height[i] is obtained, and the 2 suffixes are in 2 strings.
/************************************************************************* > File Name:hdu1403.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 April 09 Thursday 14:30 20 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[220000];classsuffixarray{ Public:Static Const intN =220000;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];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]); }voidSolve () {intAns =0; for(inti =1; i < size; ++i) {if(Pos[sa[i]] = =1&& Pos[sa[i +1]] ==2) {if(Ans < height[i]) {ans = height[i]; } }Else if(Pos[sa[i]] = =2&& Pos[sa[i +1]] ==1) {if(Ans < height[i]) {ans = height[i]; } } }printf("%d\n", ans); }}sa;Charstr[100110];intMain () { while(~scanf('%s ', str)) {sa.clear ();intCNT =0;intLen =strlen(str); for(inti =0; i < Len; ++i) {Sa.insert (Str[i]-' A '+1); pos[cnt++] =1; } pos[cnt++] =0; Sa.insert ( -);scanf('%s ', str); Len =strlen(str); for(inti =0; i < Len; ++i) {Sa.insert (Str[i]-' A '+1); pos[cnt++] =2; } Sa.getsa ( -); Sa.getheight (); Sa.solve (); }return 0;}
hdu1403---longest Common Substring (suffix array to find the longest common substring of 2 strings)