POJ1159】的更多相关文章

Palindrome DescriptionA 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 inser…
题目链接: https://cn.vjudge.net/problem/POJ-1159 题目大意: 题意很明确,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串. 解题思路: 设原序列S的逆序列为S' 最少需要补充的字母数 = 原序列S的长度 —  S和S'的最长公共子序列长度 采用滚动数组节省空间 #include<cstdio> #include<cstring> #include<algorithm> #include&l…
题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]==str[j]时)} dp[i][j]表示第i个字符到第j个字符构造成回文串最少添加的字符,但discuss里说了 这样做会超空间+超时 观察下这个方程,每次用到的就是那三个子状态,于是适当的改变方程的形式: dp[i][j]表示第i个字符开始长度为j的子串构成的字符串最少需要添加的字符,于是方程变…
//Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1) (s1[i]==s2[j]) #include <cstdio> #include <cstring> #include <iostream> using namespace std; ; ][imax_n]; char s1[imax_n];…
题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题卡内存真稀奇,于是修改成滚动数组.观察发现i值的更新只有可能是从i或i-1转移来,所以就i取模2. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリキリ♂ mind! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃…
题目大意 给定一个字符串S,问最少插入多少个字符可以使字符串S变为回文串 题解 用dp[i][j]表示把字符串s[i-j]变为回文串需要插入的最小字符数 如果s[i]==s[j]那么dp[i][j]=dp[i+1][j-1] 如果s[i]!=s[j]那么dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1 可以用滚动数组优化一下空间 代码: #include<cstdio> #include<algorithm> #include<cstring>…
G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   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 w…
题目链接. 分析: 感叹算法的力量. 方法一: 设 dp[i][j] 为字符串 s, 从 i 到 j 需要添加的最少字符数. 那么如果 s[i] == s[j], dp[i][j] = dp[i+1][j-1]. 如果 s[i] != s[j], dp[i][j] = min(dp[i+1][j], dp[i][j-1]) + 1. #include <iostream> #include <cstdio> #include <cstdlib> #include <…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 58277   Accepted: 20221 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…
H - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A palindrome is a symmetrical string, that is, a string read identically from left to right as…