(Relax 后缀数组1.4)POJ 2774 Long Long Message(求两个字符串公共子串的最大长度)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>


#define Fup(i, s, t) for (int i = s; i <= t; i ++)
#define Fdn(i, s, t) for (int i = s; i >= t; i --)
#define Path(i, s) for (int i = s; i; i = d[i].next)

using namespace std;

const int maxn = 200010;
struct number {
	int x;
	int pos;
} num[maxn];

struct node {
	int now;
	int next;

} d[maxn];

int val[maxn][2];
int c[maxn];
int rank[maxn];
int sa[maxn];
int pos[maxn];
int x[maxn];
int n;

int k;

int h[maxn];
int height[maxn];

string S,s;

int sta[maxn], num1[maxn], num2[maxn];

bool cmp(const number& a, const number& b) {
	return a.x < b.x;
}

void add_value(int u, int v, int i) {
	d[i].next = c[u];
	c[u] = i;
	d[i].now = v;
}

void radix_sort(int l, int r) {
	for (int k = 1; k >= 0; --k) {

		memset(c, 0, sizeof(c));
		for (int i = r; i >= l; --i) {
			add_value(val[pos[i]][k], pos[i], i);
		}
		int t = 0;
		for (int i = 0; i <= 200000; ++i) {
			for (int j = c[i]; j; j = d[j].next) {
				pos[++t] = d[j].now;
			}
		}
	}
	int t = 0;
	for (int i = 1; i <= n; ++i) {
		if (val[pos[i]][0] != val[pos[i - 1]][0]
				|| val[pos[i]][1] != val[pos[i - 1]][1]) {
			t++;
		}
		rank[pos[i]] = t;
	}
}

bool exist(int len) {
	int now = 0;
	int s = 0;
	for (int i = 1; i <= n; ++i) { //枚举名次数组...
		if (height[i] < len) {
			s = max(s, now); //结束当前组
			now = 1; //now恢复为1
		} else {
			now++;
		}
	}
	s = max(s, now);

	if (s >= k) {
		return 1;
	}
	return 0;
}

void get_suffix_array() {
	int t = 1;
	while (t / 2 <= n) {
		for (int i = 1; i <= n; ++i) {

			val[i][0] = rank[i];
			val[i][1] = (((i + t / 2 <= n) ? rank[i + t / 2] : 0));
			pos[i] = i;
		}
		radix_sort(1, n);
		t *= 2;
	}
	for (int i = 1; i <= n; ++i) {
		sa[rank[i]] = i;
	}
}

void get_common_prefix() {
	memset(h, 0, sizeof(h));
	for (int i = 1; i <= n; ++i) {

		if (rank[i] == 1) {
			h[i] = 0;
		} else {
			int now = 0;
			if (i > 1 && h[i - 1] > 1) {
				now = h[i - 1] - 1;
			}
			while (now + i <= n && now + sa[rank[i] - 1] <= n
					&& x[now + i] == x[now + sa[rank[i] - 1]]) {
				now++;
			}
			h[i] = now;
		}
	}
	for (int i = 1; i <= n; ++i) {
		height[rank[i]] = h[i];
	}
}

int binary_search(int l, int r) {
	while (l <= r) {
		int mid = (l + r) / 2;
		if (exist(mid)) {
			l = mid + 1;
		} else {
			r = mid - 1;
		}
	}
	return r;
}

//***以下是公共子串的最大长度的处理....上面的代码基本是一样的...
void init() {
	cin >> S >> s;
	n = (int) S.size() + s.size() + 1;
	string str = S + ' ' + s;
	Fup(i, 1, n)
	{
		if (i == (int) S.size() + 1)
			rank[i] = 27;//这里的27指的是空格作为第27个字符
		else
			rank[i] = (int) str[i - 1] - 'a' + 1;
		x[i] = rank[i];
	}
}

void solve() {
	get_suffix_array();
	get_common_prefix();
	int ans = 0;
	Fup(i, 2, n)
		if ((sa[i] <= (int) S.size() && sa[i - 1] > (int) S.size() + 1)//只有当suffix(sa[i-1)和suffix(sa[i])不是同一个字符串的两个后缀时,height[i]才是满足条件的..
				|| (sa[i] > (int) S.size() + 1 && sa[i - 1] <= (int) S.size()))
			ans = max(ans, height[i]);
	cout << ans << endl;
}

int main(){
	init();
	solve();

	return 0;
}


你可能感兴趣的:((Relax 后缀数组1.4)POJ 2774 Long Long Message(求两个字符串公共子串的最大长度))