HDU 3068 最长回文

来源:互联网 发布:mac os sierra 编辑:程序博客网 时间:2024/05/29 07:01
输出描述:
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.       

输入例子:
aaaaabab

输出例子:
4

3

初始想法:中心扩展加‘#’可以不考虑偶情况

Code:

#include<stdio.h>#include<string.h>#include<stdlib.h>#define MAX 100int 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: