Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1403
Problem Descriptiongiven, the length of the longest Common Substring of them, strings.
For example:
STR1 = Banana
STR2 = Cianaic
So the longest Common Substring are "Ana", and the length is 3.
Inputthe 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.
Outputfor Each test case, you have to tell the length of the longest Common Substring of them.
Sample Input
Bananacianaic
Sample Output
3
Test instructions
Find the longest common substring length of two strings!
The code is as follows:
#include <cstdio> #include <cstring>const int N = 100017*2;int Wa[n], wb[n], wv[n], Ws[n];int rank[n]; Rank array int height[n]; Rank the longest common prefix of the adjacent two suffixes Char str[n];int s[n], sa[n]; The SA is a suffix array, and n suffixes are sorted from small to large to the beginning of the ordered suffix int Max (int a, int b) {return a > B? a:b;} int Min (int a, int b) {return a < b? A:b;} int cmp (int *r, int A, int b, int l) {return r[a]==r[b] && r[a+l]==r[b+l];} The parameter n of the GET_SA function represents the number of characters in the string, where n is the parameter m of the 0//get_sa function that is added to the end of the string, which is the value range of the characters in the string, and is a parameter of the radix sort, or if the original sequence is a letter can be directly taken 128,/ /If the original sequence itself is an integer, M can take a value 1 larger than the largest integer. void Get_sa (int *r, int *sa, int n, int m)//multiplication algorithm {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; Sorts a string of length 1 for (p=1,j=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; Second keyword sort result 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]; First keyword sort 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++; Update rank array} return; void Get_height (int *r, int *sa, int n)//height array {int I, j, k=0; for (I=1; i<=n; i++) rank[sa[i]]=i; 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 main () {while (~SCANF ("%s", str)) {int len = strlen (str); Str[len] = ' ~ '; scanf ("%s", str+len+1); int LEN = strlen (str); for (int i = 0; i < LEN; i++) {s[i] = str[i]-' a ' +1; } S[len] = 0; Get_sa (s,sa,len,270); Get_height (S,sa,len); int ans = 0; for (int i = 2; i < LEN; i++) {if (Height[i] > ans) { if ((Sa[i-1]<len&&sa[i]>len) | | | (Sa[i-1]>len&&sa[i]<len)) ans = height[i]; }} printf ("%d\n", ans); } return 0;}
HDU 1403 longest Common Substring (suffix array ah, the longest common substring template problem)