P1435 [IOI2000] 回文字串
思路:首先考虑的是状态定义,因为它的长度是1000,所以我们可以考虑用f[i][j]表示为把i到j变为回文序列所需要添加的最小的数量,所以我们可以枚举长度,跟起点,那么对于某一个区间l,r来说,如果str[l]==str[r]那么代表两边的可以匹配上,只需要把中间的匹配上即可,所以f[l][r]=r[l+1][r-1],否则代表最外层的不能够匹配,那么我们需要在最左边放上一个或者在最右边放上一个让它匹配那么就是f[i][j]=min(f[i][j-1],f[i+1][j])+1(因为我们状态定义的是把i到j变为回文所需要添加的最少数量,所以f[i][j-1]与f[i+1][j]已经是一个回文串了)最后f[1][n]是答案
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<map>
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=4e5+7 ,M=2e7+7, INF=0x3f3f3f3f,mod=1e9+7;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}
int T,hackT;
int n,m,k;
char str[1010];
int f[1010][1010];
void solve() {
scanf("%s",str+1);
n=strlen(str+1);
for(int len=2;len<=n;len++) {
for(int i=1;i+len-1<=n;i++) {
int l=i,r=i+len-1;
if(str[l]==str[r]) f[l][r]=f[l+1][r-1];
else f[l][r]=min(f[l+1][r],f[l][r-1])+1;
}
}
printf("%d\n",f[1][n]);
}
int main() {
// init();
// stin();
// scanf("%d",&T);
T=1;
while(T--) hackT++,solve();
return 0;
}