POJ - 2774 Long Long Message

1.題面

http://poj.org/problem?id=2774

2.題意

給你兩個串,求兩個串中最長的公共子串,注意,不是公共子序列

3.思路

連接兩個字符串並用特殊字符分隔兩個字符串,隨後使用後綴數組,只要相鄰的兩個字符串屬於不同的部分,就可以更新一次答案

4.代碼

/*****************************************************************
    > File Name: Cpp_Acm.cpp
    > Author: Uncle_Sugar
    > Mail: [email protected]
    > Created Time: 2016年07月30日 星期六 21時03分33秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

# define rep(i,a,b) for (i=a;i<=b;i++)
# define rrep(i,a,b) for (i=b;i>=a;i--)

template<class T>void PrintArray(T* first,T* last,char delim=' '){ 
	for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim); 
}

const int debug = 1;
const int size  = 10 + 300000; 
const int INF = INT_MAX>>1;
typedef long long ll;

const int MAXN = 300000 + 100;
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[MAXN*3],wb[MAXN*3],wv[MAXN*3],wss[MAXN*3];
int c0(int *r,int a,int b)
{
	return r[a] == r[b] && r[a+1] == r[b+1] && r[a+2] == r[b+2];
}
int c12(int k,int *r,int a,int b)
{
	if(k == 2)
		return r[a] < r[b] || ( r[a] == r[b] && c12(1,r,a+1,b+1) );
	else return r[a] < r[b] || ( r[a] == r[b] && wv[a+1] < wv[b+1] );
}
void sort(int *r,int *a,int *b,int n,int m)
{
	int i;
	for(i = 0;i < n;i++)wv[i] = r[a[i]];
	for(i = 0;i < m;i++)wss[i] = 0;
	for(i = 0;i < n;i++)wss[wv[i]]++;
	for(i = 1;i < m;i++)wss[i] += wss[i-1];
	for(i = n-1;i >= 0;i--)
		b[--wss[wv[i]]] = a[i];
}
void dc3(int *r,int *sa,int n,int m)
{
	int i, j, *rn = r + n;
	int *san = sa + n, ta = 0, tb = (n+1)/3, tbc = 0, p;
	r[n] = r[n+1] = 0;
	for(i = 0;i < n;i++)if(i %3 != 0)wa[tbc++] = i;
	sort(r + 2, wa, wb, tbc, m);
	sort(r + 1, wb, wa, tbc, m);
	sort(r, wa, wb, tbc, m);
	for(p = 1, rn[F(wb[0])] = 0, i = 1;i < tbc;i++)
		rn[F(wb[i])] = c0(r, wb[i-1], wb[i]) ? p - 1 : p++;
	if(p < tbc)dc3(rn,san,tbc,p);
	else for(i = 0;i < tbc;i++)san[rn[i]] = i;
	for(i = 0;i < tbc;i++) if(san[i] < tb)wb[ta++] = san[i] * 3;
	if(n % 3 == 1)wb[ta++] = n - 1;
	sort(r, wb, wa, ta, m);
		for(i = 0;i < tbc;i++)wv[wb[i] = G(san[i])] = i;
	for(i = 0, j = 0, p = 0;i < ta && j < tbc;p++)
		sa[p] = c12(wb[j] % 3, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
	for(;i < ta;p++)sa[p] = wa[i++];
	for(;j < tbc;p++)sa[p] = wb[j++];
}
//str和sa也要三倍
void da(int str[],int sa[],int rank[],int height[],int n,int m)
{
	for(int i = n;i < n*3;i++)
		str[i] = 0;
	dc3(str, sa, n+1, m);
	int i,j,k = 0;
	for(i = 0;i <= n;i++)rank[sa[i]] = i;
	for(i = 0;i < n; i++)
	{
		if(k) k--;
		j = sa[rank[i]-1];
		while(str[i+k] == str[j+k]) k++;
		height[rank[i]] = k;
	}
}
int str[size], sa[size], rank[size], height[size];
char s[1000000+10];

void Debug(int n){
	for (int i=1;i<=n;i++){
		cout << "* " << i << "*\t" << height[i] << "\t";
		PrintArray(str+sa[i],str+n);
	}
	for (int i=0;i<n;i++){
		cout << i << (i==n-1?'\n':'\t');
	}
	for (int i=0;i<n;i++){
		cout << rank[i] << (i==n-1?'\n':'\t');
	}
	cout << endl;
}

int n,k;	
int table[size];

char s1[size],s2[size];
int len1;
int main()
{
	//# std::ios::sync_with_stdio(false);cin.tie(0);
	int i,j;
	while (~scanf("%s%s",s1,s2)){
		int n = 0;
		for (i=0;s1[i];i++){
			str[n++] = s1[i];
		}
		len1 = n;
		str[n++] = '1';
		for (i=0;s2[i];i++){
			str[n++] = s2[i];
		}
		str[n] = 0;
		da(str,sa,rank,height,n,300);
		//# Debug(n);	
		int ans = 0;
		for (i=2;i<=n;i++){
			if ((sa[i-1]<len1)!=(sa[i]<len1))
				ans = max(height[i],ans);
		}
		cout << ans << endl;
	}
	return 0;
}







發佈了150 篇原創文章 · 獲贊 30 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章