先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,LCS):不必连续   实在是汗颜,网上做一道题半天没进展: 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串.如何删除才能使得回文串最长呢?输出需要删除的字符个数. 首先是自己大致上能明白应该用动态规划的思想否则算法复杂度必然过大.可是对于回文串很难找到其状态和状态转移方程,换句话…
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a string s consisting of only lowercase English letters. He wants to change exactly onecharacter from the st…
转自: http://www.careercup.com/question?id=6287528252407808 问题描述: A k-palindrome is a string which transforms into a palindrome on removing at most k characters. Given a string S, and an interger K, print "YES" if S is a k-palindrome; otherwise pr…
题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> using namespace std; ; int n,m,t; short dp[maxn][maxn]; char s[maxn]; int main() { int i,j,k; #…
Uva 10453 题意:给定字符串,问最少插入多少个字符使其变成回文串,并任意输出一种结果. 题解:和Uva 10739类似,这里是只能增加.类似定义dp[i][j]表示子串Si...Sj变为回文串需要插入字符的最小数.当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1];当两者不相等时,dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1.然后dp出结果,dfs()输出答案. #include<iostream> #include<cstdio&…
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaaa&qu…
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文子串长度.对于求原串最长回文子串长度用的是DP的经典问题LCS最长公共子序列的做法. 设原串为S,原串的逆串为S‘,那么原串的最长回文子串长度=S和S'的最长公共子序列长度. 由于N的范围最大是5000,所以5000*5000的数组开不下,所以需要用到滚动数组来求.[关于原串求最长回文子串用的Man…
有点硬核的dp..要用到一个结论.. /* 把原串拆成奇偶串,再拆成极大连续的-1串:该串两端都是非-1数,中间都是-1,并且下标要么都是偶数,要么都是技术 然后对所有这些串进行dp,dp[i][0]表示到第i个-1的方案数,0表示第i个-1填的数和该串最右端的数不同,1表示相同 为什么这样是可行的? 一个重要结论:拆分成奇偶串就可以使不出现回文串的条件转化为相邻两个字符不相等 而相邻两个字符不相等的填数方案可以用dp来做,并且每一段不连续的-1段都是满足乘法原理的,段内就是递推 有个经典套路:…
链接:https://ac.nowcoder.com/acm/contest/330/I 来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如--判断一个字符串是不是回文串. 这样的题目未免让它觉得太无聊,于是它想到了一个新的问题. 如何判断一个字符串在任意位置(包括最前面和最后面)插入一个字符后能不能构成一个回文串? 输入描述: 仅一行,为一个由字母和数字组成的字符串 s. 输出描述: 如果在插入一个字符之后可以构成回文串,则输出"Yes", 否则输出&qu…
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 t…