package ch3.uva1588;
import java.util.*;
//UVa1588
class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
String master=cin.nextLine();
String driven=cin.nextLine();
if(master.length()<driven.length()){//master定为长的
String temp=master;
master=driven;
driven=temp;
}
int masterOrgLen=master.length();
//将master左右填充
String temp="";
for(int i=0;i<driven.length();i++){
temp+="0";
}
master=temp+master+temp;
//System.out.println("master="+master);
//System.out.println("driven="+driven);
int minLen=1<<30;
//开始从左向右对接,因为master上,且为长的
for(int i=0;i<master.length()-driven.length();i++){
int j;
for(j=i;j<i+driven.length();j++){
if(master.charAt(j)-48+driven.charAt(j-i)-48>3){
break;
}
}
if(j>=i+driven.length()){
if(i<=driven.length())
minLen=Math.min(minLen,master.length()-driven.length()-i);
else if(i>driven.length()&&i<=master.length()-driven.length()*2)
minLen=Math.min(minLen,master.length()-driven.length()*2);
else
minLen=Math.min(minLen,i+driven.length()-(master.length()-driven.length())+masterOrgLen);
//System.out.printf("i=%d,minLen=%d\n",i,minLen);
}
}
System.out.println(minLen);
}
}
}
算法竞赛入门经典java版程序ch3 UVa1588
最新推荐文章于 2021-02-16 05:24:27 发布