简单的最长公共子序列的变形,只是题有点吓人..
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <stdio.h>
#include <string.h>
int dp[2005][2005],ar[2005];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n,a,len1,len2,i,j;
char str[2005],ans1[2005],ans2[2005];
while(scanf("%d",&n)!=EOF)
{
scanf("%s",str);
for (i=0;i<n;i++)
{
scanf("%d",&a);
ar[str[i]]=a;
}
scanf("%s%s",ans1+1,ans2+1);
len1=strlen(ans1);
len2=strlen(ans2);
for (i=0;i<len1;i++)dp[i][0]=0;
for (j=0;j<len2;j++)dp[0][j]=0;
for (i=1;i<len1;i++)
{
for (j=1;j<len2;j++)
{
if(ans1[i]==ans2[j])dp[i][j]=dp[i-1][j-1]+ar[ans1[i]];
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
printf("%d\n",dp[len1-1][len2-1]);
}
return 0;
}