UVA 10526-intellectual property (suffix array)

Source: Internet
Author: User

UVA 10526-intellectual Property

Topic links

Test instructions: Given two questions, ask to find the second text to copy the first text of the full position and length, the output before k, by length from large to small first row. The same length by position from small to large

Idea: suffix array, two pieces of text together. Record the stitching position as TDP. This assumes sa[i] < TDP is the beginning of the preceding text. Suppose Sa[i] >= TDP is the beginning of the following text, stitching up the height array, taking advantage of the nature of the array. Sweep back from the past and sweep backwards. Save the maximum value of the entire position. Finally, scan the position again. Write down the answers.

Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxlen = 200005; const int INF = 0x3f3f3f3f;char str[55555];int k, TDP, an, v[maxlen];struct Ans {int len, POS; Ans () {}ans (int len, int pos) {This->len = Len;this->pos = pos;}} Ans[maxlen];bool cmp (ans A, ans b) {if (A.len = = B.len) return A.pos < B.pos;return a.len > B.len;} struct Suffix {int s[maxlen];int sa[maxlen], T[maxlen], T2[maxlen], C[maxlen], N;int rank[maxlen], height[maxlen];void bu Ild_sa (int m) {N++;int I, *x = t, *y = t2;for (i = 0; i < m; i++) c[i] = 0;for (i = 0; i < n; i++) c[x[i] = s[i]]++;  for (i = 1; i < m; i++) C[i] + c[i-1];for (i = n-1; I >= 0; i--) sa[--c[x[i]] = i;for (int k = 1; k <= N; k <<= 1) {int p = 0;for (i = n-k; i < n; i++) y[p++] = i;for (i = 0; i < n; i++) if (Sa[i] >= k) y[p++] = Sa[i]-k;for (i = 0; i < m; i++) c[i] = 0;for (i = 0; i < n; i++) c[x[y[i]]]++;for (i = 0; i < m; i++) c[i] + = C [I-1];for (i = n-1; I >= 0; i--) sa[--c[x[y[i]]] = Y[i];swap (x, y);p = 1;  X[sa[0]] = 0;for (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;} n--;}  void GetHeight () {int I, j, k = 0;for (i = 1; I <= n; i++) rank[sa[i]] = i;for (i = 0; i < n; i++) {if (k) K--;int J = Sa[rank[i]-1];while (s[i + K] = = S[j + K]) K++;height[rank[i]] = k;}}  void init () {TDP = 0; n = 0; an = 0;gets (str) and while (gets (str)) {if (strcmp (str, "END TDP CODEBASE") = = 0) Break;int len = strlen (str); Str[len] = ' \ n '; for (int i = 0; I <= len; i++) s[n++] = Str[i];} TDP = n;s[n++] = 260;gets (str), while (gets (str)) {if (strcmp (str, "END jcn CODEBASE") = = 0) Break;int len = strlen (str); St R[len] = ' \ n '; for (int i = 0; I <= len; i++) s[n++] = Str[i];} S[n] = 0;} void Solve () {init (); Build_sa (261); GetHeight () memset (V, 0, sizeof (v)), int Min = -1;for (int i = 1; I <= n; i++) {if (s A[i] < TDP) Min = Inf;else if (sa[i] > TDP) {if (Min = =-1) continue; min = min (height[i], min); V[sa[i]-tdp-1] = max (min, v[sa[i]-tdp-1]);}} min = -1;for (int i = n; I >= 1; i--) {if (Sa[i] < TDP) min = Inf;else if (sa[i] > TDP) {if (Min = =-1) continue; min = min (height[i + 1], min); V[sa[i]-tdp-1] = max (min, v[sa[i]-tdp-1]);}} int r = -1;for (int i = 0; i < N-TDP; i++) {if (i + v[i] <= r) continue;if (v[i] = = 0) continue;ans[an++] = ans (v[ I], i); r = i + V[i];} Sort (ans, ans + AN, CMP); for (int i = 0; i < min (A, k); i++) {printf ("infringing SEGMENT%d LENGTH%d POSITION%d\n", i + 1, Ans[i].len, Ans[i].pos), for (int j = ans[i].pos + TDP + 1; j < Ans[i].pos + TDP + 1 + ans[i].len; j + +) printf ("%c ", S[j]);p rintf (" \ n ");}} Gao;int Main () {int bo = 0;int cas = 0;while (~scanf ("%d%*c", &k) && k) {if (bo) printf ("\ n"); else Bo = 1;prin TF ("Case%d\n", ++cas); Gao.solve ();} return 0;}


UVA 10526-intellectual property (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.