about summary refs log tree commit diff
path: root/codechef/teamname.rs
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-02-15 21:05:21 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-02-16 14:57:43 +0700
commit2b91f9554b326aea138bd8a0acbfaa10d9ad59aa (patch)
tree10053cf99e42711999d33b422a8735c5367d339f /codechef/teamname.rs
parent0cf49da1527983b9d165cd394b82c6bcbcafca60 (diff)
downloadcp-2b91f9554b326aea138bd8a0acbfaa10d9ad59aa.tar.gz
[codechef] Try CP again after almost a year
Diffstat (limited to 'codechef/teamname.rs')
-rw-r--r--codechef/teamname.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/codechef/teamname.rs b/codechef/teamname.rs
new file mode 100644
index 0000000..23f23e3
--- /dev/null
+++ b/codechef/teamname.rs
@@ -0,0 +1,26 @@
+use std::collections::HashSet;
+use std::io::BufRead;
+
+fn main() {
+    let stdin = std::io::stdin();
+    let mut lines = stdin.lock().lines();
+    for _t in 0..lines.next().unwrap().unwrap().trim().parse::<u8>().unwrap() {
+        let _n = lines.next().unwrap();
+        let line = lines.next().unwrap().unwrap();
+        let words: Vec<&str> = line.split(' ').collect();
+
+        let mut graph: [HashSet<String>; 26] = Default::default();
+        for word in &words {
+            let i = word.chars().next().unwrap() as usize;
+            graph[i - 97].insert(word[1..].to_string());
+        }
+
+        println!("{}", (0..26).fold(0, |r, i| r + (0..26).fold(0, |s, j| {
+            if i == j { s } else {
+                let intersect = graph[i].iter()
+                    .fold(0, |c, d| c + graph[j].contains(d) as usize);
+                s + (graph[i].len() - intersect) * (graph[j].len() - intersect)
+            }
+        })))
+    }
+}