题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[]){
string str;
getline(cin, str);
vector<vector<bool>> dp(str.size(), vector<bool>(str.size(), false));
for(int i = 0; i < str.size(); ++i){
dp[i][i] = true;
}
int maxlen = 1;
for(int i = str.size() - 2; i >= 0; --i){
for(int j = i + 1; j < str.size(); ++j){
if(j - i == 1){
dp[i][j] = (str[i] == str[j]);
}
else{
dp[i][j] = (str[i] == str[j] && dp[i+1][j-1]);
}
if(dp[i][j] && j-i+1 > maxlen){
maxlen = j-i+1;
}
}
}
cout << maxlen << endl;
return 0;
}