SPOJ 220. Relevant phrases of annihilation (suffix array multiple nonoverlapping substrings)

Source: Internet
Author: User
Tags intel pentium

Topic: Given n strings, the oldest string that appears at least two times for each string.

How to solve the problem: at least two occurrences of each string and non-overlapping eldest-son string: Two-enumeration length after each string in the same group to keep a minimum position and a maximum position, and finally see if each string in the same group has at least two suffixes, and the suffix of the coordinate is greater than the length of the enumeration.


POJ problem Set (classical) 220. Relevant phrases of Annihilationproblem code:phrases

You are the King of Byteland. Your agents has just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on Your are Land. You immedietaly send to the Bytelandian cryptographer, but he's currently busy eating popcorn and claims that he may onl Y decrypt the most important part of the text (since the rest would is a waste of his time). You decide to select the fragment of the text which the enemy have strongly emphasised, evidently regarding it as the most Important. So, you is looking for a fragment of text which appears in all the messages disjointly at least twice. Since you is not overfond of the cryptographer, try and make this fragment as long as possible.

Input

The first line of input contains a single positive integer t<=10, the number of test cases. T test cases follow. Each test case is begins with an integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters ' a '-' Z ', possibly with some addit ional trailing white space which should be ignored.

Output

For each test case output, the length of longest string which appears disjointly at least twice in all of the messages.

Example
Input:14abbabbadabddkabababacabababaOutput:2

(in the example above, the longest substring which fulfills the requirements is ' BA ')

Added by: Adrian Kosowski
Date: 2004-10-11
Time limit: 9s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages: All Except:nodejs PERL 6 SCM Chicken vb.net
Resource: DASM Programming League 2004 (Problemset 1)







#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MOD 1000000007#define Read () freopen ("Autocomplete.in", "R", stdin) #define Write () freopen ("    Autocomplete.out "," w ", stdout) #define CIN () Ios::sync_with_stdio (false) using namespace Std;inline int Read () {char ch;    BOOL flag = FALSE;    int a = 0; while (!) ( ((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) | |    (ch = = '-')));        if (ch! = '-') {a *= 10;    A + = ch-' 0 ';    } else {flag = true; } while (((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) {        A *= 10;    A + = ch-' 0 ';    } if (flag) {a = A; } return A;        void write (int a) {if (a < 0) {Putchar ('-');    A =-A;    } if (a >=) {write (A/10); } putchar (a% 10 + ' 0 ');}    const int MAXN = 200010;int WA[MAXN], WB[MAXN], WV[MAXN], ws1[maxn];int sa[maxn];int cmp (int *r, int A, int b, int l) { return R[a] = = R[b] && r[a+l] = = R[b+l];}    void da (int *r, int *sa, int n, int m) {int I, J, p, *x = WA, *y = WB;    for (i = 0; i < m; i++) ws1[i] = 0;    for (i = 0; i < n; i++) ws1[x[i] = r[i]]++;    for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];    for (i = n-1; I >= 0; i--) sa[--ws1[x[i]] = i;        for (j = 1, p = 1; p < n; j <<= 1, 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;        for (i = 0; i < n; i++) wv[i] = X[y[i];        for (i = 0; i < m; i++) ws1[i] = 0; for (i = 0; i <N        i++) ws1[wv[i]]++;        for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];        for (i = n-1; I >= 0; i--) sa[--ws1[wv[i]] = y[i];    for (Swap (x, y), 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++; } return;    int RANK[MAXN], height[maxn];void calheight (int *r, int *sa, int n) {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;} Char STR1[MAXN], str2[maxn];int seq[maxn];int hash[maxn];char str[110][1010];int vis[110];int Find (int x) {int s = hash    [x];        for (int i = 0; i < s; i++) {int len = strlen (Str[i]);    X-= Len;    } x-= s; return x;} int Pos[20][2];bool judge (int mid, int n, int m) {for (int i = 2; I <= n; i++) {memset (Vis, 0, sizeof (VIS)        );        vis[hash[sa[i-1]]]++;      Pos[hash[sa[i-1]]][0] = pos[hash[sa[i-1]]][1] = sa[i-1];  While (mid <= height[i]) {int XP = Hash[sa[i]];                if (!vis[xp]) {vis[xp]++;                POS[XP][1] = pos[xp][0] = Sa[i];                i++;            Continue            } vis[xp]++;            Pos[xp][0] = max (pos[xp][0], sa[i]);            Pos[xp][1] = min (pos[xp][1], sa[i]);        i++;        } int k;        for (k = 0, K < m; k++) {if (Vis[k] < 2 | | pos[k][0]-pos[k][1] < mid) break;    } if (k = = m) return true; } return false;}    void Del (int n, int len, int m) {int L = 1;    int r = LEN/2;    int Xans = 0;        while (L <= r) {int mid = (l+r) >>1;            if (Judge (Mid, N, m)) {L = mid+1;        Xans = mid;    } else R = mid-1; } Cout<<xans<<endl;}    int main () {int T;    CIN >>T;    int n;        while (t--) {scanf ("%d", &n); memset (hash,-1, SizEOF (hash));        int ans = 0;        int Min = MAXN;            for (int i = 0; i < n; i++) {scanf ("%s", Str[i]);            int len = strlen (Str[i]);            min = min (min, len);                for (int j = 0; J < Len; J + +) {Seq[ans] = str[i][j];            hash[ans++] = i;        } seq[ans++] = 200+i;        } Seq[ans] = 0;        Da (seq, SA, ans+1, 310);        Calheight (seq, SA, ans);    Del (ans, Min, N); } return 0;}


SPOJ 220. Relevant phrases of annihilation (suffix array multiple nonoverlapping substrings)

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.