#include <cstdio>
#include <cctype>
#include <cstring>
int main(){
char buff[50] = { "Confuciuss say: Madam, I'm Adam." };
printf("%s\n", buff);
int maxLen = 0, maxBeg = -1, maxEnd = -1;
for (int i = 0; i < strlen(buff)/* && isalpha(buff[i])*/; ++i){
for (int j = strlen(buff) - 1; j > i/* && isalpha(buff[j])*/; --j){
while (!isalpha(buff[j])) --j;
int beg = i;
int end = j;
bool isPalindrome = true/*beg < end ? true : false*/;
while (beg < end){
while (!isalpha(buff[beg])) ++beg;
while (!isalpha(buff[end])) --end;
if (tolower(buff[beg]) != tolower(buff[end])){
isPalindrome = false;
break;
}
++beg;
--end;
}
if (isPalindrome && j - i + 1 > maxLen){
maxBeg = i;
maxEnd = j;
maxLen = j - i + 1;
}
}
}
int i = maxBeg;
while (!isalpha(buff[i])){
++i;
--maxLen;
}
for (; i <= maxEnd; ++i){
printf("%c", buff[i]);
}
printf("\n");
printf("maxLen=%d\n", maxLen);
}
算法竞赛经典入门 例3-4最长回文串
最新推荐文章于 2021-01-31 17:56:47 发布