http://acm.pku.edu.cn/JudgeOnline/problem?id=1458
北大ACM的第1458题,是一个动态规划的水题,可是我做出来了,编译出来结果也是对的,问题是提交老是runtime error,弄了一整天了,求帮忙,代码如下。
[code=Java][package answer;
import java.util.Scanner;
public class B14582 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String s;
String a;
String b;
int point=0;
int[][] array;
while(true){
a=new String();
b=new String();
point=0;
s=new String(in.nextLine());
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '){
//a.append(s.charAt(i));
a=a+s.charAt(i);
}
else{
point=i;
break;
}
}
for(int i=point;i<s.length();i++){
if(s.charAt(i)!=' '){
//b.append(s.charAt(i));
b=b+s.charAt(i);
}
}
//System.out.println(a+"+++++"+b);
array=new int[a.length()+1][b.length()+1];
for(int i=0;i<b.length()+1;i++){
array[0][i]=0;
}
for(int i=0;i<a.length()+1;i++){
array[i][0]=0;
}
for(int i=1;i<=a.length();i++){
for(int j=1;j<=b.length();j++){
if(a.charAt(i-1)==b.charAt(j-1)){
array[i][j]=array[i-1][j-1]+1;
}
else{
array[i][j]=Math.max(array[i-1][j],array[i][j-1]);
}
}
}
System.out.println(array[a.length()][b.length()]);
}
}
}]
------解决方案--------------------
- Java code
import java.util.Scanner;public class B14582 {public static void main(String args[]) {Scanner in = new Scanner(System.in);String s;String a;String b;int point=0;int[][] array;while(true){a=new String();b=new String();point=0;s=new String(in.nextLine());for(int i=0;i<s.length();i++){if(s.charAt(i)!=' '){//a.append(s.charAt(i));a=a+s.charAt(i);}else{point=i;break;}}for(int i=point;i<s.length();i++){if(s.charAt(i)!=' '){//b.append(s.charAt(i));b=b+s.charAt(i);}}//System.out.println(a+"+++++"+b);array=new int[a.length()+1][b.length()+1];for(int i=0;i<b.length()+1;i++){array[0][i]=0;}for(int i=0;i<a.length()+1;i++){array[i][0]=0;}for(int i=1;i<=a.length();i++){for(int j=1;j<=b.length();j++){if(a.charAt(i-1)==b.charAt(j-1)){array[i][j]=array[i-1][j-1]+1;}else{array[i][j]=Math.max(array[i-1][j],array[i][j-1]);}}}System.out.println(array[a.length()][b.length()]);}}}