1. 程式人生 > >poj 3280

poj 3280

color stream 一中 ace poj pan ems names ring

考察i~j之間的元素,據說是lcs,不懂,好像以前做過uva的類似,但全忘了。。。。。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxm=2500;
const int maxn=200;
int a[maxn];
int dp[maxm][maxm];
int n,m;
const int inf=0x3f3f3f3f;
int main()
{
    while(~scanf("
%d%d",&n,&m)) { string aa; cin >> aa; for(int i=0;i<n;i++) { char cc; int dd,ee; cin >> cc >> dd >> ee; a[cc]=min(dd,ee);//刪除或添加同一個元素是一樣的,都能保證回文 } memset(dp,0,sizeof(dp));
for(int k=1;k<aa.size();k++) { for(int i=0,j=k;j<aa.size();i++,j++) { dp[i][j]=inf; if(aa[i]==aa[j]) dp[i][j]=dp[i+1][j-1];//兩邊都一樣,往裏找 else//不一樣的在左加一和右減一中選一個 { dp[i][j]=min(dp[i+1
][j]+a[aa[i]],dp[i][j]); dp[i][j]=min(dp[i][j-1]+a[aa[j]],dp[i][j]); } } } printf("%d\n",dp[0][aa.size()-1]); } return 0; }

poj 3280