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

本文共 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

你可能感兴趣的文章
我的友情链接
查看>>
Git常用操作及分支
查看>>
关于一种求最大公约数的算法的分析与证明
查看>>
微信授权莫名创建用户数据失败的原因
查看>>
网络高手修身
查看>>
JavaWeb综合案例-键盘模拟
查看>>
Android Day03-SQLite数据库操作及ListView详解
查看>>
Looking for APAC Operations IT XML Database Developer in Shenzhen and Hongkong
查看>>
Myeclipse常用快捷键
查看>>
我的友情链接
查看>>
Unity3d多线程
查看>>
炉石传说 C# 开发笔记 (源代码整理公开)
查看>>
前端文摘:Web 开发模式演变历史和趋势
查看>>
最大子数组和问题的解
查看>>
cout设置输出数据不显示科学计数法
查看>>
zoj 1659 Mobile Phone Coverage(矩形面积并)
查看>>
python学习 day3
查看>>
Centos 6.4下用Squid配置反向代理多个内网WEB服务器
查看>>
王者荣耀之父姚晓光“奇葩”的工作理念
查看>>
Flask 信号
查看>>