POJ 2774 Long long Message suffix array////title:////the longest common substring of two strings.////:////suffix array. Concatenate two strings with one other character that does not appear in two strings, and at the end// Also use one character as the end. A string of substrings, which must be a prefix of a suffix. Find the common prefix LCP for the height array//height[i] for sa[i-1] and Sa[i]. This will connect two strings. The longest common//substring must be contiguous. If not adjacent, There will surely be a string with a longer common string with him adjacent to it//because they are lined up. This is the key. Finally, it is the answer that asks whether to take out a//MAX Height[i in two different strings.////sentiment:////Despite seeing the papers of Luo's predecessors, Know the whole idea, learn the multiplication algorithm of teacher Liu. Follow your own understanding//knock a suffix array, the result has been re, has been wa, searched the search. I find that all of you have written about the same. I do not know the suffix array. Do not have to add a minimum//character to the back. (This is for ease of comparison size) My understanding is that this character defines a benchmark, because it is smaller//more efficient than all of them. Two keyword sorting combined with a small label, not only does not affect the results, but will be more effective/ Achieve 22 comparisons. This is the first reason for my WA. The second is to change the scope of C to 500, smaller. In fact, this is//there is a problem. Because of the cardinality sort. The final result must be a 0~n-1 range. So c is small, it must be re. The so-called Cardinal sort. Once again strengthened their understanding. This problem starts from the beginning, to the last AC and understanding. It took 5 hours.//during which I have been all kinds of debug. I suspect there is a mistake here, and I suspect there is a mistake. It's still a piece of the brain. Not really able to apply//level But I will persist. No one teaches, then some are self-taught. There are some of their own brains. Be calm/awake. Never panic, though this is a bare question of an array of suffixes. But I still spent 5 hours to//ac on the basis of my understanding of the practice. The multiplication is amazing. Set of templates, I will never. I can only understand it again and again.//In general, the first suffix array of life, although a lot of hardships. But there is a little bit of harvest. Even if only a small, I will//for it. Fighting!!! #include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace std;const int max_n = 500009;int N;int c[max_ N];int sa[max_n];int height[max_n];int rank[max_n];int t[max_n];int t2[max_n];char str1[MAX_N];char str2[MAX_N];int u; int s[max_n];void build_sa (int n,int m) {int *x = T;int *y = t2;for (int i=0;i<m;i++) c[i] = 0;for (int i=0;i<n;i++) c[ X[i] = s[i]]++;for (int i=1;i<m;i++) c[i] + = c[i-1];for (int i=n-1;i>=0;i--) sa[--c[x[i] [] = i;for (int k = 1; k <= n; k <<=1) {int p = 0;for (int i=n-k;i<n;i++) y[p++] = i;for (int i=0;i<n;i++) if (Sa[i] >= k) y[p++] = Sa[i] -k;for (int i = 0;i < m; i++) c[i] = 0;for (int i = 0;i < n; i++) c[x[y[i]]]++;for (int i=0;i<m;i++) c[i] + = c[i- 1];for (int i=n-1;i>=0;i--) sa[--c[x[y[i]]] = Y[i];swap (x, y);p = 1;x[sa[0]] = 0;for (int i=1;i<n;i++) x[sa[i]] = Y[sa [I-1]] ==y[sa[i]] && y[sa[i-1]+k] = = Y[sa[i]+k]?p-1:p++;if (p>=n) break;m = p;}} void Get_h (int n) {for (int i = 0;i < n;i++) Rank[sa[i]] = I;iNT k = 0;//int j;//height[0] = 0;for (int i = 0;i < n;i++) {if (k) K--;int j = sa[rank[i]-1];while (S[i+k]==s[j+k]) k++;he Ight[rank[i]] = k;//cout << k << Endl;}} BOOL Judge (int i) {if (Sa[i-1]<u && u < sa[i]) | | (Sa[i] < u && u < sa[i-1])) return True;return false;} void print (int *a) {for (int i=0;i<n;i++) {printf ("%d", A[i]);} cout << Endl;} int Get_long () {int mx = 0;for (int i=1;i<n;i++) {if (height[i]>mx && judge (i)) {mx = height[i];}} return MX;} void Solve () {int len1 = strlen (str1); int len2 = strlen (str2); u = len1;//str1[len++] = ' # ';//for (int i = 0;str2[i];i++,len + +) {//str1[len] = str2[i];//}//str1[len++] = 0;//n = Len;n = 0;for (int i=0;str1[i];i++) s[n++] = str1[i]-' a ' + 1;s[n++] = 28;for (int i =0;str2[i];i++) s[n++] = str2[i]-' a ' + 1;//cout << n << "" << len1 + len2 << End l;s[n++] = 0;build_sa (n,30);//print (sa)//cout << str1 << endl;get_h (n);//print (height);p rintf ("%d\n", Get_long ());}int Main () {//freopen ("1.txt", "R", stdin), while (scanf ("%s%s", STR1,STR2)!=eof) {Solve ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2774 Long long Message suffix array