http://zhedahht.blog.163.com/blog/static/25411174201063105120425/
思路:
最简单的区间dp思想。
#include <iostream> using namespace std; int dp[100][100]; int solve(const char* s) { if (s == nullptr) return -1; int maxvalue = 1; int len = strlen(s); memset(dp, 0, sizeof(dp)); for (int i = 0; i < len; ++i) { dp[i][i] = 1; if (i + 1 < len && s[i] == s[i+1]) dp[i][i+1] = 2; } for (int d = 2; d < len; ++d) { for (int i = 0, j = d; j < len; ++i, ++j) { if (s[i] == s[j] && dp[i+1][j-1]) dp[i][j] = dp[i+1][j-1] + 2; if (maxvalue < dp[i][j]) maxvalue = dp[i][j]; } } return maxvalue; } int main() { char b[100] = "google"; cout << solve(b) << endl; return 0; }