UVA 10739 String to Palindrome(DP)

In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below:

Here you’d have the ultimate freedom. You are allowed to:

  • Add any character at any position
  • Remove any character from any position
  • Replace any character at any position with another character

Every operation you do on the string would count for a unit cost. You’d have to keep that as low as possible.

For example, to convert “abccda” you would need at least two operations if we allowed you only to add characters. But when you have the option to replace any character you can do it with only one operation. We hope you’d be able to use this feature to your
advantage.

Input

The input file contains several test cases. The first line of the input gives you the number of test cases, T (1≤T≤10). Then T test cases will follow, each in one line. The input for each test case consists of a string containing lower case letters only.
You can safely assume that the length of this string will not exceed 1000 characters.

Output

For each set of input print the test case number first. Then print the minimum number of characters needed to turn the given string into a palindrome.

Sample Input                               Output for Sample Input

6
tanbirahmed
shahriarmanzoor
monirulhasan
syedmonowarhossain
sadrulhabibchowdhury
mohammadsajjadhossain

Case 1: 5

Case 2: 7

Case 3: 6

Case 4: 8

Case 5: 8

Case 6: 8

题意:有三种操作,每种操作花费一单位花费。问最少的花费

区间DP:求最小花费。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
const int maxn=1100;
int dp[maxn][maxn];
char str[maxn];
int t;
int main()
{
     int cas=1;
     scanf("%d",&t);
     getchar();
     while(t--)
     {
         gets(str+1);
         int len=strlen(str+1);
         CLEAR(dp,INF);
         for(int i=1;i<=len;i++)
         {
             if(str[i]==str[i+1])
                dp[i][i+1]=0;
             dp[i][i]=0;
         }
         for(int l=1;l<=len;l++)
         {
             for(int i=1;i+l<=len;i++)
             {
                 int j=i+l;
                 if(str[i]==str[j])
                    dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
                 else
                 {
                    dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;
                    dp[i][j]=min(dp[i][j],dp[i+1][j-1]+1);
                 }
             }
         }
         printf("Case %d: ",cas++);
         printf("%d\n",dp[1][len]);
     }
     return 0;
}
时间: 2024-10-26 05:05:38

UVA 10739 String to Palindrome(DP)的相关文章

uva 10739 String to Palindrome (dp)

uva 10739 String to Palindrome In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below: Here you'd have the ultimate freedom. You are allowed to: Add any character at a

区间DP UVA 10739 String to Palindrome

题目传送门 1 /* 2 题意:三种操作,插入,删除,替换,问最少操作数使得字符串变成回文串 3 区间DP:有一道类似的题,有点不同的是可以替换,那么两端点不同的时候可以替换掉一个后成回文, 4 即dp[j+1][k-1] + 1,还有这道题没有要求打印 5 */ 6 /************************************************ 7 * Author :Running_Time 8 * Created Time :2015-8-17 15:45:22 9 *

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的. 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[i][j - 1]); 如果 i = j  dp[i][j] = 0; 14145138 10651 Pebble

UVa 10739 - String to Palindrome

题目:给你一个字符串,可以进行增删改三种操作,问变成回文串最少的操作次数. 分析:动态规划,dp,LCS.可以利用区间dp求解,这里利用LCS求解更快. 利用字符串和自己的翻转求最大公共子序列,然后枚举所有的dp[i][len-i], 找最小的即可.注意可能最小值在dp[i-1][len-i],即str[i]为中间元素,不用匹配. 说明:注意dp的初始化赋值. #include <cstring> #include <cstdio> int dp[1001][1001]; int

uva 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

POJ 3280 Cheapest Palindrome(DP)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j 这一段位置变成回文所需的最小的代价. 1 //3280 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 6 using namespace std ; 7 8 char sh[2100]

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible in

uva 10617 Again Palindrome (DP)

uva 10617 Again Palindrome 题目大意:给出一段字符串,可进行删除操作,可以删除任意位置任意个数(可以是0)的字符.问,进行删除操作使原本字符串变成回文字符串,有几种方式. 解题思路: dp[i][j] = 1 (i == j),单独一个字符也是回文字符串 s[i] != s[j]时, dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1], dp[i + 1][j] 和 dp[i][j + 1]的公共部分dp[

UVA - 10559 Blocks 和 Vasya and Binary String CodeForces - 1107E (dp OR 记忆化搜索)

UVA - 10559 Blocks 题意:消消乐,每次连续相同的可以消除,分数加上长度的平方,问最多可以获得几分全部消完 题解: 区间dp + 记忆化搜索 dp[i][j][k] : (区间 [i,  j] 后面带上一段和 j 颜色相同的且长度为 k )的消消乐最大积分 1.消最后一段颜色和 j 颜色相同的 dp[i][j][k] <-- dp[i][j-1][0] + (k+1)^2 2.对于i <= l < j, 如果 l 和 j 的颜色相同, 那么可以把 [l+1, j-1]消掉