1.链接地址:

http://bailian.openjudge.cn/practice/1159/

http://poj.org/problem?id=1159

2.题目:

Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 49849   Accepted: 17153

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

3.思路:

这题要知道其实最少增加的个数= 字符串总字数 - LCS(最长公共子序列)

所以就转化为求LCS

LCS为典型的dp算法之一,时间复杂度O(n^2),空间复杂度O(n)

!!!这题用string会超时,郁闷。开始对string没好感

4.代码:

 #include <iostream>
#include <cstdio>
#include <cstring> #define max(a,b) ((a) > (b) ? (a) : (b)) using namespace std; int same(char ch1,char ch2)
{
if(ch1 == ch2) return ;
else return ;
} int LCS(char *str1,char *str2,int len1,int len2)
{
int i,j; //if(len1 < len2) {char *str3 = str1;str1 = str2;str2 = str3;} int **dp = new int*[];
for(i = ; i < ; ++i) dp[i] = new int[len2 + ];
memset(dp[],,sizeof(int) * (len2 + ));
dp[][] = ; for(i = ; i <= len1; ++i)
{
for(j = ; j <= len2; ++j)
{
dp[i % ][j] = max(dp[(i - ) % ][j],max(dp[i % ][j - ],dp[(i - ) % ][j - ] + same(str1[i - ],str2[j - ])));
//cout<<"dp[" << i << "][" << j << "]=" << dp[i % 2][j] << endl;
}
}
int max = dp[len1 % ][len2]; for(i = ; i < ; ++i) delete [] dp[i];
delete [] dp; return max;
} int main()
{
int n;
cin>>n; char *str1 = new char[n];
char *str2 = new char[n]; int i;
for(i = ; i < n; ++i)
{
cin>>str1[i];
str2[n - - i] = str1[i];
} int lcs_len = LCS(str1,str2,n,n); cout<<(n - lcs_len)<<endl; delete [] str1;
delete [] str2;
return ;
}

OpenJudge/Poj 1159 Palindrome的更多相关文章

  1. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  2. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

  3. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

  4. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

  5. poj 1159 Palindrome(区间dp)

    题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...

  6. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  7. POJ 1159 Palindrome(最长公共子序列)

    http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...

  8. poj 1159 Palindrome 【LCS】

    任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...

  9. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...

随机推荐

  1. linux修改文件权限和用户组管理小结

    如何在linux下修改组权限 chmod g+r path/file 加读权限 当前目录 chmod -R g+r path/file 加读权限 当前目录以及子目录 g-r 减读权限g+w 加写权限g ...

  2. Hdu 5444 Elven Postman dfs

    Elven Postman Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  3. [VirtualBox] Install Ubuntu 14.10 error 5 Input/output error

    After you download the VirtualBox install package and install it (just defualt setting). Then you sh ...

  4. [AngularJS + cryptoJS + Gravatar] Provider vs factory

    Configurable Bits Need a Provider We want to be able to configure the characterLength before Tweetab ...

  5. PAT 1009

    1009. Product of Polynomials (25) This time, you are supposed to find A*B where A and B are two poly ...

  6. Android中的距离单位

    px 像素:每个px对应屏幕上面的一个点 dip或dp(device independent pixels 设备独立像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dip=1px.但 ...

  7. 四、Socket之UDP异步传输文件-用控件显示文件传输进度

    上一篇文章三.Socket之UDP异步传输文件中,实现了多文件的传输和MD5校验,还显示了文件传输过程中的信息,在这一篇文章中,将介绍怎样实现传输文件的进度显示和实现选择保存文件路径. 首先,来实现一 ...

  8. 实例源码--Android智能家居系统源码

      下载源码   技术要点:  1.Android应 用开发基础框架 2.SQLITE数据库的 使用 3.网络通信 4.GOOGLE地图模块 5.源码带有非常详 细的中文注释 ...... 详细介绍: ...

  9. extremeComponents(ec)源码分析

    eXtremeComponents(简称ec)是一系列提供高级显示的开源JSP定制标签,当前的包含的组件为eXtremeTable,用于以表形式显示数据. 其本质是jsp的自定义标签,抓住这一点就抓住 ...

  10. Android_gridVIew

    xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...