import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer("");
String m="";
int counter=1;
while((m=br.readLine())!=null){
if("#".equals(m))
continue;
String h=br.readLine();
sb.append("Case #").append(counter).append(": you can visit at most ")
.append(LCS(m,h,new int[0][0])).append(" cities.\n");
counter++;
}
System.out.print(sb);
}
static int LCS(String m,String n,int[][] arr){
arr=new int[m.length()+1][n.length()+1];
for(int i=0;i<m.length()+1;i++)
arr[i][0]=0;
for(int i=0;i<n.length()+1;i++)
arr[0][i]=0;
for(int i=0;i<m.length();i++){
for(int j=0;j<n.length();j++){
if(m.charAt(i)==n.charAt(j))
arr[i+1][j+1]=arr[i][j]+1;
else
arr[i+1][j+1]=Math.max(arr[i+1][j],arr[i][j+1]);
}
}
return arr[m.length()][n.length()];
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer("");
String m="";
int counter=1;
while((m=br.readLine())!=null){
if("#".equals(m))
continue;
String h=br.readLine();
sb.append("Case #").append(counter).append(": you can visit at most ")
.append(LCS(m,h,new int[0][0])).append(" cities.\n");
counter++;
}
System.out.print(sb);
}
static int LCS(String m,String n,int[][] arr){
arr=new int[m.length()+1][n.length()+1];
for(int i=0;i<m.length()+1;i++)
arr[i][0]=0;
for(int i=0;i<n.length()+1;i++)
arr[0][i]=0;
for(int i=0;i<m.length();i++){
for(int j=0;j<n.length();j++){
if(m.charAt(i)==n.charAt(j))
arr[i+1][j+1]=arr[i][j]+1;
else
arr[i+1][j+1]=Math.max(arr[i+1][j],arr[i][j+1]);
}
}
return arr[m.length()][n.length()];
}
}
No comments:
Post a Comment