[二分][后缀数组]Finding Palindromes POJ3376

23 篇文章 0 订阅
22 篇文章 0 订阅

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')

题意: 给出n个字符串,求在所有字符串中出现次数至少为两次(不可重叠)的最长公共子串长度。

分析: 感觉像是前几道题目的综合。多个字符串要处理,显然是用分隔符间隔开然后依次连接起来,之后二分长度len,对于确定的len去check是否合法,check过程可以遍历一遍height数组,对于一段连续的height值大于等于len的区间,统计是否来自全部字符串,同时统计是否在每个字符串内不重叠地出现了两次,这个过程可以利用vector简化。剩下的步骤与之前的题目类似,不多赘述。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1e6+10;
int n, m, num_str;
char s[maxn];
int sa[maxn], height[maxn], x[maxn], y[maxn], rk[maxn], tong[maxn], str[maxn];
char st[maxn];

void get_sa()
{
	for(int i = 0; i <= m; i++) tong[i] = 0;
	for(int i = 0; i <= 2*n; i++) y[i] = x[i] = 0;
    for(int i = 1; i <= n; i++) tong[x[i] = s[i]] ++;
    for(int i = 2; i <= m; i++) tong[i] += tong[i-1];
    for(int i = n; i; i--) sa[tong[x[i]]--] = i;
    for(int k = 1; k <= n; k <<= 1) 
	{
        int num = 0;
        for(int i = n-k+1; i <= n; i++) y[++num] = i;
        for(int i = 1; i <= n; i++) 
		{
            if(sa[i] <= k) continue;
            y[++num] = sa[i] - k;
        }
        for(int i = 0; i <= m; i++) tong[i] = 0;
        for(int i = 1; i <= n; i++) tong[x[i]]++;
        for(int i = 2; i <= m; i++) tong[i] += tong[i-1];
        for(int i = n; i; i--) sa[tong[x[y[i]]]--] = y[i], y[i] = 0;
    	for(int i = 0; i <= 2*num; i++)
    	{
    		int temp = x[i];
    		x[i] = y[i];
    		y[i] = temp;
		}
        x[sa[1]] = 1, num = 1;
        for(int i = 2; i <= n; i++) 
            x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i] + k] == y[sa[i-1] + k]) == 1 ? num : ++ num;
        if(n == num) return;
        m = num;
    }
}

void get_height() 
{
    for(int i = 1; i <= n; i++) rk[sa[i]] = i;
    for(int i = 1, k = 0; i <= n; i++) 
	{
        if(rk[i] == 1) continue;
        if(k) k--;
        int j = sa[rk[i]-1];
        while(i + k <= n && j + k <= n && s[i+k] == s[j+k]) k++;
        height[rk[i]] = k;
    }
}

bool check(int x)
{
	vector<int> a[15];
	bool flag = false;
	for(int i = 1; i <= n; i++)
	{
		if(height[i] >= x)
		{
			if(!flag)
			{
				flag = true;
				a[str[sa[i-1]]].push_back(sa[i-1]);
			}
			a[str[sa[i]]].push_back(sa[i]);
		}
		if(height[i] < x || i == n)
		{
			flag = false;
			for(int i = 1; i <= num_str; i++)
			{
				if(a[i].size() < 2)
					break;
				int t = *max_element(a[i].begin(), a[i].end())-*min_element(a[i].begin(), a[i].end());
				if(t < x)
					break;
				if(i == num_str)
					return true;
			}
			for(int i = 1; i <= num_str; i++)
				a[i].clear();
		}
	}
	return false;
}

void solve() 
{ 
    get_sa();
    get_height();
	int l = 1, r = 10005, ans = -1;//二分符合题意的最长长度 
	while(l <= r)
	{
		int m = l+r>>1;
		if(check(m))
		{
			ans = m;
			l = m+1;
		}
		else
			r = m-1;
	}
	if(ans == -1)
		cout << 0 << endl;
	else
		cout << ans << endl;
}

signed main()
{
	int T;
	cin >> T;
	while(T--) 
	{
		cin >> num_str;
		int len = 0;
		for(int i = 1; i <= num_str; i++)
		{
			scanf("%s", st+1);
			int lent = strlen(st+1);
			for(int j = len+1; j <= lent+len; j++)
				s[j] = st[j-len], str[j] = i;
			len += lent;
			s[++len] = i;
		}
		n = len;
		m = 200;
		solve();
	}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值