形成回文串的最小代价(代价1)
for(int k=1;k
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 9973
#define N 70
typedef long long ll;
const int M=100005;
int n,m;
int sx,sy,sz;
char mp[M];
int pa[M],rankk[M];
int head[M*6],vis[M*6];
double dis[M][10];
//int g[5][5];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
//pair
vector
int sum[2005];
int dp[2005][2005];
int add,rem;
struct node
{
int st,s;
};
bool cmp(node a,node b)
{
if(a.st!=b.st)
return a.st
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
bool isrun(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return true;
return false;
}
int main()
{
int i,j;
char c;
while(~scanf("%d%d",&n,&m))
{
scanf("%s%*c",mp);
for(i=0;i
scanf("%c %d %d%*c",&c,&add,&rem);
sum[(c-'a')]=min(add,rem);
}
int len=strlen(mp);
memset(dp,0,sizeof(dp));
for(int k=1;k
i=0;
for(j=k;j
dp[i][j]=INF;
if(mp[i]==mp[j])
dp[i][j]=dp[i+1][j-1];
else{
dp[i][j]=min(dp[i][j],dp[i+1][j]+sum[mp[i]-'a']);
dp[i][j]=min(dp[i][j],dp[i][j-1]+sum[mp[j]-'a']);
}
}
}
printf("%d\n",dp[0][m-1]);
}
return 0;
}
记忆化搜索
int dfs(int l,int r)
{
if(dp[l][r]!=-1)
return dp[l][r];
if(l>=r)
{
return dp[l][r]=0;
}
if(mp[l]==mp[r])
{
return dp[l][r]=dfs(l+1,r-1);
}
else{
return dp[l][r]=min(dfs(l+1,r)+sum[mp[l]-'a'],dfs(l,r-1)+sum[mp[r]-'a']);
}
}
int main()
{
int i,j;
char c;
while(~scanf("%d%d",&n,&m))
{
scanf("%s%*c",mp);
for(i=0;i
scanf("%c %d %d%*c",&c,&add,&rem);
sum[(c-'a')]=min(add,rem);
}
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(0,m-1));
}
return 0;
}