Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 48526   Accepted: 16674

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

题意:给一个字符串的长度和这个字符串s[],问将这个字符串变成回文串至少要加几个字符;

思路:将这个字符串逆置s1[],求出s[]和s1[]的最长公共子序列的长度len,然后n-len即是要求的长度;
 #include<stdio.h>
#include<string.h>
short dp[][];
int main()
{
int i,j,n;
char s[],s1[];
memset(dp,,sizeof(dp)); scanf("%d",&n);
scanf("%s",s); for(int i = ; i < n; i++)
s1[i] = s[n--i];
s1[n] = '\0'; for(i = ; i < n; i++)
{
for(j = ; j < n; j++)
{
if(s[i] == s1[j])
dp[i+][j+] = dp[i][j]+;
else
{
if(dp[i+][j] > dp[i][j+])
dp[i+][j+] = dp[i+][j];
else dp[i+][j+] = dp[i][j+];
}
}
}
int ans = n-dp[n][n];
printf("%d\n",ans);
return ;
}

Palindrome(最长公共子序列)的更多相关文章

  1. POJ1159——Palindrome(最长公共子序列+滚动数组)

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

  2. POJ 1159 Palindrome 最长公共子序列的问题

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

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

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

  4. POJ 1159:Palindrome 最长公共子序列

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 Desc ...

  5. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  6. HDU 1513 Palindrome(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

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

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

  8. LCS最长公共子序列~dp学习~4

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...

  9. poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53414   Accepted: 18449 Desc ...

随机推荐

  1. IOS-NSDateFormatter使用介绍

    IOS-NSDateFormatter使用介绍 NSDateFormatter的使用: NSDate *nowDate = [[NSDate alloc] init]; NSDateFormatter ...

  2. 关于struts2中action请求会执行两次的问题

    关于struts2中action请求会执行两次的问题     在struts2中发现,调用action中的方法,方法会被执行两次,后来发现调用的方法是get开头的,把它改为其他名称开头的后,就不会执行 ...

  3. css考核点整理(九)-有几种文字替换方式,之间的优缺点

    有几种文字替换方式,之间的优缺点

  4. URLConnection类详解

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3753224.html ...

  5. Erlang - Download and Install for Linux

    1. 下载 Erlang [huey@huey-K42JE erlang]$ wget http://www.erlang.org/download/otp_src_R16B03.tar.gz 2. ...

  6. Shell - 文件运算符

    文件运算符  文件运算符  描述 -b file  检测 file 是否为块设备文件 -c file  检测 file 是否为字符设备文件  -d file  检测 file 是否为目录 -e fil ...

  7. [原创] SQLite数据库使用清单(下)

    上文两章对SQLite的功能.语法.和操作进行了介绍,本文讲解SQLite的一些高级语法和操作. 3.

  8. java可变参数Varargs

    http://www.cnblogs.com/shishm/archive/2012/01/31/2332656.html J2SE 1.5提供了“Varargs”机制.借助这一机制,可以定义能和多个 ...

  9. oracle中不曾熟悉的 to_char、to_number(未完待续)

    十进制       十六进制88               58 用法一:Converts a HEX number  to  o FLOAT (转换一个十六进制数的浮标) SQL> sele ...

  10. http错误代码含义大全详解

    http 错误代码表 所有 HTTP 状态代码及其定义.  代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理, ...