回文字符串

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
描述
所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba"。当然,我们给你的问题不会再简单到判断一个字符串是不是回文字符串。现在要求你,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串。
输入
第一行给出整数N(0<N<100)
接下来的N行,每行一个字符串,每个字符串长度不超过5000.
输出
每行输出所需添加的最少字符数
样例输入
1
Ab3bd
样例输出
2

分析:将原串逆转后求最长公共子序列 ,len-max 为结果
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std; char ss[];
char ss2[];
short dp[][];
int max(int a,int b){
if(a>b)return a;else return b;
}
int main()
{
int n;int i,j;
while(scanf("%d",&n)!=EOF) scanf("%s",ss);
int len=strlen(ss);
for(i=;i<len;i++){
ss2[len--i]=ss[i];
}ss2[len]=; for(i=;i<=len;i++){
for(j=;j<=len;j++){
dp[i][j]=;
}
}
for(i=;i<len;i++){
for(j=;j<len;j++){
if(ss[i]==ss2[j]){
dp[i+][j+]=dp[i][j]+;
}else
dp[i+][j+]=max(dp[i+][j],dp[i][j+]);
}
}
printf("%d\n",len-dp[len][len]);
return ;
}

回文字符串的变形——poj1159的更多相关文章

  1. NYOJ-37 回文字符串 —— LCS变形

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=37 题解: 一开始想从两边向中间添加字符,发现这样不是最优的.因为加入字符之后,这些原本存 ...

  2. 51Nod - 1092 回文字符串(添加删除字符LCS变形)

    回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca, ...

  3. (DP)51NOD 1006 最长公共子序列&1092 回文字符串

    1006 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abc ...

  4. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. 回文字符串的判断!关于strlen(char * str)函数

    #include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...

  6. NYOJ_37.回文字符串 (附滚动数组)

    时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...

  7. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

    131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...

  8. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  9. 【又见LCS】NYOJ-37 回文字符串

    [题目链接] 回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...

随机推荐

  1. FlashFXP 4.3.1 注册码

    FlashFXP 4.3.1 注册码 --------start--------- FLASHFXPwQAOlhkgwQAAAAC6W5MNJwTnsl73nIraAU149tnCQS0hmZU3GG ...

  2. Qt5.3中qml ApplicationWindow设置窗口无边框问题

    这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...

  3. 详解offset

    offset offset 译为“偏移量”,是javascript很重要的一个概念.涉及到便宜量的主要有offsetLeft.offsetTop.offHeight.offsetWidth这四个属性还 ...

  4. poj3686

    题解: KM算法 把每一个点拆成n个 然后改变编圈 代码: #include<cstdio> #include<cmath> #include<cstring> # ...

  5. Linux服务器上安装tomcat

    安装软件 : apache-tomcat-9.0.0.M1.tar.gz(下载地址http://tomcat.apache.org/) 步骤一 Tomcat是其中一个开源的且免费的java Web服务 ...

  6. idea git tag 管理

    项目release 之后一般都会打一个tag 做记录.本人使用idea管理tag的时候,遇到的问题做一些记录. 1:idea 创建tag idea 创建tag ,我们可以右键项目,然后按照下图操作创建 ...

  7. 源码编译tmux

    (1)clone 源代码仓库: $ git clone https://github.com/tmux/tmux.git (2) 编译之前先安装libevent,去官网下载tar包: http://l ...

  8. C#设置System.Net.ServicePointManager.DefaultConnectionLimit,突破Http协议的并发连接数限制

    在Http协议中,规定了同个Http请求的并发连接数最大为2. 这个数值,可谓是太小了. 而目前的浏览器,已基本不再遵循这个限制,但是Dot Net平台上的 System.Net 还是默认遵循了这个标 ...

  9. mac 使用 pf 做端口转发

    Mac os中我发现直接输入localhost是拒绝访问的,原因在于OSX 对于1024内端口需要 root 权限,因此需要做一个80端口的转发. 曾经的 ipfw 已经被 pf 所替换. 首先我们要 ...

  10. 安装Spring报错An error occurred while collecting items to be installed

    原因主要是eclipse和spring版本之间的匹配问题. An error occurred while collecting items to be installed session conte ...