Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <fstream>
#include <sstream>
#include <unordered_map>
using namespace std;

const unsigned long long BASE = (unsigned long long)(1e9 + 7);
bool flagBuffer[1000001][26];

struct Node
{
    Node(int i, Node* f, bool * flag)
    {
        idx = i;
        father = f;
        for (int i = 0; i < 26; i++)
        {
            child[i] = NULL;
            count[i] = int(flag[i]);
        }
        sum = 1;    // itself
    }
    Node * father;
    Node * child[26];
    int idx;
    int count[26];  // count of all child strings has the char
    int sum;    // count of all strings has this node
};

Node * push(Node * root, const string & s, vector<Node*> & newNode)
{
    // construct flag buffer
    memset(flagBuffer, 0, sizeof(bool) * s.length() * 26);
    for (int i = 1; i < (int)s.length(); i++)
    {
        for (int j = 0; j < 26; j++)
        {
            flagBuffer[i][j] = flagBuffer[i - 1][j] || (int(s[i - 1] - 'a') == j);
        }
    }
    // push into tree
    Node * pCurrent = root; // root can be ignored
    for (int i = (int)s.length() - 1; i >= 0; i--)
    {
        int idx = s[i] - 'a';
        if (pCurrent->child[idx] != NULL)
        {
            pCurrent = pCurrent->child[idx];
            for (int j = 0; j < 26; j++)
            {
                pCurrent->count[j] += flagBuffer[i][j];
            }
            pCurrent->sum++;
        }
        else
        {
            while (i >= 0)
            {
                idx = s[i] - 'a';
                Node * pNew = new Node(idx, pCurrent, flagBuffer[i]);
                pCurrent->child[idx] = pNew;
                pCurrent = pNew;
                newNode.push_back(pNew);
                i--;
            }
            break;
        }
    }
    return pCurrent;
}

int main()
{
    int n;
    cin >> n;
    // construct tree
    memset(flagBuffer, 0, sizeof(flagBuffer));
    Node root(0, NULL, flagBuffer[0]);
    vector<Node*> allNode;
    vector<Node*> newNode;
    allNode.reserve(n);
    newNode.reserve(n);
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        Node * pNode = push(&root, s, newNode);
        allNode.push_back(pNode);
    }
    // count
    int count = 0;
    for (int i = 0; i < (int)allNode.size(); i++)
    {
        count += allNode[i]->sum - 1;
        int idx = allNode[i]->idx;
        Node * pFather = allNode[i]->father;
        for (int j = 0; j < 26; j++)
        {
            if (j != idx && pFather->child[j] != NULL)
            {
                count += pFather->child[j]->count[idx];
            }
        }
    }
    cout << count << endl;

    for (int i = 0; i < (int)newNode.size(); i++)
    {
        delete newNode[i];
    }
    return 0;
}


Compiler output:
a.cc: In function 'Node* push(Node*, const std::string&, std::vector<Node*>&)':
a.cc:37:5: error: 'memset' was not declared in this scope
   37 |     memset(flagBuffer, 0, sizeof(bool) * s.length() * 26);
      |     ^~~~~~
a.cc:9:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    8 | #include <unordered_map>
  +++ |+#include <cstring>
    9 | using namespace std;
a.cc: In function 'int main()':
a.cc:81:5: error: 'memset' was not declared in this scope
   81 |     memset(flagBuffer, 0, sizeof(flagBuffer));
      |     ^~~~~~
a.cc:81:5: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'