求一字符串中最大回文串的长度
比如:[*]"AABB" 回文长度为: 4回文子串为: "AABB"
[*]"12abcba22" 回文长度为: 7回文子串为: "2abcba2"
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, char **argv){ if (argc != 2) { printf("usage: ./a.out <string>\n"); exit(EXIT_FAILURE); } char *s = argv; char *p; int max = 1; int i; for (p = s; *p != '\0'; p++) { for (i = 1; (p - i) >= s && *(p + i) != '\0'; i++) { if (*(p - i) != *(p + i)) break; } if (max < (i * 2 - 1)) max = (i * 2 - 1); for (i = 0; (p - i) >= s && *(p + i + 1) != '\0'; i++) { if (*(p - i) != *(p + i + 1)) break; } if (max < (i * 2)) max = (i * 2); } printf("max = %d\n", max); exit(EXIT_SUCCESS);}
页:
[1]