[IOI2000] 回文字串 / [蓝桥杯 2016 省] 密码脱落 - 洛谷
/*
* @Description: To iterate is human, to recurse divine.
* @Autor: Recursion
* @Date: 2022-04-08 11:29:24
* @LastEditTime: 2022-04-08 11:52:37
*/
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
string s1;
string s2;
int dp[1001][1001];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s1;
s2.assign(s1.rbegin(),s1.rend());
//cout << s1 << endl << s2 << endl;
for(int i = 1;i <= s1.length();i ++)
for(int j = 1;j <= s1.length();j ++){
if(s1[i - 1] == s2[j - 1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
cout << s1.length()-dp[s1.length()][s1.length()] << endl;
return 0;
}
该博客主要讨论了字符串处理问题,通过一个IOI2000年的题目和蓝桥杯2016省赛的题目展示了如何使用动态规划来求解回文子串的最大长度。代码中定义了一个二维动态规划数组,通过比较字符是否相等更新最长公共子序列的长度。最终输出的是原始字符串与反转字符串最长公共子串的长度差,即非回文子串的长度。
526

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



