博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 10739 - String to Palindrome(带增删改操作的回文串问题)
阅读量:4035 次
发布时间:2019-05-24

本文共 2796 字,大约阅读时间需要 9 分钟。

1、

2、题目大意:

给定一个字符串,可以对其做以下三种操作,增加一个字符,删除一个字符,替换一个字符,求出使得该字符串成回文串的最小操作步数,每种操作执行一次算一步

3、DP(x,y)表示比较到下标是x和下标是y时的最小的步数,最终结果为DP(0,len-1),正着和反着比较

if(str[x]==str[y])

dp[x][y]=DP(x+1,y+1);

if(str[x]!=str[y])

做删除操作,状态1+min(DP(x+1,y),DP(x,y-1))

做添加操作,状态1+DP(x+1,y-1)

做替换操作,状态1+DP(x+1,y-1)

4/AC代码:

#include
#include
#define N 1005#include
using namespace std;char str[N];int visit[N][N];int dp[N][N];int DP(int x,int y){ if(visit[x][y]) return dp[x][y]; else if(x>=y) { visit[x][y]=1; dp[x][y]=0; return 0; } else { if(str[x]==str[y]) { dp[x][y]=DP(x+1,y-1); visit[x][y]=1; } else { dp[x][y]=1+min(min(DP(x+1,y),DP(x,y-1)),DP(x+1,y-1)); visit[x][y]=1; return dp[x][y]; } }}int main(){ int t,cas=0; scanf("%d",&t); getchar(); while(t--) { cas++; memset(visit,0,sizeof(visit)); memset(dp,0,sizeof(dp)); gets(str); int len=strlen(str); int ans=DP(0,len-1); printf("Case %d: %d\n",cas,ans); } return 0;}/*6tanbirahmedshahriarmanzoormonirulhasansyedmonowarhossainsadrulhabibchowdhurymohammadsajjadhossain*/

4、题目;

Problem H

String to Palindrome

Input: Standard Input

Output: Standard Output

Time Limit: 1 Second

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


Problem setter: Monirul Hasan, EPS

 

转载地址:http://dhddi.baihongyu.com/

你可能感兴趣的文章
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Word Break(python)
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
windows下 pip安装
查看>>
线程和进程
查看>>
UNIX 学习笔记-文件I/O(open)
查看>>
UNIX学习笔记-文件I/O--(creat,close)
查看>>
UNIX学习笔记--(lseek)
查看>>
java swing最简单实例(1) 一个空的JFrame
查看>>
java swing最简单实例(2) 往JFrame里面放一个容器或组件
查看>>
java自定义容器排序的两种方法
查看>>
进化论中的概率论 进化13个字母序列的无差期望值公式
查看>>
网络英语 第一章
查看>>
搞笑短信用英文写起来啥样呢……
查看>>