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

  1. 5
  2. Ab3bd

Sample Output

  1. 2
  2.  
  3. 题意:给一个字符串的长度和这个字符串s[],问将这个字符串变成回文串至少要加几个字符;
  4.  
  5. 思路:将这个字符串逆置s1[],求出s[]和s1[]的最长公共子序列的长度len,然后n-len即是要求的长度;
  1. #include<stdio.h>
  2. #include<string.h>
  3. short dp[][];
  4. int main()
  5. {
  6. int i,j,n;
  7. char s[],s1[];
  8. memset(dp,,sizeof(dp));
  9.  
  10. scanf("%d",&n);
  11. scanf("%s",s);
  12.  
  13. for(int i = ; i < n; i++)
  14. s1[i] = s[n--i];
  15. s1[n] = '\0';
  16.  
  17. for(i = ; i < n; i++)
  18. {
  19. for(j = ; j < n; j++)
  20. {
  21. if(s[i] == s1[j])
  22. dp[i+][j+] = dp[i][j]+;
  23. else
  24. {
  25. if(dp[i+][j] > dp[i][j+])
  26. dp[i+][j+] = dp[i+][j];
  27. else dp[i+][j+] = dp[i][j+];
  28. }
  29. }
  30. }
  31. int ans = n-dp[n][n];
  32. printf("%d\n",ans);
  33. return ;
  34. }

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. jQuery日期联动插件

    此版本为网上的日期联动插件修改版,加入了修改后事件 /* * jQuery Date Selector Plugin * 日期联动选择插件 * * Demo: $("#calendar&qu ...

  2. 10.4 noip模拟试题

    题目名称 PA 青春 三部曲 名称 huakai taritari truetears 输入 huakai.in taritari.in truetears.in 输出 huakai.out tari ...

  3. JAVA跑马灯实现1

    <TextView        android:layout_width="wrap_content"        android:layout_height=" ...

  4. linux教程:配置Tomcat开机启动

    我们在linux下安装好tomcat之后:经常是需要配置到开机启动的: 这样的话就不需要我们每次重启linux服务器之后自己在登陆运行startup.sh文件启动tomcat了 本次的演示环境是在ce ...

  5. Android开发手记(30) 触摸及手势操作

    触摸操作在现在智能手机系统中起到举足轻重的作用,本文将对安卓中的触摸以及一些简单手势的操作进行简单的介绍. 1.触摸 首先是关于触摸的判断,有两种方法可以判断的触摸操作. (1)setOnTouchL ...

  6. Android开发--二维码开发应用(转载!)

    android项目开发 二维码扫描   基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤   首先需要用到google提供的zxin ...

  7. SQL存储过程基于字段名传入的字符串拼接.

    --定义存储过程. Create PROCEDURE Usp_Static ), ), --分组字段. ), --统计字段. ), --表头字段. ) --聚会函数. AS ) --存储游标执行的列. ...

  8. 关于百度 UEditor的使用

    1.文件路径的配置: 注意:在页面上需要指定editor文件所在的路径,否则报错 后面有时间,再说说 kindEditor和  bootstrap3的summernote的  Editor,  fck ...

  9. C# Activator.CreateInstance()

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  10. Datatables+Bootstrap

    http://sandbox.runjs.cn/show/thwac3ec 运行效果 <!DOCTYPE html> <html lang="en"> &l ...