输出描述:
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
输入例子:
aaaa abab
输出例子:
43
初始想法:中心扩展加‘#’可以不考虑偶情况
Code:
#include<stdio.h> #include<string.h> #include<stdlib.h> #define MAX 100 int main() { int i,j, len, tmp, max; char str[MAX], nstr[MAX], c; while (~scanf("%s", str)) { scanf("%c", &c); len = strlen(str); max = 0; for (i = 0; i < len; i++) { nstr[i * 2] = '#'; nstr[i * 2 + 1] = str[i]; } nstr[2 * len] = '#'; for (i = 0; i <= 2 * len; i++) { tmp = 0; for (j = 0; j <= i; j++) { if (nstr[i + j] != nstr[i - j]) break; } max = max > j ? max : j; } printf("%d\n", max - 1); } return 0; }
超时。
使用Manacher算法,待续。
Code: