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. MySQL Error Handling in Stored Procedures---转载

    This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in store ...

  2. jQuery ajax 传递数组到struts2

    使用jQuery的$.ajax()方法进行异步交互时,如果传递的数据有数组(例如传输checkbox数据),Action中经常会接受不到数据. 此时应该注意一下data中数组的写法,例如: //组合成 ...

  3. SQL Server中的20个系统变量

    1.@@CONNECTIONS返回自上次启动 Microsoft SQL Server以来连接或试图连接的次数.示例:下面的示例显示了到当前日期和时间为止试图登录的次数.SELECT GETDATE( ...

  4. Myeclipse快捷键的设置

  5. 实用脚本 - - insertAfter 在现有元素后插入一个新元素

    function insertAfter(newElement,targetElement){ var parent = targetElement.parentNode; if(parent.las ...

  6. 介绍一个小工具 Linqer

    http://www.cnblogs.com/huangxincheng/archive/2011/05/12/2044990.html

  7. php函数serialize()与unserialize()

    serialize()和unserialize()在php手册上的解释是: serialize — Generates a storable representation of a value ser ...

  8. 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)

    委托与Lambda表达式   1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树   一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...

  9. Java线程(学习整理)--4---一个简单的生产者、消费者模型

     1.简单的小例子: 下面这个例子主要观察的是: 一个对象的wait()和notify()使用情况! 当一个对象调用了wait(),那么当前掌握该对象锁标记的线程,就会让出CPU的使用权,转而进入该对 ...

  10. 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L

    Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...