POJ 2774 Long long Message suffix array

Source: Internet
Author: User

Title: Give two strings, find the longest common sequence of the two strings.


Idea: to synthesize two strings into one, then run a suffix array, find the rank array and the height array, and then verify that rank is adjacent to the two suffixes in different strings, and if so, update the answer.


CODE:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 2000100using namespace Std;char s[max],_s[max];int len,_len;int sa[max],val[max],rank[max],height[max];int ans; inline bool Same (int x,int y,int l) {return val[x] = = val[y]&& ((x + l >= len && y + l >= len) | | (x + L < len && y + L < len && val[x + l] = = Val[y + l]));} void Getsuffixarray () {static int _val[max],q[max],cnt[max],lim = 300;for (int i = 0; i < len; ++i) ++cnt[val[i] = S[i]];f or (int i = 1; i < Lim; ++i) Cnt[i] + cnt[i-1];for (int i = len-1; I >= 0; i) sa[--cnt[val[i]] = i;for (int d = 1 ;; ++D) {int top = 0,l = 1 << (d-1), for (int i = 0; i < len; ++i) if (Sa[i] + l >= len) q[top++] = sa[i];for (int i = 0; i < Len; ++i) if (Sa[i] >= l) q[top++] = Sa[i]-l;for (int i = 0; i < Lim; ++i) cnt[i] = 0;for (int i = 0; i < len; ++i) ++cnt[v al[q[i]]];for (int i = 1; i < Lim; ++i) Cnt[i] + cnt[i-1];for (int i = len-1; I >= 0; -i) sa[--cnt[val[q[i]]] = Q[i];lim = 0;for (int i = 0,j; i < Len; ++lim) {for (j = i; j < len-1 && Same (sa[ J],sa[j + 1],l); ++J); for (; I <= J; ++i) _val[sa[i]] = lim;} for (int i = 0; i < len; ++i) Val[i] = _val[i];if (lim = = len) break; return;} void GetHeight () {for (int i = 0; i < len; ++i) rank[sa[i]] = i;for (int i = 0; i < len; ++i) {if (!rank[i]) continue;int j = 0;if (i) j = max (0,height[rank[i-1]]-1); for (; I + j < len && Sa[rank[i]-1] + j < len && S[i + j] = = S[sa[rank[i]-1] + j]; ++J); Height[rank[i]] = j;}} int main () {scanf ("%s%s", s,_s), _len = Len = strlen (s); S[len] = ' $ '; int __len = strlen (_s); for (int i = 0; i < __len; ++i) S[len + i + 1] = _s[i];s[len + strlen (_s) + 1] = ' + '; len = strlen (s); Getsuffixarray (); GetHeight (); for (int i = 1; i < Len; ++i) if (Sa[i] < _len && sa[i-1] > _len | | (Sa[i] > _len && sa[i-1] < _len) ans = max (ans,height[i]); cout << ans << endl;rEturn 0;} 


POJ 2774 Long long Message suffix array

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.