Spoj 220 suffix array: returns the oldest string that appears at least twice and does not overlap.

Source: Internet
Author: User

Idea: N strings are also connected into one, separated by unused characters in the middle, and then the suffix array is obtained.

Because there is no overlap, it is the same as poj 1743, but here there are multiple strings. Each string must determine whether the longest public prefix in it overlaps, so you can save it with an array and then judge it.

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <queue> # include <set> # include <cmath> # include <bitset> # define MEM (, b) memset (a, B, sizeof (A) # define lson I <1, L, mid # define rson I <1 | 1, Mid + 1, R # define llson j <1, L, mid # define rrson j <1 | 1, Mid + 1, R # define INF 0x7fffffff # define maxn 110010 using namespace STD; typedef long ll; typedef unsigned long ull; void R Adix (int * STR, int * a, int * B, int N, int m) {static int count [maxn]; MEM (count, 0 ); for (INT I = 0; I <n; I ++) ++ count [STR [A [I]; for (INT I = 1; I <= m; I ++) Count [I] + = count [I-1]; for (INT I = n-1; I> = 0; I --) B [-- count [STR [A [I] = A [I];} void suffix (int * STR, int * Sa, int N, int m) // calculate the suffix array SA {static int rank [maxn], a [maxn], B [maxn]; for (INT I = 0; I <N; I ++) rank [I] = I; Radix (STR, rank, SA, n, m); rank [SA [0] = 0; for (in T I = 1; I <n; I ++) rank [SA [I] = rank [SA [I-1] + (STR [SA [I]! = STR [SA [I-1]); For (INT I = 0; 1 <I <n; I ++) {for (Int J = 0; j <N; j ++) {A [J] = rank [J] + 1; B [J] = J + (1 <I)> = n? 0: rank [J + (1 <I)] + 1; SA [J] = J;} Radix (B, SA, rank, N, N ); radix (A, rank, SA, n, n); rank [SA [0] = 0; For (Int J = 1; j <n; j ++) rank [SA [J] = rank [SA [J-1] + (A [SA [J-1]! = A [SA [J] | B [SA [J-1]! = B [SA [J]) ;}} void calcheight (int * STR, int * Sa, int * H, int * Rank, int N) // obtain the longest public prefix array H {int K = 0; H [0] = 0; For (INT I = 0; I <n; I ++) rank [SA [I] = I; for (INT I = 0; I <n; I ++) {k = 0? 0: K-1; If (rank [I]) while (STR [I + k] = STR [SA [rank [I]-1] + k]) K ++; else K = 0; H [rank [I] = K ;}} int A [maxn], sa [maxn], height [maxn], rank [maxn]; int V [maxn], Max [15], Min [15], R, N, Len, J; char s [10002]; bool judge (INT m) {MEM (max, 0); MEM (Min, 100); For (INT I = 1; I <Len; I ++) {If (height [I] <m) {MEM (max, 0); MEM (minute, 100); Max [V [SA [I] = sa [I]; min [V [SA [I] = sa [I];} else {max [V [SA [I] = max (MAX [V [SA [I], sa [I]); min [V [SA [I] = min (Min [V [SA [I], sa [I]); max [V [SA [I-1] = max (MAX [V [SA [I-1], sa [I-1]); min [V [SA [I-1] = min (Min [V [SA [I-1], sa [I-1]); For (j = 1; j <= N; j ++) if (MAX [J]-min [J] <m) break; If (j> N) return true ;}} return false ;} int binary () {int ans = 0, mid, L = 0; while (L <= r) {mid = (L + r)> 1; if (Judge (MID) ans = mid, L = Mid + 1; else r = mid-1;} return ans;} int main () {// freopen ("1.txt"," r ", stdin); int t; scanf (" % d ", & T); While (t --) {scanf ("% d", & N); Len = r = J = 0; For (INT I = 1; I <= N; I ++) {scanf ("% s", S); int len1 = strlen (s); If (r <len1> 1) r = len1> 1; // The maximum limit of two points for (int K = 0; k <len1; k ++) A [Len + k] = s [k] + 5, V [Len + k] = I; // the string in which V records a [Len + len1] = ++ J; // The string is separated by characters that have not been used before. V [Len + len1] = 0; Len = Len + len1 + 1;} Len --; suffix (A, SA, Len, 256); calcheight (A, SA, height, rank, Len); printf ("% d \ n", binary ();} return 0 ;}



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.