POJ 1226 substrings (suffix array)

Source: Internet
Author: User
Tags int size

Substrings
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13919 Accepted: 4922

Description you given a number of case-sensitive strings of alphabetic characters, find the largest string X, such tha T either X, or its inverse can is found as a substring of any of the given strings.

Input the first line of the input contains a single integer t (1 <= t <=), the number of test cases, followed by The input data for each test case. The first line of all test case contains a single integer n (1 <= n <=), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.

Output there should is one line per test case containing the length of the largest string found.

Sample Input

2
3
ABCD
bcdff
BRCD
2
rose
Orchid

Sample Output

2

Source Tehran 2002 Preliminary

[Submit] [Go back] [Status] [discuss]

The longest common substring that appears in each string after the occurrence or reversal.

Puzzle: Suffix Array

Each string is reversed followed by the original string, and then all strings are connected. Then two points can be judged.

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define N 100003
using namespace Std;
int M,n,p,len;
int sa[n],rank[n],height[n],xx[n],yy[n],*x,*y;
int b[n],a[n],vis[n],pos[n];
Char S[n];
	void Init () {memset (sa,0,sizeof (SA));
	memset (rank,0,sizeof (rank));
	Memset (b,0,sizeof (b));
	memset (vis,0,sizeof (VIS));
	memset (Pos,0,sizeof (POS));
	memset (A,0,sizeof (a));
memset (height,0,sizeof (height)); 
} int cmp (int i,int j,int l) {return y[i]==y[j]&& (i+l>len?-1:y[i+l]) = = (J+l>len?-1:y[j+l]); void Get_sa ()
	{x=xx; y=yy; m=500;
	for (int i=1;i<=len;i++) b[x[i]=a[i]]++;
	for (int i=1;i<=m;i++) b[i]+=b[i-1];
	for (int i=len;i>=1;i--) sa[b[x[i]]--]=i;
		for (int k=1;k<=len;k<<=1) {p=0;
		for (int i=len-k+1;i<=len;i++) y[++p]=i;
		for (int i=1;i<=len;i++) if (sa[i]>k) y[++p]=sa[i]-k;
		for (int i=1;i<=m;i++) b[i]=0;
		for (int i=1;i<=len;i++) b[x[y[i]]]++; for (int i=1;i<=m;i++) b[i]+=b[i-1];
		for (int i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i]; Swap (x, y); p=2;
		X[sa[1]]=1;
		for (int i=2;i<=len;i++) x[sa[i]]=cmp (sa[i-1],sa[i],k)? p-1:p++;
		if (P>len) break;
	m=p+1;
	} p=0;
	for (int i=1;i<=len;i++) rank[sa[i]]=i;
		for (int i=1;i<=len;i++) {if (rank[i]==1) continue;
		int j=sa[rank[i]-1];
		while (I+p<=len&&j+p<=len&&a[i+p]==a[j+p]) p++;
		Height[rank[i]]=p;
	P=max (p-1,0);
	}} int pd (int x) {int size=0; int last=1;
	memset (vis,0,sizeof (VIS)); 
	if (Pos[sa[1]]) size=1,vis[pos[sa[1]]]=1;
	 	for (int i=2;i<=len;i++) if (height[i]>=x) {vis[pos[sa[i]]]++;
	 if (Vis[pos[sa[i]]]==1&&pos[sa[i]]) size++;
	 	} else {if (size==n) return 1;
	 	for (int j=last;j<i;j++) vis[pos[sa[j]]]=0;
		if (Pos[sa[i]]) size=1,vis[pos[sa[i]]]=1;
		else size=0;
	 last=i;
	} if (size==n) return 1;
return 0;
	} int main () {freopen ("a.in", "R", stdin);
	Freopen ("My.out", "w", stdout);
	int T;
	scanf ("%d", &t); for (iNT t=1;t<=t;t++) {init (); scanf ("%d", &n);
		len=0; int base=200;
		int n1=0;
			for (int i=1;i<=n;i++) {scanf ("%s", s+1);
			N1=strlen (s+1);
			if (i!=1) a[++len]=++base;
			for (int j=1;j<=n1;j++) a[++len]=s[j],pos[len]=i;
			A[++len]=++base;
		for (int j=n1;j>=1;j--) a[++len]=s[j],pos[len]=i;
			} if (n==1) {printf ("%d\n", N1);
		Continue
		} Get_sa (); int l=1; int R=len;
		int ans=0;
			while (l<=r) {int mid= (L+R)/2;
			if (PD (mid)) Ans=max (Ans,mid), l=mid+1;
		else r=mid-1;
	} printf ("%d\n", ans); }
}


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.