博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Palindrome
阅读量:6171 次
发布时间:2019-06-21

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

Palindrome

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 88   Accepted Submission(s) : 30
Problem Description
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
 

 

Input
Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.
 

 

Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
 

 

Sample Input
5 Ab3bd
 

 

Sample Output
2
 代码:
1 #include
2 #include
3 const int MAXN=5010; 4 const int INF=0xfffffff; 5 #define MAX(x,y) (x>y?x:y) 6 int dp[MAXN][2]; 7 char a[MAXN],b[MAXN]; 8 int main(){ 9 int t,i,j;10 while(~scanf("%d",&t)){11 scanf("%s",a+1);12 //printf("%d\n",t);13 for(i=t,j=1;i>0;i--,j++)b[j]=a[i];14 b[j]='\0';15 //printf("%s\n",b+1);16 memset(dp,0,sizeof(dp));17 dp[0][0]=0;18 for(i=1;i<=t;i++){19 for(j=1;j<=t;j++){20 if(a[i]==b[j])dp[j][i%2]=dp[j-1][(i-1)%2]+1;21 else dp[j][i%2]=MAX(dp[j-1][i%2],dp[j][(i-1)%2]);22 }23 }24 //printf("%d\n",dp[t][t%2]);25 printf("%d\n",t-dp[t][t%2]);26 }27 return 0;}

题解;还可以是%2,%3,%4,........

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

你可能感兴趣的文章
前端构建工具gulp入门教程
查看>>
转载:爱加密40.98%占有率稳居应用加密市场榜首
查看>>
怎么退出 git bash vim编辑器
查看>>
64位Red Hat 6.5 修改yum源
查看>>
Android之人脸识别
查看>>
linux学习一天一个命令(19)[df命令]
查看>>
oracle 备份脚本
查看>>
Android AdapterView 源码分析以及其相关回收机制的分析
查看>>
About Your Third iOS App
查看>>
Linux CentOS 5.8 磁盘分区开机自动挂载
查看>>
L-1-9 Linux命令之用户日志查看命令
查看>>
SQL Server 2012入门安装篇:(2)服务组件及其功能介绍
查看>>
Centos6.5 (或Linux) 安装Elasticsearch
查看>>
MYSQL安全策略→▉收集整理▋
查看>>
使用枚举单例实现Xml、properties属性配置文件的操作
查看>>
深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
查看>>
CentOS 7下配置IP地址
查看>>
自己常用的编码转换工具。
查看>>
中国式排名也可以简简单单
查看>>
两段代码的比较 其实很多问题都可以简单化的
查看>>