1) poj 3280
注意回文串中间一定存在会回文子串。可以区间dp
#include<stdio.h>
#include <iostream>
#include <stack>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std ;
int add[130] , del[130] ;
char read[2050] ;
int dp[2050][2050] ;
int main()
{
int n , m ;
while( ~ scanf("%d %d" , &n , &m)){
scanf("%s" , read + 1 ) ;
char temp[5] ;int a , b;
memset(add , 0 , sizeof(add)) ; memset(del , 0 , sizeof(del)) ;
while( n -- ){
scanf("%s %d %d" , temp , &a , &b) ;
del[temp[0]] = b , add[temp[0]] = a ;
}
memset(dp , 0 , sizeof(dp)) ;
for(int i = m - 1 ; i >= 1 ; i -- ){
for(int j = i + 1 ; j <= m ; j ++ ){
dp[i][j] = min(dp[i+1][j] + add[read[i]] , dp[i+1][j] + del[read[i]]) ;
int tmp = min(dp[i][j-1] + add[read[j]] , dp[i][j-1] + del[read[j]]) ;
dp[i][j] = min(dp[i][j] , tmp) ;
if(read[i] == read[j])
dp[i][j] = min (dp[i][j] , dp[i+1][j-1]) ;
}
}
printf("%d\n", dp[1][m]) ;
}
return 0;
}