Group Programming ladder match L2-008 symmetry longest substring (25 points)

Topic links:

L2-008 symmetry longest substring (25 points)

thinking:

This question is seeking the longest palindrome string of bare questions, violence is said to be able to live?
Bloggers with a string suffix array of arrays + + height range RMQ solution, is the most troublesome method to seek, why bother it? Because just want to review the suffix array :)
solving palindrome strings have a more simple and efficient algorithm for example Manacher algorithm;
since it is not to go into the bare title, pay attention to two points bloggers encounter in the course of the review of this suffix array a list of what to remind myself hope to help you:
1. after calculating lcp should keep in mind lcp[i]is the first ione and the second i+1one was the longest common prefix length and i-1a common prefix length;
2. rmq query interval ito jtime , it should be noted that the query min(rk[i],rk[j])to max[rk[i],rk[j])-1, why should it minus 1? From the assumption xfound y, meaning and y is at the longest common prefix length and the next, and our current independent interrogation;

Code:

#include<bits/stdc++.h>

using namespace std;

const int maxn = 2345;
int dat[maxn][20], mm[maxn];
void initRMQ(int n, int b[]) {
	mm[0] = -1;
	for(int i = 1; i <= n; ++i) {
		mm[i] = ((i & (i - 1)) == 0) ? mm[i - 1] + 1 : mm[i - 1];
		dat[i][0] = b[i];
	}
	for(int j = 1; j <= mm[n]; ++j)
	for(int i = 1; i + (1 << j) - 1 <= n; ++i)
	dat[i][j] = min(dat[i][j - 1], dat[i + (1 << (j - 1))][j - 1]);
}
inline int rmq(int x, int y) {
	if(x > y) swap(x ,y); --y;
	int k = mm[y - x + 1];
	return min(dat[x][k], dat[y - (1 << k) + 1][k]);	
}
string s;
int n, k, sa[maxn], rk[maxn], tmp[maxn], lcp[maxn];
bool cmp(const int & i, const int & j) {
	if(rk[i] != rk[j]) return rk[i] < rk[j];
	return (i + k <= n ? rk[i + k] : -1) < (j + k <= n ? rk[j + k] : -1);
}
void get_sa() {
	n = s.length();
	for(int i = 0; i <= n; ++i) sa[i] = i, rk[i] = i < n ? s[i] : -1;
	for(k = 1; k <= n; k <<= 1) {
		sort(sa, sa + n + 1, cmp);
		tmp[sa[0]] = 0;
		for(int i = 1; i <= n; ++i) tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]);
		for(int i = 0; i <= n; ++i) rk[i] = tmp[i];
	}
}
void get_lcp() {
	for(int i = 0; i <= n; ++i) rk[sa[i]] = i;
	int h = 0; lcp[0] = 0;
	for(int i = 0; i < n; ++i) {
		int j = sa[rk[i] - 1];
		if(h) --h;
		while(j + h < n && i + h < n && s[j + h] == s[i + h]) ++h;
		lcp[rk[i]- 1] = h;
	}
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	getline(cin, s);
	string a = s;
	reverse(a.begin(), a.end());
	s += "$" + a;
	get_sa(); get_lcp();
	initRMQ(n, lcp);
	int ans = 1;
	for(int i = 0; i < n / 2; i++) {
		int j = n - i - 1;
		int l = rmq(rk[i], rk[j]);
		ans = max(ans, 2 * l - 1);
	}
	for(int i = 0; i < n / 2; i++) {
		int j = n - i;
		int l = rmq(rk[i], rk[j]);
		ans = max(ans, l << 1);	
	}
	cout << ans;
	return 0;
}
Published 280 original articles · won praise 7 · views 6697

Guess you like

Origin blog.csdn.net/qq_45228537/article/details/104044019
Recommended