您的位置:首页 > 其它

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

2017-02-06 21:08 309 查看
题目描述

回文串就是正反着看都一样的串(如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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: