/*
Largest panlindrome in a string
*/
#include <iostream>
void reverseStr(char s[])
{
int length = strlen(s);
int i = 0;
int j = length - 1;
char tmp;
while(i < j)
{
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
};
void findPanlindrome(char s1[], char s2[], char res[])
{
int mat[100][100];
int length = strlen(s1);
int maxVal = 0;
int maxI = -1;
memset(mat, 0, sizeof(mat));
for(int i = 1; i <= length; i++)
{
for(int j = 1; j <= length; j++)
{
if(s1[i] == s2[j])
{
mat[i][j] = mat[i - 1][j - 1] + 1;
if(mat[i][j] > maxVal)
{
maxVal = mat[i][j];
maxI = i;
}
}
}
}
int ind = 0;
for(int i = maxI - maxVal + 1; i <= maxI; i++)
{
res[ind] = s1[i];
ind++;
}
res[ind] = '\0';
};
int main()
{
char str1[20] = "bananaluck";
char str2[20];
strcpy(str2, str1);
reverseStr(str1);
char res[100];
findPanlindrome(str1, str2, res);
printf("Result: %s", res);
return 0;
}Bloomberg 05122012 [1]
最新推荐文章于 2021-11-11 02:52:09 发布
本文介绍了一种寻找字符串中最长回文子串的方法。通过将原字符串与其反转后的字符串进行比对,并利用动态规划算法来找出最长的相同字符序列,即为最长回文子串。文章提供了一个完整的C++实现示例。
1万+

被折叠的 条评论
为什么被折叠?



