Language: C++

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>


#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;

int ptr[52][12000005],c[12000005],id=0,msk[1000005];
ll ans=0;
vector<string> v[1000005];

int main()
{ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
rep(i,0,n-1)
{
	string s;
	cin>>s;
	reverse(all(s));
	v[s.size()].pb(s);
}

ren(len,1000004,1)
{
	if(v[len].size()==0)continue;
	tr(v[len],it)
	{
		string s=*it;
		int root=0,f=1;
		rep(i,0,len-2)
		{
			if(ptr[s[i]-'a'][root]==0)
			{
				f=0;
				break;
			}
			root=ptr[s[i]-'a'][root];
		}
		if(!f)continue;
		ans+=c[ptr[s[len-1]-'a'][root]]+c[ptr[s[len-1]-'a'+26][root]];
	}
	tr(v[len],it)
	{
		string s=*it;
		msk[len]=0;
		ren(i,len-1,0)
		{
			int x=s[i]-'a';
			msk[i]=(msk[i+1]|(1<<x));
		}
		int root=0;
		rep(i,0,len-1)
		{
			int x=s[i]-'a';
			int to=ptr[x][root];
			if(to==0)
			{
				++id;
				to=id;
				ptr[x][root]=id;
			}
			c[to]++;
			if(i<len-1)
			{
				int mask=msk[i+1];
				rep(y,0,25)
				{
					if(((mask>>y)&1)==0)continue;
					if(y==x)continue;
					if(ptr[y+26][root]==0)
					{
						++id;
						ptr[y+26][root]=id;
					}
					c[ptr[y+26][root]]++;
				}
			}
			root=to;
		}
	}
}
ol(ans);
return 0;
}



Compiler output:
/tmp/ccsuponh.o: in function `main':
a.cc:(.text+0xc2): relocation truncated to fit: R_X86_64_PC32 against symbol `v[abi:cxx11]' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x11c): relocation truncated to fit: R_X86_64_PC32 against symbol `v[abi:cxx11]' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x14f): relocation truncated to fit: R_X86_64_PC32 against symbol `v[abi:cxx11]' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x2b9): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x315): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x324): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x32e): relocation truncated to fit: R_X86_64_PC32 against symbol `ans' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x370): relocation truncated to fit: R_X86_64_PC32 against symbol `v[abi:cxx11]' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x3b4): relocation truncated to fit: R_X86_64_PC32 against symbol `v[abi:cxx11]' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x3fd): relocation truncated to fit: R_X86_64_PC32 against symbol `msk' defined in .bss section in /tmp/ccsuponh.o
a.cc:(.text+0x44a): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status