#include <bits/stdc++.h>
using namespace std;
class TrieNode{
public:
int fr[26];
int endHere;
TrieNode* children[26];
TrieNode() {
endHere = 0;
for(int i = 0;i<26;i++){
children[i] = NULL;
fr[i] = 0;
}
}
};
class Trie {
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}
int insert(string word) {
TrieNode* curr = root;
int hits = 0, hit=0;
for(int i = 0; i<word.size(); i++) {
int index = word[i] - 'a';
if(curr->children[index] == NULL) {
curr->children[index] = new TrieNode();
}
curr = curr->children[index];
hit += curr->endHere;
hits = curr->fr[index];
(curr->fr[index])++;
}
hit -= curr->endHere;
(curr->endHere)++;
return hits+hit;
}
};
void solve(vector<string> v) {
int n = v.size();
int ans = 0;
Trie t;
for(int i=0;i<n;i++) {
int val = t.insert(v[i]);
ans += val;
}
cout << ans << endl;
}
int main() {
solve({"wall", "wallpaper", "science", "wallet", "philosopher", "phil", "book"});
solve({"abc","a","a","b","ab","ac"});
solve({"a", "a", "a", "a", "abc"});
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBUcmllTm9kZXsKcHVibGljOgogICAgaW50IGZyWzI2XTsKICAgIGludCBlbmRIZXJlOwogICAgVHJpZU5vZGUqIGNoaWxkcmVuWzI2XTsKICAgIAogICAgVHJpZU5vZGUoKSB7CiAgICAJZW5kSGVyZSA9IDA7CiAgICAgICAgZm9yKGludCBpID0gMDtpPDI2O2krKyl7CiAgICAgICAgICAgIGNoaWxkcmVuW2ldID0gTlVMTDsKICAgICAgICAgICAgZnJbaV0gPSAwOwogICAgICAgIH0KICAgIH0KfTsKCmNsYXNzIFRyaWUgewpwdWJsaWM6CglUcmllTm9kZSogcm9vdDsKCQogICAgVHJpZSgpIHsKICAgICAgICByb290ID0gbmV3IFRyaWVOb2RlKCk7CiAgICB9CgogICAgaW50IGluc2VydChzdHJpbmcgd29yZCkgewogICAgICAgIFRyaWVOb2RlKiBjdXJyID0gcm9vdDsKICAgICAgICBpbnQgaGl0cyA9IDAsIGhpdD0wOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGk8d29yZC5zaXplKCk7IGkrKykgewogICAgICAgICAgICBpbnQgaW5kZXggPSB3b3JkW2ldIC0gJ2EnOwogICAgICAgICAgICBpZihjdXJyLT5jaGlsZHJlbltpbmRleF0gPT0gTlVMTCkgewogICAgICAgICAgICAgICAgY3Vyci0+Y2hpbGRyZW5baW5kZXhdID0gbmV3IFRyaWVOb2RlKCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3VyciA9IGN1cnItPmNoaWxkcmVuW2luZGV4XTsKICAgICAgICAgICAgaGl0ICs9IGN1cnItPmVuZEhlcmU7CiAgICAgICAgICAgIGhpdHMgPSBjdXJyLT5mcltpbmRleF07CiAgICAgICAgICAgIChjdXJyLT5mcltpbmRleF0pKys7CiAgICAgICAgfQogICAgICAgIGhpdCAtPSBjdXJyLT5lbmRIZXJlOwogICAgICAgIChjdXJyLT5lbmRIZXJlKSsrOwogICAgICAgIHJldHVybiBoaXRzK2hpdDsKICAgIH0KfTsKCgp2b2lkIHNvbHZlKHZlY3RvcjxzdHJpbmc+IHYpIHsKCWludCBuID0gdi5zaXplKCk7CglpbnQgYW5zID0gMDsKCVRyaWUgdDsKCWZvcihpbnQgaT0wO2k8bjtpKyspIHsKCQlpbnQgdmFsID0gdC5pbnNlcnQodltpXSk7CgkJYW5zICs9IHZhbDsKCX0KCWNvdXQgPDwgYW5zIDw8IGVuZGw7Cn0KCmludCBtYWluKCkgewoJc29sdmUoeyJ3YWxsIiwgIndhbGxwYXBlciIsICJzY2llbmNlIiwgIndhbGxldCIsICJwaGlsb3NvcGhlciIsICJwaGlsIiwgImJvb2sifSk7Cglzb2x2ZSh7ImFiYyIsImEiLCJhIiwiYiIsImFiIiwiYWMifSk7Cglzb2x2ZSh7ImEiLCAiYSIsICJhIiwgImEiLCAiYWJjIn0pOwoJcmV0dXJuIDA7Cn0=