最长公共子串问题理解的话。这个就不是问题了。水……
zoj这种题目好多,都没有说数据范围……只能拼rp。。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 4050
int a[300];
int f[N][N], n, m;
char s1[N], s2[N];
int main() {
int t, v;
char ch;
while (scanf("%d", &t) == 1) {
while (t--) {
scanf(" %c %d", &ch, &v);
a[(int)ch] = v;
}
scanf(" %s %s", s1, s2);
n = strlen(s1);
m = strlen(s2);
memset(f, 0, sizeof(f));
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++) {
if (s1[i-1] == s2[j-1]) f[i][j] = f[i-1][j-1] + a[(int)s1[i-1]];
f[i][j] = max(f[i][j], max(f[i-1][j], f[i][j-1]));
}
printf("%d\n", f[n][m]);
}
return 0;
}