forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.java
More file actions
142 lines (120 loc) · 3.96 KB
/
Copy pathA.java
File metadata and controls
142 lines (120 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.io.PrintStream;
import java.util.*;
/**
* Codejam 2009 Round 1B
* Problem A. Decision Tree
*/
public class Main {
class MyScanner {
char[] chars;
int len, index;
public MyScanner(String s) {
chars=s.toCharArray();
len=chars.length;
index=0;
}
public boolean hasNext() {
ignoreSpaces();
return index<len;
}
public void ignoreSpaces() {
while (index<len && Character.isWhitespace(chars[index])) index++;
}
public char nextChar() {
ignoreSpaces(); if (!hasNext()) throw new InputMismatchException();
return chars[index++];
}
public void back() {
index--;
}
public String nextString(String pattern) {
ignoreSpaces(); if (!hasNext()) throw new InputMismatchException();
StringBuilder builder=new StringBuilder();
while (index<len && !Character.isWhitespace(chars[index])) {
builder.append(chars[index++]);
if (!builder.toString().matches(pattern)) {
index--;
return builder.substring(0, builder.length()-1);
}
}
return builder.toString();
}
public String next() {
return nextString("[^\\s]+");
}
public int nextInt() {
return Integer.parseInt(nextString("[0-9]+"));
}
public double nextDouble() {
return Double.parseDouble(nextString("[0-9]+(\\.[0-9]*)?"));
}
}
class Tree {
String name;
double p;
Tree left, right;
public Tree(double _p, String _name) {
p=_p;name=_name;
}
}
public String solve(Scanner scanner) {
int l=Integer.parseInt(scanner.nextLine());
StringBuilder builder=new StringBuilder();
for (int i=0;i<l;i++) builder.append(scanner.nextLine()).append(" ");
MyScanner ss=new MyScanner(builder.toString());
Tree root=readTree(ss);
ArrayList<String> ans=new ArrayList<>();
ans.add("");
int n=Integer.parseInt(scanner.nextLine());
while (n-->0) {
Scanner scanner1=new Scanner(scanner.nextLine());
scanner1.next();
HashSet<String> set=new HashSet<>();
int m=scanner1.nextInt();
while (m-->0) set.add(scanner1.next());
Tree tree=root;
double p=tree.p;
while (tree.name!=null) {
if (set.contains(tree.name))
tree=tree.left;
else
tree=tree.right;
p*=tree.p;
}
ans.add(String.format("%.9f", p));
}
return String.join("\n", ans);
}
private Tree readTree(MyScanner scanner) {
// read '('
scanner.nextChar();
// read p
double p=scanner.nextDouble();
if (scanner.nextChar()==')') {
return new Tree(p, null);
}
scanner.back();
Tree tree=new Tree(p, scanner.next());
tree.left=readTree(scanner);
tree.right=readTree(scanner);
scanner.nextChar();
return tree;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=Integer.parseInt(scanner.nextLine());
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
try {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
catch (Throwable e) {
System.err.println("ERROR in case #"+t);
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}