5-12 最长对称子串 (25分) 对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
输入格式: 输入在一行中给出长度不超过1000的非空字符串。
输出格式: 在一行中输出最长对称子串的长度。
输入样例: Is PAT&TAP symmetric?
输出样例: 11
以下为accepted代码
#include <stdio.h>#include <string.h>int main(){ int i, j, t, flag, max, cnt; char st[1004]; while(gets(st) != NULL) { int len = strlen(st); max = 1, cnt = 1; for(t = 0; t < len; t++) { flag = 0, i = t, j = len-1; while(i < j) { if(st[i] == st[j]) { if(j - i == 2) flag++; i++, j--; flag += 2; } if(st[i] != st[j]) { j--; i = t; flag = 0; } } if(flag > max) { max = flag; cnt = 0; } } if(cnt) PRintf("%d/n", max); else printf("%d/n", max); } return 0;}新闻热点
疑难解答