spoj 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.

Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba

Output:
2

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


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm = 100005;
const int INF = 1e9 + 7;
int n, m, T;
char str[maxm];
int id[maxm], sa[maxm], Rank[maxm], Height[maxm], tp[maxm], s[maxm], a[maxm], dp[maxm][25], mx[maxm], mi[maxm];
int cmp(int *f, int x, int y, int w)
{
	return f[x] == f[y] && f[x + w] == f[y + w];
}
void Rsort()
{
	for (int i = 0;i <= m;i++) s[i] = 0;
	for (int i = 1;i <= n;i++) s[Rank[tp[i]]]++;
	for (int i = 1;i <= m;i++) s[i] += s[i - 1];
	for (int i = n;i >= 1;i--) sa[s[Rank[tp[i]]]--] = tp[i];
}
void suffix()
{
	memset(Height, 0, sizeof(Height));
	for (int i = 1;i <= n;i++) Rank[i] = a[i], tp[i] = i;
	m = 127, Rsort();
	for (int i, w = 1, p = 1;p < n;w += w, m = p)
	{
		for (p = 0, i = n - w + 1;i <= n;i++) tp[++p] = i;
		for (i = 1;i <= n;i++) if (sa[i] > w) tp[++p] = sa[i] - w;
		Rsort(), swap(Rank, tp), Rank[sa[1]] = p = 1;
		for (i = 2;i <= n;i++) Rank[sa[i]] = cmp(tp, sa[i], sa[i - 1], w) ? p : ++p;
	}
	int j, k = 0;
	for (int i = 1;i < n;Height[Rank[i++]] = k, tp[i] = 0)
		for (k = k ? k - 1 : k, j = sa[Rank[i] - 1];a[i + k] == a[j + k];k++);
	//for (int i = 1;i <= n;i++) printf("%d ", Height[i]);printf("\n");
}
bool judge(int x)
{
	int i, j;
	for (i = 0;i <= T + 1;i++) mi[i] = INF, mx[i] = 0;
	for (i = 2;i <= n;i++)
	{
		if (Height[i] < x)
		{
			for (j = 1;j <= T;j++) mx[j] = 0, mi[j] = INF;
			mx[id[sa[i]]] = sa[i], mi[id[sa[i]]] = sa[i];
		}
		else
		{
			mx[id[sa[i]]] = max(mx[id[sa[i]]], sa[i]);
			mi[id[sa[i]]] = min(mi[id[sa[i]]], sa[i]);
			mx[id[sa[i - 1]]] = max(mx[id[sa[i - 1]]], sa[i - 1]);
			mi[id[sa[i - 1]]] = min(mi[id[sa[i - 1]]], sa[i - 1]);
			for (j = 1;j <= T;j++)
			{
				if (mx[j] - mi[j] < x)
					break;
			}
			if (j == T + 1) return 1;
		}
	}
	return 0;
}
int main()
{
	int i, j, k, sum, t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &T);n = 0;
		for (i = 1;i <= T;i++)
		{
			scanf("%s", str);
			for (j = 0;str[j] != '\0';j++)
			{
				a[++n] = str[j];
				id[n] = i;
			}
			a[++n] = '#' + i, id[n] = i;
		}
		a[n] = 0;
		suffix();
		int l = 0, r = 10000, mid, ans = 0;
		while (l <= r)
		{
			mid = (l + r) / 2;
			if (judge(mid))
			{
				ans = mid;
				l = mid + 1;
			}
			else r = mid - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值