POJ 2774

点击打开链接 题目链接

可以用hash 和后缀数组两种方法。

(用hash 的话标记hash 要自己手打hash_map,stl超时了)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
const int maxn = 100000 + 23;
const LL base1 = 99991;
const LL MOD = 199613;
char str[maxn];
char s[maxn];
LL phash[maxn];
struct Map {
	int begin[MOD],Next[maxn];
	LL END[maxn];
	int cnt;
	Map() { cnt = 0;memset(begin,-1,sizeof(begin)); }
	void clear() { cnt = 0;memset(begin,-1,sizeof(begin));}
	void Push(LL x) {
		int s = x % MOD;
		for(int now = begin[s] ; now != -1; now = Next[now])
		if(END[now] == x) return;
		Next[cnt] = begin[s];
		begin[s] = cnt;
		END[cnt++] = x;
	}
	
	bool count(LL x) {
		int s = x % MOD;
		for(int now = begin[s]; now != -1; now = Next[now])
		if(END[now] == x) return true;
		return false;
	}
} HasH;
void Gethash(char *a,int m) {
	int len  = strlen(a);
	HasH.clear();
	LL hash = 0;
	for(int  i = 0; i < m; ++i) hash = hash * base1 + a[i];
	HasH.Push(hash);
	for(int i = 1; i <= len - m; ++i) {
		hash = (hash - phash[m -1] * a[i-1])* base1 + a[i + m - 1];
		HasH.Push(hash);
	}
}
int main()
{
	phash[0] = 1;
	for(int i = 1; i < maxn; ++i) phash[i] = phash[i-1] * base1;
	while(scanf("%s%s",str,s) == 2) {
		int len = strlen(s);
		int len2 = strlen(str);
		int l = 0, r = len2 > len ? len : len2;
		r++;
		while( l +1< r) {
			bool jug = false;
			int m = (l + r) >> 1;
		    //int m = 27;
			Gethash(str,m);
			LL hash = 0;
			for(int i = 0; i < m; ++i) hash = hash* base1 + s[i];
			if(HasH.count(hash)) jug = true;
			if(!jug) {
				for(int i = 1; i <= len - m; ++i) {
					hash = (hash - phash[m -1] * s[i-1])* base1 + s[i + m - 1];
					if(HasH.count(hash)) {
						jug = true;
						break;
					}
				}
			}
			if(jug) l = m;
			else r = m;
		}//
		printf("%d\n",l);
	}
	return 0;
}
后缀树组的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 131;
char str[maxn >> 1];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
int rank[maxn],height[maxn],num[maxn]; 
void GetSA(int *s,int n,int m) {
	int i, *x =t, *y = t2,p,k; //x 保存的是当前状态的rank,y保存的是第二关键字排序的结果 
	for(i = 0; i < m; ++i) c[i] = 0;//给第一关键字排序 
	for(i = 0; i < n; ++i) c[x[i] = s[i]] ++;
	for(i = 1; i < m; ++i) c[i] += c[i-1];
	for(int i = n - 1; i >=0; --i) sa[--c[x[i]]] = i;
	for(k = 1,p = 1; k <= n; k <<= 1, m = p) {
		for(p = 0,i = n -k; i < n; ++i) y[p++] = i;
		for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;//给第二关键字排序 
		for(i = 0; i < m; ++i) c[i] = 0;
		for(i = 0; i < n; ++i) c[x[y[i]]] ++;//根据第二关键字和rank给第一关键字排序 
		for(i = 1; i < m; ++i) c[i] += c[i-1];
		for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
		for(swap(x,y),p = 1,x[sa[0]] = 0,i = 1; i < n; ++i) //重新计算x 
		x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1] + k] == y[sa[i] + k] ? p-1 : p++;
		if(p >= n) break;
	}
}

void Getheight(int *s,int n) {
	int i,j,k = 0;
	for(i = 1; i <= n; ++i) rank[sa[i]] = i;
	for(i = 0; i < n; ++i) {
		if(k) k--;
		int j = sa[rank[i] - 1];
		while(s[i+k] == s[j+k]) k ++;
		height[rank[i]] = k;
	}
}

int main() {
	while(scanf("%s",str)!= EOF) {
		int pos = 0,len1,len2;
		len1 = strlen(str);
		for(int i = 0; i < len1; ++i) {
			num[pos++] = str[i] - 'a' + 2;
		}
		num[pos++] = 1;
		scanf("%s",str);
		len2 = strlen(str);
		for(int i = 0; i < len2; ++i) {
			num[pos++] = str[i] - 'a' + 2;
		}
		int n = len1 + len2;
		GetSA(num,n + 1, 30);
		Getheight(num,n);
		/*for(int i = 0; i <= n; ++i) cout <<num[i] << ' ';
		cout << endl;
		for(int i = 0; i <= n; ++i) cout <<sa[i] << ' ';
		cout << endl;
		cout << pos << endl;
		for(int i = 0; i < pos; ++i) cout << height[i] << ' ';
		cout << endl;*/
		int cnt = 0;
		for(int i = 2; i < pos; ++i) {
			if((sa[i] < len1 && sa[i-1] > len1 )|| (sa[i] > len1 && sa[i-1] < len1)) {
				cnt = max(cnt,height[i]);
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值