1. 程式人生 > >【poj 1159】 Palindrome DP(類最長公共子序列)+滾動陣列

【poj 1159】 Palindrome DP(類最長公共子序列)+滾動陣列

Palindrome
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 58492 Accepted: 20318

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

Source
IOI 2000

題意:求一個序列改造構成迴文串最小步數;

思路:計算串與其逆串類最長公共子序列; ans=len-maxcom
*因為500*5000,dp[i][j]只與dp[i-1][]有關用滾動陣列優化

程式碼

#include<iostream>
#include<stdio.h> #include<string.h> using namespace std; int n; char s1[5005]; char s2[5005]; int dp[2][5005]; int main() { scanf("%d",&n); scanf("%s",s1+1); for(int i=1;i<=n;i++) s2[i]=s1[n-i+1]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(s1[i]==s2[j]) dp[i%2][j]=dp[(i-1)%2][j-1]+1; else dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]); } printf("%d\n",n-dp[n%2][n]); }

相關推薦

poj 1159 Palindrome DP(公共序列)+滾動陣列

Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 58492 Accepted: 20318 Description A palindrom

poj 1159 Palindrome(公共序列 + 滾動陣列)

http://poj.org/problem?id=1159 題意:給定一個字串,問最少插入多少個字元,使得該字串變成迴文字串。 思路:原字串序列是X,逆序列是Y,則最少需要補充的字母數=X的長度-X

poj 1159 Palindrome公共序列+滾動陣列

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 writ

1159 Palindrome(迴文串&LCS公共序列&滾動陣列)

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

HDU:1513 Palindrome(迴文字串+公共序列+滾動陣列

Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5246    Accepted Subm

公共序列滾動陣列寫法

如poj1159,普通寫法會mle滾動陣列可以解決,原因在於普通的dp寫法雖然一層套一層,但是實際上僅僅使用了該層和上一層,所以可以據此優化。PS.解釋圖片來自網路,圖片原本來自演算法導論#include<iostream> #include<cstdio&

HDU 1159 DP公共序列

類似於字典序比較的最長公共子序列, 只要找到狀態方程就比較好搞, 從角標1開始計算不會出錯,從0還是報錯了; 切防止對比溢位要在本來的基礎上+1計算; 言歸正傳: DP型別都是狀態方程很重要; 假設兩

HDU1159 Common Subsequence(DP公共序列

【HDU】1159 Common Subsequence (DP、最長公共子序列) 題目內容 Problem Description A subsequence of a given sequence is the given sequence

51nod 1183 編輯距離線性dp+類似公共序列

ima else ems 俄羅斯 hid ace mem std 類型 1183 編輯距離 基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 收藏 關註 編輯距離,又稱Levenshtein距離

jzoj5920. NOIP2018模擬10.21風箏(dp上升序列)

5920. 【NOIP2018模擬10.21】風箏 Description 當一陣風吹來,風箏飛上天空,為了你,而祈禱,而祝福,而感動…… Description oyiya 在 AK 了 IOI 之後來到了鄉下,在田野中玩耍,放鬆身心。 他發現前面有一排小朋

poj——1159dp公共序列

注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。 #include <iostream> #include <cmath> #include

POJ 1159 Palindrome 公共序列+滾動陣列

題目描述: A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You ar

HackerRankCommon Child (LCS)公共序列

lin ring def imp sep content hat jin ted Given two strings a and b of equal length, what’s the longest string (S) that can be construct

LeetCodeLongest Common Subsequence公共序列(求出某一解+LCS長度)

Longest Common Subsequence 給出兩個字串,找到最長公共子序列(LCS),返回LCS的長度。 說明 最長公共子序列的定義: • 最長公共子序列問題是在一組序列(通常2個)中找到最長公共子序列(注意:不同於子串,LCS不需要是

[poj 2274]後綴數組+公共序列

max %d eight har 題目 while color sca 鏈接 題目鏈接:http://poj.org/problem?id=2774 後綴數組真的太強大了,原本dp是n^2的復雜度,在這裏只需要O(n+m)。 做法:將兩個串中間夾一個未出現過的字符接起來,然

hdu1159-Common Subsequence(DP公共序列LCS)

dice com main sizeof accept pan nbsp any ++ Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (

HDU 1159:Common Subsequence(公共序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23108    Accepted Submission(s

mark公共序列poj 1458+hdu 1159

經典的問題,在各大部落格上有數不清的好帖子 下面為最常見的n^2演算法 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #inclu

poj 1159Palindrome 題意&題解&程式碼(C++)

題目連結: http://poj.org/problem?id=1159 題意: 給出一個長為n的字串,求最少新增幾個字元可以將這個字串變為迴文字串。 題解: 要把這個串變為迴文串,很容易想到

HDU 1159 基礎DP 公共序列

TAT,,,覺得自己不算太理解,但是居然可以憑著記憶做出來。。蠻拼的 #include <cstdio> #include <cstring> #include <iostream> #include <string> usi