好恶心.........
/*
ID: wgh86791
LANG: C
TASK: calfflac
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
char s[20001]; //原字符串
char t[40005]; //处理后的字符串
int l[40005]; //t字符串中回文串的半径
int d[40005]; //t字符串中字符对应原串的位置
int min(int x, int y)
{
return x > y ? y : x;
}
int main(void)
{
FILE * in = fopen("calfflac.in", "r");
FILE * out = fopen("calfflac.out", "w+");
s[0] = '\0';
while( fgets(t, 20001, in) != NULL)
strcat(s, t);
int i, j, cnt;
t[0] = '@'; //防止越界用的
t[1] = '#';
cnt = 2;
//处理字符串
for(i=0; i<strlen(s); i++)
{
if( !isalpha(s[i]))
continue;
t[cnt] = toupper(s[i]);
d[cnt] = i;
cnt++;
t[cnt] = '#';
cnt++;
}
t[cnt++] = '\0';
int max=0, pos, index, ans=0;
for(i=1; i<strlen(t); i++)
{
if(max > i)
l[i] = min(l[2*index-i], max-i);
else
l[i] = 1;
for(; t[i+l[i]] == t[i-l[i]]; l[i]++);
if(l[i]+i > max)
{
max = l[i] + i;
index = i;
}
if(l[i] > ans)
{
ans = l[i];
pos = i;
}
}
for(i = pos - ans + 1; !isalpha(t[i]); i++); //找到原串中最大回文串的首地址
for(j = pos + ans - 1; !isalpha(t[j]); j--); // 尾地址
fprintf(out, "%d\n", ans-1);
for(i = d[i]; i <= d[j]; i++)
fprintf(out, "%c", s[i]);
fprintf(out, "\n");
return 0;
}