Roy played a game with his roommates the other day.
His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common character in the 2 strings, the bonus value of that character would be added to Roy's score. However at the mean time, the 2 characters and those before them in the strings would be erased.Roy now needs your help, because he wants to know the maximum score he can get. InputThere are several test cases.
For each test case, the first line contains an integer N.The following N lines describe a list of N characters and their bonus values.Then the following 2 lines give the 2 strings. OutputFor each test case, output in one line the best score Roy can get.
Sample Input3
a 1b 1c 1abcbca3a 1b 10c 100abccab Sample Output2100
度娘的思路:本题的算法思想与LCS问题类似。设输入的2个字符串s1[0...n]和s2[0...m],另f(i,j)表示子串s1[0...i]和s2[0...j]对应的最大得分,则有DP递推式
f(i,j)=max{f(i-1,j-1)+(x[i]==y[j])?v(x[i]):0,f(i,j-1),f(i-1,j)},(v(c)表示字符c的分值),所以最大得分便是f(n,m)。我呢,各种犯错,记录下
View Code
1 # include2 # include 3 # include 4 # include