博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LightOJ 1033 Generating Palindromes(dp)
阅读量:6248 次
发布时间:2019-06-22

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

LightOJ 1033  Generating Palindromes(dp)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/A

题目:

Description

By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.

Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length n, no more than (n - 1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome "abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are only allowed to insert characters at any position of the string.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a string of lowercase letters denoting the string for which we want to generate a palindrome. You may safely assume that the length of the string will be positive and no more than 100.

Output

For each case, print the case number and the minimum number of characters required to make string to a palindrome.

Sample Input

6

abcd

aaaa

abc

aab

abababaabababa

pqrsabcdpqrs

Sample Output

Case 1: 3

Case 2: 0

Case 3: 2

Case 4: 1

Case 5: 0

Case 6: 9

题目大意:

给出一个字符串,求至少添加多少个元素使其变成回文串。

分析:

动态规划,(dp),从起点和终点开始往中间进行dp

状态转移方程:

当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1];

当s[i]!=s[j]时,dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;

代码:

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 7 char s[105]; 8 int dp[105][105]; 9 10 int min(int a,int b)//输出最小值11 {12 return a>b?b:a;13 }14 15 int main()16 {17 int t;18 int n,m=1;19 scanf("%d",&t);20 while(t--)21 {22 scanf("%s",s+1);//从数组下标为1的位置开始23 int n=strlen(s+1);//输入字符串的长度24 memset(dp,0,sizeof(dp));25 for(int i=n;i>=1;i--)//从两边开始搜索26 for(int j=i+1;j<=n;j++)27 {28 if(s[i]==s[j]) //s[i]与s[j]相等,搜索下一个29 dp[i][j]=dp[i+1][j-1];30 else //s[i]与s[j]不相等,取一个最小值再加131 dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;32 }33 printf("Case %d: %d\n",m++,dp[1][n]);34 }35 return 0;36 }

 

 

 

 

转载于:https://www.cnblogs.com/ttmj865/p/4735966.html

你可能感兴趣的文章
在 Windows Server 2008 中部署带 SignalR 的网站出错
查看>>
A glance for agile method
查看>>
Java高级教程:Java并发性和多线程
查看>>
Android更新带进度条的通知栏
查看>>
Python XML解析
查看>>
五步搭建属于自己的个人网站
查看>>
换今日特价图片---轻开电子商务系统(企业入门级B2C站点)
查看>>
任务调度利器:Celery
查看>>
利用java mail发送邮件(转)
查看>>
Mybatis(六) Spring整合mybatis
查看>>
教您用Xshell快速连接远程电脑
查看>>
怎样阅读源码
查看>>
RxJava系列之中的一个 初识Rxjava
查看>>
智能巡检资料
查看>>
cocos2dx-3.1 接入多盟广告sdk+Android (2)
查看>>
Android Eclipse 导入 AS Gradle AAR 库手冊
查看>>
腾讯实习生三面
查看>>
CTFcrackTools-V3 - 一款旨在帮助 CTFer 在 CTF 中发挥作用的一个框架
查看>>
weblogic隐藏版本号教程(10.3.6为例)
查看>>
Html input 常见问题
查看>>