HDU 1403 Longest Common Substring

Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:
str1 = banana
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.
 

Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.
 

Output
For each test case, you have to tell the length of the Longest Common Substring of them.
 

Sample Input
  
  
banana cianaic
 

Sample Output
  
  
3

后缀数组的应用

#include<iostream>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200005;

class suffix
{
private:
	char s[maxn];
	int r[maxn], w[maxn], ss[maxn], h[maxn];
	int sa[maxn], rk[maxn + maxn], size, bit = 256;
	int size1;
public:
	bool get(){ 
		if (scanf("%s", s + 1) != 1) return false;
		size1 = strlen(s + 1);
		scanf("%s", s + size1 + 2);
		s[++size1] = 1;
		size = strlen(s + 1);
		return true;
	}
	void pre()
	{
		memset(rk, 0, sizeof(rk));
		for (int i = 1; i <= bit; i++) w[i] = 0;
		for (int i = 1; i <= size; i++) w[(int)s[i]]++;
		for (int i = 1; i <= bit; i++) w[i] += w[i - 1];
		for (int i = size; i; i--) sa[w[(int)s[i]]--] = i;
		for (int i = 1, j = 1; i <= size; i++)
		rk[sa[i]] = (s[sa[i]] == s[sa[i + 1]] ? j : j++);
		for (int j = 1; j < size; j += j)
		{
			for (int i = 1; i <= size; i++) w[i] = 0;
			for (int i = 1; i <= size; i++) w[rk[i + j]]++;
			for (int i = 1; i <= size; i++) w[i] += w[i - 1];
			for (int i = size; i; i--) ss[w[rk[i + j]]--] = i;

			for (int i = 1; i <= size; i++) w[i] = 0;
			for (int i = 1; i <= size; i++) w[rk[ss[i]]]++;
			for (int i = 1; i <= size; i++) w[i] += w[i - 1];
			for (int i = size; i; i--) sa[w[rk[ss[i]]]--] = ss[i];

			for (int i = 1, k = 1; i <= size; i++)
				r[sa[i]] = (rk[sa[i]] == rk[sa[i + 1]] && rk[sa[i] + j] == rk[sa[i + 1] + j]) ? k : k++;
			for (int i = 1; i <= size; i++) rk[i] = r[i];
		}
		for (int i = 1, k = 0, j; i <= size; h[rk[i++]] = k)
		for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++);
	}
	void work()
	{
		int mid = (size1 - 1) >> 1;
		for (int l = 0, r = size1 - 1,f; l < r;)
		{
			f = 0;
			for (int i = 2, j; i <= size; i = j + 1)
			{
				int f1 = 0, f2 = 0;
				for (j = i; h[j] >= mid; j++)
				{
					if (sa[j] < size1) f1 = 1; else f2 = 1;
					if (sa[j - 1] < size1) f1 = 1; else f2 = 1;
					if (f1 && f2) { f = 1; goto end; }
				}
			}
			end:;
			if (f) l = mid; else r = mid - 1;
			mid = (l + r) >> 1;
			if (mid == l) mid = r;
		}
		printf("%d\n", mid);
	}
}f;

int main()
{
	while (f.get())
	{
		f.pre();
		f.work();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值