5 * Solution for NEERC'2009 Problem H: Headshot
6 * This solution checks correctness of the input.
7 * @author Roman Elizarov
9 public class headshot_re
{
11 public static void main(String
[] args
) throws Exception
{
12 new headshot_re().go();
15 void go() throws Exception
{
23 void read() throws Exception
{
24 Scanner in
= new Scanner(new File("headshot.in"));
25 in
.useLocale(Locale
.US
);
28 assert s
.matches("[01]{2,100}");
29 assert s
.contains("0");
33 enum Answer
{ EQUAL
, ROTATE
, SHOOT
}
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
) {
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
))
56 void write() throws Exception
{
57 PrintWriter out
= new PrintWriter("headshot.out");
62 //----------------- just for validation ------------------
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
= "";
74 private boolean localeset
;
76 public Scanner(File source
) throws FileNotFoundException
{
77 in
= new BufferedReader(new FileReader(source
));
82 assert line
== null : "Extra data at the end of file";
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
;
95 } catch (IOException e
) {
96 throw new AssertionError("Failed to read line with " + e
);
102 public String
next() {
103 assert line
!= null : "EOF";
104 assert line
.length() > 0 : "Empty line " + lineNo
;
106 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " starts with whitespace";
108 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
109 assert line
.charAt(pos
) == ' ' : "Wrong whitespace on line " + lineNo
;
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() {
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
;
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)";
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
;
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
;