使用C /C++ 求最长回文子串
#include <iostream> using namespace std; //*s为字符串,n为字符串的长度 int LagPalindrome(char* str, int n) { int count = 0; int max = 0;//最长回文子串的长度 if (str == NULL || n < 1) { return 0; } for (int i = 0; i < n; i++) { //子串为奇数时 for (int j = 0; (i - j) >= 0 && (i + j) < n; j++) { if (str[i - j] != str[i + j]) { break; } count = 2 * j + 1; } if (count > max) { max = count; } //子串为偶数时 for (int k = 0; (i - k) >= 0 && (i + k + 1) < n; k++) { if (str[i - k] != str[i + k + 1]) { break; } count = 2 * k + 2; } if (count > max) { max = count; } } return max; } int main() { char str[] = "112233"; int n = strlen(str); int MaxLen; MaxLen = LagPalindrome(str, n); cout << "最长回文子串的长度是:" << MaxLen << endl; return 0; }