Language: C++

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

vector< vector<int> > T[26];
int l, size;

bool Match(string a, string b, int k)
{
    for (int i = 0; i < k; ++i)
        if (a[i] != b[i])
            return false;
    return true;
}

void Stworz(int n, vector<string> a)
{
    int p = 1;
    while (p < n)
        p *= 2;
    l = p;
    size = 2 * p - 1;
    vector< vector<int> > puste(size + 1);
    for (int i = 0; i < 26; ++i)
        T[i] = puste;
    for (int i = 0; i < 26; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            for (int k = a[j].size() - 1; k >= 0; --k)
            {
                if (a[j][k] == (char) (i + 'a'))
                {
                    T[i][j + l].push_back(k);
                    break;
                }
            }
        }
    }
    for (int i = 0; i < 26; ++i)
    {
        for (int j = l - 1; j >= 1; --j)
        {
            for (int k = 0; k < T[i][2 * j].size(); ++k)
                T[i][j].push_back(T[i][2 * j][k]);
            for (int k = 0; k < T[i][2 * j + 1].size(); ++k)
                T[i][j].push_back(T[i][2 * j + 1][k]);
            sort(T[i][j].begin(), T[i][j].end());
        }
    }
}

int Ile(vector<int>& a, int k)
{
    int l = 0, p = a.size() - 1, s, r = -1;
    while (l <= p)
    {
        s = (l + p) / 2;
        if (a[s] >= k)
        {
            r = s;
            p = s - 1;
        }
        else
            l = s + 1;
    }
    if (r == -1)
        return 0;
    return a.size() - r;
}

int Pyt(vector< vector<int> >& T, int a, int b, int k)
{
    a += l;
    b += l;
    int wyn = Ile(T[a], k);
    if (a != b)
        wyn += Ile(T[b], k);
    while (a / 2 != b / 2)
    {
        if (a % 2 == 0)
            wyn += Ile(T[a + 1], k);
        if (b % 2 == 1)
            wyn += Ile(T[b - 1], k);
        a /= 2;
        b /= 2;
    }
    return wyn;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector<int> lit(26, 0);
    vector<string> a;
    for (int i = 0; i < n; ++i)
    {
        string x;
        cin >> x;
        if (x.size() == 1)
            ++lit[x[0] - 'a'];
        else
        {
            a.push_back(x);
            reverse(a.back().begin(), a.back().end());
        }
    }
    sort(a.begin(), a.end());
    n = a.size();
    ll odp = 0;
    Stworz(n, a);
    for (int i = 0, j = 0; j < n; ++j)
    {
        while (!(a[i].size() >= a[j].size() - 1 && Match(a[i], a[j], a[j].size() - 1)))
            ++i;
        if (i == j)
            continue;
        odp += Pyt(T[a[j][a[j].size() - 1] - 'a'], i, j - 1, a[j].size() - 1);
    }
    for (int i = n - 1, j = n - 1; i >= 0; --i)
    {
        while (!(a[j].size() >= a[i].size() - 1 && Match(a[i], a[j], a[i].size() - 1)))
            --j;
        if (i == j)
            continue;
        odp += Pyt(T[a[i][a[i].size() - 1] - 'a'], i + 1, j, a[i].size() - 1);
    }
    vector<ll> wyst(26, 0);
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < 26; ++j)
        {
            bool jest = false;
            for (int k = 0; k < a[i].size(); ++k)
                if (a[i][k] == (char) (j + 'a'))
                    jest = true;
            if (jest)
                ++wyst[j];
        }
    }
    for (int i = 0; i < 26; ++i)
        odp += lit[i] * wyst[i];
    cout << odp;
    return 0;
}


Compiler output:
a.cc: In function 'void Stworz(int, std::vector<std::__cxx11::basic_string<char> >)':
a.cc:26:5: error: reference to 'size' is ambiguous
   26 |     size = 2 * p - 1;
      |     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:10:8: note:                 'int size'
   10 | int l, size;
      |        ^~~~
a.cc:27:33: error: reference to 'size' is ambiguous
   27 |     vector< vector<int> > puste(size + 1);
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:10:8: note:                 'int size'
   10 | int l, size;
      |        ^~~~