about summary refs log tree commit diff
path: root/codechef/teamname.rs
blob: 23f23e36a0db625c72c1ae64793aeb64d2484fff (plain) (blame)
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
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)
            }
        })))
    }
}