博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电1513--Palindrome(滚动数组+LCS)
阅读量:7066 次
发布时间:2019-06-28

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

Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4062    Accepted Submission(s): 1384

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
 

 

Source
 

 

Recommend
linle   |   We have carefully selected several similar problems for you:            
RE:怎么把一个字符串变为回文串? → → 反转后求LCS; 为了不爆内存用滚动数组,吐个槽: 这题WA到恶心。
   滚动数组:
1 #include 
2 #include
3 #include
4 using namespace std; 5 char a[5050], b[5050]; 6 int dp[2][5050]; 7 int main() 8 { 9 int n;10 while(~scanf("%d", &n))11 {12 scanf("%s", b);13 strcpy(a, b);14 strrev(b);15 memset(dp, 0, sizeof(dp)); //+F+++U++++K++16 for(int i = 1; i <= n; i++)17 for(int j = 1; j <= n; j++)18 {19 if(a[i-1] == b[j-1])20 dp[i%2][j] = dp[(i-1)%2][j-1] + 1;21 else22 dp[i%2][j] = max(dp[(i-1)%2][j], dp[i%2][j-1]);23 }24 printf("%d\n", n - dp[n%2][n]);25 }26 return 0; 27 }

 

 

转载于:https://www.cnblogs.com/soTired/p/4719176.html

你可能感兴趣的文章
spring学习笔记(4)依赖注入详解
查看>>
菜鸟学自动化测试(五)-----selenium命令之定位页面元素
查看>>
【SICP练习】64 练习2.35
查看>>
PSK星座对象(constellation.cc)
查看>>
Linux链接脚本学习--lds
查看>>
Android将list数据通过LitePal保存到本地(集合保存到本地)
查看>>
hdu 1285 确定比赛名次
查看>>
Hackshanghai 黑马
查看>>
win7 Ubuntu双系统重装win7后Ubuntu引导消失
查看>>
Java 混淆那些事(二):认识 ProGuard GUI
查看>>
js中继承的几种实现方式
查看>>
Kotlin开发遇到问题汇总
查看>>
理解原型其实是理解原型链
查看>>
ES6语法(二) 函数
查看>>
《编程珠玑》读书笔记(2,3)
查看>>
this 绑定题目简析
查看>>
iOS标准库中常用数据结构和算法之查找
查看>>
每天学习2小时,17年前端经验分享,让你前端之路不再迷茫
查看>>
node学习记录(1)
查看>>
Eureka微服务实战-服务提供者
查看>>