SPOJ 220 Relevant Phrases of Annihilation(后缀数组)

时间:2023-03-09 01:25:58
SPOJ 220 Relevant Phrases of Annihilation(后缀数组)

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to 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 begins with 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 additional 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.

题目大意:给n个字符串,求在每个字符串中出现至少两次且不重叠的最长子串的长度。

思路:每个字符串用不相同的字符连起来。求后缀数组和height[]数组。

二分长度L,检查长度。

检查的时候从前往后扫一遍,看有没有连起来的一部分,公共前缀大于等于L且在每个字符串中都出现了且不重叠。

复杂度为O(nlogn)

代码(0.34s):

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; char s[MAXN];
int id[MAXN];
int sa[MAXN], rank[MAXN], height[MAXN], c[MAXN], tmp[MAXN];
int n, m, T; void makesa(int m) {
memset(c, , m * sizeof(int));
for(int i = ; i < n; ++i) ++c[rank[i] = s[i]];
for(int i = ; i < m; ++i) c[i] += c[i - ];
for(int i = ; i < n; ++i) sa[--c[rank[i]]] = i;
for(int k = ; k < n; k <<= ) {
for(int i = ; i < n; ++i) {
int j = sa[i] - k;
if(j < ) j += n;
tmp[c[rank[j]]++] = j;
}
int j = c[] = sa[tmp[]] = ;
for(int i = ; i < n; ++i) {
if(rank[tmp[i]] != rank[tmp[i - ]] || rank[tmp[i] + k] != rank[tmp[i - ] + k])
c[++j] = i;
sa[tmp[i]] = j;
}
memcpy(rank, sa, n * sizeof(int));
memcpy(sa, tmp, n * sizeof(int));
}
} void calheight() {
for(int i = , k = ; i < n; height[rank[i++]] = k) {
k -= (k > );
int j = sa[rank[i] - ];
while(s[i + k] == s[j + k]) ++k;
}
} int mx[MAXN], mn[MAXN];
int stk[MAXN]; void update_max(int &a, int b) {
if(a == - || a < b) a = b;
} void update_min(int &a, int b) {
if(a == - || a > b) a = b;
} bool check(int L) {
int sum = , top = ;
memset(mx, -, m * sizeof(int));
memset(mn, -, m * sizeof(int));
memset(c, , m * sizeof(int));
for(int i = ; i < n; ++i) {
if(height[i] >= L) {
update_max(mx[id[sa[i]]], sa[i]);
update_min(mn[id[sa[i]]], sa[i]);
stk[++top] = id[sa[i]];
if(mx[id[sa[i]]] - mn[id[sa[i]]] >= L) {
if(!c[id[sa[i]]]) ++sum;
c[id[sa[i]]] = true;
if(sum >= m) return true;
}
} else {
sum = ;
while(top) {
int t = stk[top--];
mx[t] = mn[t] = -;
c[t] = false;
}
update_max(mx[id[sa[i]]], sa[i]);
update_min(mn[id[sa[i]]], sa[i]);
stk[++top] = id[sa[i]];
}
}
return false;
} int solve() {
int l = , r = ;
while(l < r) {
int mid = (l + r) >> ;
if(check(mid)) l = mid + ;
else r = mid;
}
return l - ;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &m);
n = ;
for(int i = ; i < m; ++i) {
scanf("%s", s + n);
while(s[n]) id[n++] = i;
s[n++] = i + ;
}
s[n - ] = ;
makesa();
calheight();
printf("%d\n", solve());
}
}