POJ-1226(后缀数组)(Substrings)

可以暴力破解,这里贴的代码没有模版

int who[Max], yes[101], ii;
int len, n, nn;
int ans[Max], ss;
int check(int mid)
{
	int i, j, k, t, s, flag = 0;
	for (i = 2; i <= len; i = j + 1) {
		for (; height[i] < mid && i <= len; i++) ;
		for (j = i; height[j] >= mid; j++) ;
		if (j - i + 1 < n) continue;
		ii++; s = 0;
		for (k = i - 1; k < j; k++)
			if ((t = who[sa[k]]) != 0)
				if (yes[t] != ii)
					yes[t] = ii, s++;
		if (s >= n) return 1;
	}
	return 0;
}
int r[Max];
int main()
{
	int i, j, k;
	char s[1005];
	int flag = 0;
	int t;
	cin>>t;
	while (t--) {
		scanf("%d", &n);
		len = 0;
		for (i = 1; i <= n; ++i) {
			scanf("%s", s);
			int k = strlen(s);
			for (j = 0; j < k; ++j) {
				r[j + len] = s[j] + 200;
				who[j + len] = i;				
			}
			r[len + k] = 2 * i - 1;
			who[len + k] = 0;
			len += k + 1;
			for (j = 0; j < k; ++j) {
				r[j + len] = s[k - j - 1] + 200;
				who[j + len] = i;				
			}
			r[len + k] = 2 * i;
			who[len + k] = 0;
			len += k + 1;
		}
		--len;
		r[len] = 0;
		memset(sa, 0, sizeof(sa));
		da(r, len + 1, 500);
		calHeight(r, len);
		height[len + 1] = -1;
		int min = 1, max = 100;
		while (min <= max) {
			int mid = (min + max) >> 1;
			if (check(mid)) min = mid + 1;
			else max = mid - 1;
		}
		if (n == 1) max = len / 2;
		printf("%d\n", max);
	}
    return 0;
}


你可能感兴趣的:(POJ-1226(后缀数组)(Substrings))