HDU 3294 Girls' research Manacher

#include<iostream>
#include<cstring>
using namespace std;

char s[200010];
char c;
int len;

void init()   //转换字符
{
	int e = c-'a';
	len = strlen(s);
	for(int i = 0; i < len; i++)
		s[i] = s[i]-e < 'a' ? s[i]-e+26 : s[i]-e;
}

int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a > b ? b : a; }

void Manacher()
{
	if(len == 1) 
	{
		printf("No solution!\n");
		return;
	}
	len = 2*len+1;
	char *cArr = new char[len];
	int *pArr = new int[len];
	for(int i = 0; i < len; i++)
		cArr[i] = i&1 ? s[(i-1)/2] : '#';
	int R = -1;  //最右界限
	int C = -1;   //最右界限对应的中心
	int maxn = 0;  //最大回文串长度(-1后)
	int index = -1; //最大回文串中心
	for(int i = 0; i < len; i++)
	{
		pArr[i] = i >= R ? 1 : min(R-i, pArr[2*C-i]);
		while(i+pArr[i] < len && i-pArr[i] > -1)
		{
			if(cArr[i+pArr[i]] == cArr[i-pArr[i]]) pArr[i]++;
			else break; 
		}
		if(i+pArr[i] > R)
		{
			R = i+pArr[i];
			C = i;
		}
		if(pArr[i] > maxn)
		{
			index = i;
			maxn = pArr[i];
		}
	}
	//输出
	if(maxn >= 3)
	{
		int l = (index-maxn+2)/2;
		int r = (index+maxn-2)/2;
		printf("%d %d\n", l, r);
		for(int i = l; i <= r; i++) printf("%c", s[i]);
		printf("\n");
	}
	else printf("No solution!\n");
	
	delete[] cArr;
	delete[] pArr;
}

int main()
{
	while(~scanf("%c %s", &c, s))  //~一定要加,吐了,T了无数次
	{
		getchar();
		init(); 
		Manacher();
	}
	return 0;
}
上一篇:Codeforces960G Bandit Blues


下一篇:Oracle拆分字符串,字符串分割的函数。