Adding some more judges, here and there.
[and.git] / NEERC / headshot / headshot_re.java
blob734c7ec502ff9a4945f5a85c8c14c4ca42a64abe
1 import java.io.*;
2 import java.util.*;
4 /**
5 * Solution for NEERC'2009 Problem H: Headshot
6 * This solution checks correctness of the input.
7 * @author Roman Elizarov
8 */
9 public class headshot_re {
11 public static void main(String[] args) throws Exception {
12 new headshot_re().go();
15 void go() throws Exception {
16 read();
17 solve();
18 write();
21 String s;
23 void read() throws Exception {
24 Scanner in = new Scanner(new File("headshot.in"));
25 in.useLocale(Locale.US);
26 s = in.next();
27 in.nextLine();
28 assert s.matches("[01]{2,100}");
29 assert s.contains("0");
30 in.close();
33 enum Answer { EQUAL, ROTATE, SHOOT }
35 Answer a;
37 void solve() {
38 int p1 = count("01");
39 int q1 = count("0");
40 int p2 = count("1");
41 int q2 = s.length();
42 int diff = p1 * q2 - p2 * q1;
43 a = diff < 0 ? Answer.SHOOT : diff > 0 ? Answer.ROTATE : Answer.EQUAL;
46 private int count(String t) {
47 int count = 0;
48 String ss = s + s.substring(0, t.length() - 1);
49 for (int i = 0; i < s.length(); i++) {
50 if (ss.substring(i, i + t.length()).equals(t))
51 count++;
53 return count;
56 void write() throws Exception {
57 PrintWriter out = new PrintWriter("headshot.out");
58 out.println(a);
59 out.close();
62 //----------------- just for validation ------------------
64 /**
65 * Strict scanner to veryfy 100% correspondence between input files and input file format specification.
66 * It is a drop-in replacement for {@link java.util.Scanner} that could be added to a soulution source
67 * (cut-and-paste) without breaking its ability to work with {@link java.util.Scanner}.
69 public class Scanner {
70 private final BufferedReader in;
71 private String line = "";
72 private int pos;
73 private int lineNo;
74 private boolean localeset;
76 public Scanner(File source) throws FileNotFoundException {
77 in = new BufferedReader(new FileReader(source));
78 nextLine();
81 public void close() {
82 assert line == null : "Extra data at the end of file";
83 try {
84 in.close();
85 } catch (IOException e) {
86 throw new AssertionError("Failed to close with " + e);
90 public void nextLine() {
91 assert line != null : "EOF";
92 assert pos == line.length() : "Extra characters on line " + lineNo;
93 try {
94 line = in.readLine();
95 } catch (IOException e) {
96 throw new AssertionError("Failed to read line with " + e);
98 pos = 0;
99 lineNo++;
102 public String next() {
103 assert line != null : "EOF";
104 assert line.length() > 0 : "Empty line " + lineNo;
105 if (pos == 0)
106 assert line.charAt(0) > ' ' : "Line " + lineNo + " starts with whitespace";
107 else {
108 assert pos < line.length() : "Line " + lineNo + " is over";
109 assert line.charAt(pos) == ' ' : "Wrong whitespace on line " + lineNo;
110 pos++;
111 assert pos < line.length() : "Line " + lineNo + " is over";
112 assert line.charAt(0) > ' ' : "Line " + lineNo + " has double whitespace";
114 StringBuilder sb = new StringBuilder();
115 while (pos < line.length() && line.charAt(pos) > ' ')
116 sb.append(line.charAt(pos++));
117 return sb.toString();
120 public int nextInt() {
121 String s = next();
122 assert s.length() == 1 || s.charAt(0) != '0' : "Extra leading zero in number " + s + " on line " + lineNo;
123 assert s.charAt(0) != '+' : "Extra leading '+' in number " + s + " on line " + lineNo;
124 try {
125 return Integer.parseInt(s);
126 } catch (NumberFormatException e) {
127 throw new AssertionError("Malformed number " + s + " on line " + lineNo);
131 public double nextDouble() {
132 assert localeset : "Locale must be set with useLocale(Locale.US)";
133 String s = next();
134 assert s.length() == 1 || s.startsWith("0.") || s.charAt(0) != '0' : "Extra leading zero in number " + s + " on line " + lineNo;
135 assert s.charAt(0) != '+' : "Extra leading '+' in number " + s + " on line " + lineNo;
136 try {
137 return Double.parseDouble(s);
138 } catch (NumberFormatException e) {
139 throw new AssertionError("Malformed number " + s + " on line " + lineNo);
143 public void useLocale(Locale locale) {
144 assert locale == Locale.US;
145 localeset = true;