-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcitystate.java
More file actions
44 lines (40 loc) · 1.41 KB
/
citystate.java
File metadata and controls
44 lines (40 loc) · 1.41 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
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.*;
class stuff {
String city;
String state;
}
public class citystate {
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader br = new BufferedReader(new FileReader("citystate.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("citystate.out")));
int n = Integer.parseInt(br.readLine());
Map<String, Long> count = new HashMap<String, Long>();
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String city = st.nextToken();
String state = st.nextToken();
if(!city.substring(0, 2).equals(state)) {
String key = city.substring(0, 2) + state;
if(!count.containsKey(key)) {
count.put(key, 0L);
}
count.put(key, count.get(key) + 1);
}
}
long ret = 0;
for(String key: count.keySet()) {
String otherKey = key.substring(2) + key.substring(0, 2);
if(count.containsKey(otherKey)) {
ret += count.get(key) * count.get(otherKey);
}
}
// note that we have double-counted each pair
pw.println(ret / 2);
pw.close();
}
}