HDU-3294 Girls‘ research

模板题,修改字符串和求出最大回文串中点和半径。

#include 
#include 
#include 
using namespace std;
const int N = 1e6 + 5;
char s[N], ss[N];
int p[N], len, index;
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, cmp = 1;
	index = -1;
    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 > cmp){
        	index = i;
        	cmp = p[i] - 1;
        }
    }
}
int main()
{
	char ch;
	while(scanf("%c %s", &ch, s) != EOF){
		getchar();
		len = strlen(s);
		for(int i = 0; i < len; ++i){
			int t = ch - 'a';
			s[i] = ((s[i] - ch) + 26) % 26 + 97;
		}
		Manacher();
		if(index == -1){
			printf("No solution!\n");
		}
		else{
			int t = p[index] - 1;
			int l = ((index - t) - 1) / 2, r = ((index + t) - 1) / 2 - 1;
			printf("%d %d\n", l, r);
			for(int i = l; i <= r; ++i){
				printf("%c", s[i]);
			}
			printf("\n");
		}
	}
	return 0;
}

你可能感兴趣的:(Manacher)