amazon笔试题

下面是来自于亚马逊的编程笔试题,感兴趣的同学可以先自己想想解题思路:

Please implement a simple logic rule, the input is a set of logic formula, the format is like a1,a2,a3,…,an->b, which means a1,a2,a3,…an can deduce b. It meets the following rules:

1. If A->B B->C then A->C

2. If A,B->C D->A then D,B->C

3. If A,B->C then B,A->C

 

The input contains two lines:

1. The first line contains n (100>n>0) base formulas, separated by spaces

2. The second line contains m (100>m>0) formulas that need to check true or false according to above n formulas, separated by spaces

 

The output should be m lines, if the i-th formula in the second line can be deduced by the n base formulas, the i-th line should be "true", otherwise, "false".

 

Each element is a case sensitive alphabet, to simplify the parsing work, “->” is replaced by ">", there might be more than 1 character on the left side and only one on the right side, the following is an example: A,B,C>D

 

Sample Input and Output

Input

A>B B>C

A>C

Output

true

 

Input

A,B>C D,E>A E>B

E,D>C F,E>C

Output

true

false

 

Input

A>B A,B>C

A>C

Output

true

 

Please complement the matchRule method in Solution class:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

 

public class Solution {

public static void main(String[] args) throws IOException {

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

 

String baseRules = br.readLine();

String checkingRules = br.readLine();

 

matchRule(baseRules, checkingRules);

 

br.close();

isr.close();

}

 

private static void matchRule(String baseRules, String checkingRules) {

 

}

}

 

 

 

  1 import java.io.BufferedReader;
  2 import java.io.IOException;
  3 import java.io.InputStreamReader;
  4 import java.util.HashMap;
  5 import java.util.Vector;
  6 
  7 
  8 public class Solution {
  9     
 10     
 11     private static HashMap<String, node> nodeAlreadyHave = new HashMap<String, node>();
 12     public static void main(String[] args) throws IOException {
 13         InputStreamReader isr = new InputStreamReader(System.in);
 14         BufferedReader br = new BufferedReader(isr);
 15 
 16 //        String baseRules = "A>B A,B>C";
 17 //        String checkingRules = "A>C";
 18         String baseRules = br.readLine();
 19         String checkingRules = br.readLine();
 20          
 21         matchRule(baseRules, checkingRules);
 22          
 23         br.close();
 24         isr.close();
 25     }
 26     static class node{
 27         String identified;
 28         Vector<node> nextnodes = null;
 29         int count;
 30         
 31         node(String name)
 32         {
 33             identified = name;
 34             count = 0;
 35             nextnodes = new Vector<node>();
 36         }
 37         
 38         boolean after(node t)
 39         {
 40             if(identified.equals(t.identified))
 41                 return true;
 42             for(int i = 0;i < nextnodes.size(); i++)
 43             {
 44                 if(nextnodes.get(i).after(t))
 45                     return true;
 46             }
 47             return false;
 48         }
 49     }
 50     private static void matchRule(String baseRules, String checkingRules) {
 51         System.out.println("test");
 52         String[] baseRulesArray = baseRules.split(" ");
 53         for(int i=0 ;i < baseRulesArray.length; i++)
 54         {
 55             System.out.println(baseRulesArray[i]);
 56             
 57         }
 58         
 59         parseRule(baseRulesArray);
 60         String[] checkingRulesArray = checkingRules.split(" ");
 61         for(int i=0 ;i < checkingRulesArray.length; i++)
 62         {
 63             System.out.println(checkingRulesArray[i]);
 64             checkRules(checkingRulesArray[i]);
 65         }
 66         
 67         
 68         
 69     }
 70     
 71     private static void parseRule(String[] rules)
 72     {
 73         for(int i = 0 ; i < rules.length;i++)
 74         {
 75             String[] token = rules[i].split("[, >]");
 76             node t;
 77             if(!nodeAlreadyHave.containsKey(token[token.length-1]))
 78             {
 79                 t = new node(token[token.length-1]);
 80                 nodeAlreadyHave.put(token[token.length-1], t);
 81             }else {
 82                 t = nodeAlreadyHave.get(token[token.length-1]);
 83             }
 84             for(int j=0 ;j< token.length-1; j++)
 85             {
 86                 node s;
 87                 if(!nodeAlreadyHave.containsKey(token[j]))
 88                 {
 89                     s = new node(token[j]);
 90                     nodeAlreadyHave.put(token[j], s);
 91                 }else{
 92                     s = nodeAlreadyHave.get(token[j]);
 93                 }
 94                 s.nextnodes.add(t);
 95                 
 96                 //System.out.println("token "+token[j]);
 97             }
 98         }
 99         
100     }
101     
102     private static void checkRules(String checkRule)
103     {
104         String[] token = checkRule.split("[, >]");
105         node t;
106         if(!nodeAlreadyHave.containsKey(token[token.length-1]))
107         {
108             System.out.println("false");
109             return ;
110         }else {
111             t = nodeAlreadyHave.get(token[token.length-1]);
112         }
113         for(int j=0 ;j< token.length-1; j++)
114         {
115             node s;
116             if(!nodeAlreadyHave.containsKey(token[j]))
117             {
118                 System.out.println("false");
119                 return ;
120             }else{
121                 s = nodeAlreadyHave.get(token[j]);
122             }
123             if(s.after(t))
124             {
125                 System.out.println("true");
126                 return;
127             }
128             
129             
130         }
131         System.out.println("false");
132     }
133     
134 
135     
136 
137 }

 

posted @ 2013-05-11 21:45  JustinYo  阅读(822)  评论(0编辑  收藏  举报