首页 > 专题系列 > Java解POJ > POJ 3158 Kickdown [解题报告] Java
2013
11-12

POJ 3158 Kickdown [解题报告] Java

Kickdown

问题描述 :

A research laboratory of a world-leading automobile company has received an order to create a special transmission mechanism, which allows for incredibly efficient kickdown — an operation of switching to lower gear. After several months of research engineers found that the most efficient solution requires special gears with teeth and cavities placed non-uniformly. They calculated the optimal flanks of the gears. Now they want to perform some experiments to prove their findings.

The first phase of the experiment is done with planar toothed sections, not round-shaped gears. A section of length n consists of n units. The unit is either a cavity of height h or a tooth of height 2h. Two sections are required for the experiment: one to emulate master gear (with teeth at the bottom) and one for the driven gear (with teeth at the top).

There is a long stripe of width 3h in the laboratory and its length is enough for cutting two engaged sections together. The sections are irregular but they may still be put together if shifted along each other.

The stripe is made of an expensive alloy, so the engineers want to use as little of it as possible. You need to find the minimal length of the stripe which is enough for cutting both sections simultaneously.

输入:

There are two lines in the input file, each contains a string to describe a section. The first line describes master section (teeth at the bottom) and the second line describes driven section (teeth at the top). Each character in a string represents one section unit — 1 for a cavity and 2 for a tooth. The sections can not be flipped or rotated.

Each string is non-empty and its length does not exceed 100.

输出:

Write a single integer number to the output file — the minimal length of the stripe required to cut off given sections.

样例输入:

sample input #1
2112112112
2212112

sample input #2
12121212
21212121

sample input #3
2211221122
21212

样例输出:

sample output #1
10

sample output #2
8

sample output #3
15

解题代码:

//* @author  [email protected]
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        String s1,s2,s;
        int[] m;
        int k,v1,v2;
        boolean band;
        while(scan.hasNext()){
            s1=scan.next();
            s2=scan.next();
            s=s1;
            for(int i=0;i< s2.length();i++)s+="1";
            k=-1;
            for(int i=0;i<=s.length();i++){
                band=true;
                for(int j=0;j< s2.length();j++){
                 if((Integer.parseInt(s2.charAt(j)+"")+Integer.parseInt(s.charAt(i+j)+""))>3)
                    {
                      band=false;break;
                   }
                }
                if(band){k=i;break;}
            }
            v1=Math.max(s1.length(),k+s2.length());
            s=s2;
            for(int i=0;i< s1.length();i++)s+="1";
            k=-1;
            for(int i=0;i<=s.length();i++){
                band=true;
                for(int j=0;j< s1.length();j++){
                 if((Integer.parseInt(s1.charAt(j)+"")+Integer.parseInt(s.charAt(i+j)+""))>3)
                      {band=false;break;}
                }
                if(band){k=i;break;}
            }            
            v2=Math.max(s2.length(),k+s1.length());
            System.out.println(Math.min(v1, v2));
        }
    }
}

  1. 可以参考算法导论中的时间戳。就是结束访问时间,最后结束的顶点肯定是入度为0的顶点,因为DFS要回溯