POJ-3974 Palindrome

Manacher模板题

#include 
#include 
#include 
using namespace std;
const int N = 1e6 + 5;
const int NN = 2e6 + 5;
char s[N], ss[NN];
int p[NN], len, ans;
void Manacher() {
    int l = 0;
    ss[l++] = '$';
    ss[l++] = '#';
    for (int i = 0; i < len; i++) {
        ss[l++] = s[i];
        ss[l++] = '#';
    }
    ss[l] = 0;
    int MaxR = 0;
    int c = 0;
    for (int i = 1; i < l; i++) {
        p[i] = MaxR > i ? min(p[2 * c - i], MaxR - i) : 1;
        while (ss[i + p[i]] == ss[i - p[i]])
            p[i]++;
        if (i + p[i] > MaxR) {
            MaxR = i + p[i];
            c = i;
        }
        if(p[i] - 1 > ans){
        	ans = p[i] - 1;
        }
    }
}
int main()
{
	int cnt = 1;
	while(scanf("%s", s) != EOF && strcmp("END", s)){
		len = strlen(s);
		ans = 0;
		Manacher();
		printf("Case %d: %d\n", cnt, ans);
		++cnt;
	}
	return 0;
}

你可能感兴趣的:(Manacher)