标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4064 Accepted Submission(s): 967
package 动态规划; import java.util.Scanner; public class hdu_1243 { static int []value; static String str1,str2; public static void main(String[] args) { Scanner sc =new Scanner (System.in); while(sc.hasNext()){ int n = sc.nextInt(); String str = sc.next(); char [] a = str.toCharArray(); value = new int[100]; for(int i=0;i<n;i++){ int num = sc.nextInt(); value[a[i]-‘a‘] = num; } str1 = sc.next(); str2 = sc.next(); LCS(str1,str2); } } private static void LCS(String str, String str1) { char [] x =str.toCharArray(); char [] y = str1.toCharArray(); int[][] dp = new int [x.length+1][y.length+1]; for(int i=1;i<x.length+1;i++){ for(int j=1;j<y.length+1;j++){ if(x[i-1]==y[j-1]){ dp[i][j]=dp[i-1][j-1]+value[str.charAt(i-1)-‘a‘]; }else if(dp[i-1][j]>dp[i][j-1]){ dp[i][j]=dp[i-1][j]; }else { dp[i][j]=dp[i][j-1]; } } } System.out.println(dp[x.length][y.length]); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5372392.html