1. 程式人生 > >poj——1159(dp之最長公共子序列)

poj——1159(dp之最長公共子序列)

注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define INF 0xfffffff
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
#define N 5002
int n;
char a[N],b[N];
short int opt[N][N];
int The_most_common_conqu(){
	for (int i=0;i<n+5;i++)
	  	opt[i][0]=opt[0][i]=0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
		   if (a[i-1]==b[j-1])
			opt[i][j]=opt[i-1][j-1]+1;
		   else
			opt[i][j]=opt[i-1][j]>opt[i][j-1]?opt[i-1][j]:opt[i][j-1];
	return opt[n][n];
}
int main()
{
        int i,j,k,t;
        int m,num,count;
        while(scanf("%d",&n)!=-1){
        	cin>>a;
        	int q=0;
        	for(i=n-1;i>=0;i--){
	        	b[q++]=a[i];
	        }
	        b[q]='\0';
	        cout<<n-The_most_common_conqu()<<endl;
        }
        return 0;
}


相關推薦

poj——1159dp公共序列

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

HDU 1159DP公共序列

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

【HDU】1159 Common SubsequenceDP公共序列

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

hdu1159-Common SubsequenceDP公共序列LCS

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

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

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

POJ 1458 - Common Subsequence公共序列 題解

void 方式 mem strong 輸出 inline ron eof init 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=1458 題目大意: 有若幹組數據,每組給出兩個字符

動態規劃公共序列LCS

int tdi -s can 數組下標 include har 遞推 最長公共子序列 在字符串S中按照其先後順序依次取出若幹個字符,並講它們排列成一個新的字符串,這個字符串就被稱為原字符串的子串 有兩個字符串S1和S2,求一個最長公共子串

學習筆記--NLP文字相似度LCS公共序列

最長公共子序列 一個序列S任意刪除若干個字元得到的新序列T,則T叫做S的子序列 兩個序列X和Y的公共子序列中,長度最長的那個,定義為X和Y的最長公共子序列  例如:      --字串12455與245576的最長公共子序列為2455      --字串acd

動態規劃公共序列C++原始碼

動態規劃之最長公共子序列 問題: 在序列X={x1,x2,…,xm}與Y={y1,y2,…,yn}中查詢長度最長的公共子序列,往往不是一個。例如:X={A,B,C,B,D,A,B},Y={B,D,C,A,B,A},則公共子序列有Z={B,C,B,A},Z1={B

poj公共序列公共

Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X =

hdu1513Palindrome動態規劃公共序列變形+滾動陣列

2題意: 就是填多少字元使之變成迴文字串;#include<iostream> #include<cstring> #include<queue> #include<cstdio> #include<string.h> #include<al

Common Subsequence DP公共序列

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ...,

POJ 1458公共序列

Time Limit: 1000MS Memory Limit: 10000K Description A subsequence of a given sequence is the given sequence with some elemen

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

HDU 1159 Common Subsequence公共序列

大概題意:給出兩個字串s1,s2,求他們的最長公共子序列長度... 大概算是模板題? #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&

演算法導論——動態規劃公共序列LCS迴文序列LPS

有兩個字串A和B,假設為A=”abcbdab”,B=”bdcaba”;最長公共子序列(LCS)問題指的時找到A和B的一個公共的子串C,C的長度要是最長。 在這裡我們很明顯的發現最長子序列為”bcba” 用動態規劃的思想來考慮這個問題: 若A={a1,a2,

1159——Common Subsequence 公共序列

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ...,

動態規劃入門公共序列LCS

LCS是動態規劃在字串問題中應用的典型。問題描述:給定2個序列,求這兩個序列的最長公共子序列,不要求子序列連續。例如{2,4,3,1,2,1}和{1,2,3,2,4,1,2}的結果是{2,3,2,1}或者{2,4,1,2}。 思路:如果不用動態規劃去做,而用暴力法,則必須找

HDU——1159 Common Subsequence(DP 公共序列

Problem Description   A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a

筆試題學習dp,重疊問題,卡特蘭數,手電過橋,公共序列

卡特蘭數:https://blog.csdn.net/doc_sgl/article/details/8880468 dp,重疊子問題:https://www.cnblogs.com/hapjin/p/5572483.html 美團2016校招筆試題:https://zhuanlan.zhihu.com/p/