5 * Solution for NEERC'2009 Problem D: Database
6 * This solution checks correctness of the input.
7 * @author Roman Elizarov
9 public class database_re
{
11 public static void main(String
[] args
) throws Exception
{
12 new database_re().go();
15 void go() throws Exception
{
23 List
<List
<String
>> rows
= new ArrayList
<List
<String
>>();
25 void read() throws Exception
{
26 Scanner in
= new Scanner(new File("database.in"));
27 in
.useLocale(Locale
.US
);
30 String rest
= in
.nextLine();
31 assert rest
.equals("");
32 assert n
>= 1 && n
<= 10000;
33 assert m
>= 1 && m
<= 10;
34 for (int i
= 0; i
< n
; i
++) {
35 String line
= in
.nextLine();
36 assert line
.length() <= 80;
37 List
<String
> row
= Arrays
.asList(line
.split(","));
38 assert row
.size() == m
;
39 for (int j
= 0; j
< m
; j
++) {
40 String s
= row
.get(j
);
41 assert s
.trim().equals(s
);
42 assert s
.length() > 0;
43 for (int k
= 0; k
< s
.length(); k
++) {
45 assert c
>= 32 && c
<= 126 && c
!= 44;
60 for (int i
= 0; i
< m
; i
++) {
61 for (int j
= i
+ 1; j
< m
; j
++) {
73 Pair(String s1
, String s2
) {
79 public boolean equals(Object o
) {
82 if (!(o
instanceof Pair
))
87 if (!s1
.equals(pair
.s1
))
89 if (!s2
.equals(pair
.s2
))
96 public int hashCode() {
97 int result
= s1
.hashCode();
98 result
= 31 * result
+ s2
.hashCode();
103 private void checkRowPair(int i
, int j
) {
104 Map
<Pair
, Integer
> vals
= new HashMap
<Pair
, Integer
>();
105 for (int k
= 0; k
< n
; k
++) {
106 List
<String
> row
= rows
.get(k
);
107 Integer old
= vals
.put(new Pair(row
.get(i
), row
.get(j
)), k
);
119 void write() throws Exception
{
120 PrintWriter out
= new PrintWriter("database.out");
125 out
.printf("%d %d%n", r1
+ 1, r2
+ 1);
126 out
.printf("%d %d%n", c1
+ 1, c2
+ 1);
131 //----------------- just for validation ------------------
134 * Strict scanner to veryfy 100% correspondence between input files and input file format specification.
135 * It is a drop-in replacement for {@link java.util.Scanner} that could be added to a soulution source
136 * (cut-and-paste) without breaking its ability to work with {@link java.util.Scanner}.
138 public class Scanner
{
139 private final BufferedReader in
;
140 private String line
= "";
143 private boolean localeset
;
145 public Scanner(File source
) throws FileNotFoundException
{
146 in
= new BufferedReader(new FileReader(source
));
150 public void close() {
151 assert line
== null : "Extra data at the end of file";
154 } catch (IOException e
) {
155 throw new AssertionError("Failed to close with " + e
);
159 public String
nextLine() {
160 assert line
!= null : "EOF";
161 String result
= line
.substring(pos
);
163 line
= in
.readLine();
164 } catch (IOException e
) {
165 throw new AssertionError("Failed to read line with " + e
);
173 public String
next() {
174 assert line
!= null : "EOF";
175 assert line
.length() > 0 : "Empty line " + lineNo
;
177 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " starts with whitespace";
179 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
180 assert line
.charAt(pos
) == ' ' : "Wrong whitespace on line " + lineNo
;
182 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
183 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " has double whitespace";
185 StringBuilder sb
= new StringBuilder();
186 while (pos
< line
.length() && line
.charAt(pos
) > ' ')
187 sb
.append(line
.charAt(pos
++));
188 return sb
.toString();
191 public int nextInt() {
193 assert s
.length() == 1 || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
194 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
196 return Integer
.parseInt(s
);
197 } catch (NumberFormatException e
) {
198 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
202 public double nextDouble() {
203 assert localeset
: "Locale must be set with useLocale(Locale.US)";
205 assert s
.length() == 1 || s
.startsWith("0.") || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
206 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
208 return Double
.parseDouble(s
);
209 } catch (NumberFormatException e
) {
210 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
214 public void useLocale(Locale locale
) {
215 assert locale
== Locale
.US
;