【POJ1159】【DP】17.2.6 T1 强迫症 题解

题目描述
回文串就是正反着看都一样的串(如abba,acbca)。jyb 不幸得了回文串强迫症,看到一个串就想把它变成回文串。jyb 可以在串中任意位置插入字符。例如Ab3bd,插入2 个字符可以变dAb3bAd 或者 Adb3bdA。现在jyb 想知道,最少插入多少个字符,才能把一个串变成回文串呢?
【输入格式】
一个包含大小写字母和数字的字符串s,区分大小写。
【输出格式】
一个整数,最少插入的字符数量。
【输入样例】
Ab3bd
【输出样例】
2
【数据规模】
20% 数据满足 s 的长度 ≤10。
100%数据满足s 的长度 ≤5000。

题目类似POJ1159(几乎一样)
此题直接正反相互求一遍LCS就完了,水题。(虽然没有一遍A,zz…)

附AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))

using namespace std;

const int MaxLen = 5005;
char a[MaxLen],b[MaxLen];
int len;
int DP[MaxLen][MaxLen];

int main(){
    freopen("ocd.in","r",stdin);
    freopen("ocd.out","w",stdout);
    gets(a+1); len = strlen(a+1);
    for(int s = 1; s < len; s++)
    {
        for(int i = 1; i <= len-s; i++)
        {
            int j = i+s;
            DP[i][j] = 10000;
            if(a[i] == a[j]) DP[i][j] = DP[i+1][j-1];
            DP[i][j] = min(DP[i][j], min(DP[i+1][j]+1, DP[i][j-1]+1));
        }
    }
    cout << DP[1][len] << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值