题解 | 密码截取
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
#include <iostream>
#include<bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
string s;
cin >> s;
int leghth_max = 1;
for (int index = 1; index < s.size(); index++) {
int leghth1 = 1;
for (int i = index + 1, j = index - 1; i < s.size() && j >= 0; i++, j--) {
if (s[i] == s[j])leghth1 += 2;
else break;
}
int leghth2=0;
for(int i=index,j=index-1; i < s.size() && j >= 0; i++, j--){
if (s[i] == s[j])leghth2 += 2;
else break;
}
leghth_max = max(leghth_max, max(leghth1,leghth2));
}
cout << leghth_max;
}
// 64 位输出请用 printf("%lld")
动规已然忘了怎么写,但思路还在,单中心和双中心,分两次循环。这一刻才感受到知识真的学到自己手里了,写的是自己思路的代码而不是背。

传音控股公司福利 333人发布