Problem:
Score : 700 points

Problem Statement

Limak can repeatedly remove one of the first two characters of a string,
for example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.

You are given N different strings S_1, S_2, \ldots, S_N.
Among N \cdot (N-1) / 2 pairs (S_i, S_j),
in how many pairs could Limak obtain one string from the other?

Constraints

2 \leq N \leq 200\,000

S_i consists of lowercase English letters a-z.

S_i \neq S_j

1 \leq |S_i|

|S_1| + |S_2| + \ldots + |S_N| \leq 10^6

Input

Input is given from Standard Input in the following format.

N
S_1
S_2
\vdots
S_N

Output

Print the number of unordered pairs (S_i, S_j)
where i \neq j and Limak can obtain one string from the other.

Sample Input 1

3
abcxyx
cyx
abc

Sample Output 1

1

The only good pair is (abcxyx, cyx).

Sample Input 2

6
b
a
abc
c
d
ab

Sample Output 2

5

There are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab).

Solution in Rust (1.42.0) (judge verdict: Accepted)

const N: usize = 26;
fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let mut trie = Trie {
        nodes: vec![Node::new()],
    };
    let n: usize = sc.read();
    let mut ss = vec![];
    for _ in 0..n {
        let s = sc
            .read::<String>()
            .chars()
            .rev()
            .map(|c| c as usize - 'a' as usize)
            .collect::<Vec<usize>>();
        let mut count = [0; N];
        for &c in s.iter() {
            count[c] += 1;
        }
        trie.insert(0, &s, 0, &mut count);
        ss.push(s);
    }

    let mut ans = 0;
    for s in ss.iter() {
        let count = trie.get(0, s, 0);
        assert!(count >= 1);
        ans += count - 1;
        // eprintln!("count={}", count);
    }
    println!("{}", ans);
}

struct Trie {
    nodes: Vec<Node>,
}

impl Trie {
    fn insert(&mut self, node_id: usize, s: &[usize], pos: usize, count: &mut [usize]) {
        for i in 0..N {
            if count[i] > 0 {
                self.nodes[node_id].one_char_count[i] += 1;
            }
        }
        if s.len() == pos {
            return;
        }

        let c = s[pos];
        let next_id = if self.nodes[node_id].children[c] == 0 {
            let next_id = self.nodes.len();
            self.nodes.push(Node::new());
            self.nodes[node_id].children[c] = next_id;
            next_id
        } else {
            self.nodes[node_id].children[c]
        };

        count[c] -= 1;
        self.insert(next_id, s, pos + 1, count);
    }

    fn get(&self, node_id: usize, s: &[usize], pos: usize) -> usize {
        if pos == s.len() - 1 {
            let last_c = s[pos];
            return self.nodes[node_id].one_char_count[last_c];
        }

        let next_c = s[pos];
        let next_id = self.nodes[node_id].children[next_c];
        assert_ne!(next_id, 0);
        self.get(next_id, s, pos + 1)
    }
}

struct Node {
    children: [usize; N],
    one_char_count: [usize; N],
}

impl Node {
    fn new() -> Self {
        Self {
            children: [0; 26],
            one_char_count: [0; 26],
        }
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write_all(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}