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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

internal partial class Solver {
    public void Run() {
        var n = ni();
        var S = ns(n);
        var hist = new HashMap<ulong, int>();
        Array.Sort(S, (s1, s2) => s1.Length.CompareTo(s2.Length));
        long ans = 0;
        var rhash = new RabinKarpHash();
        foreach (var s in S) {
            rhash.Build(s.Length, i => (uint)(s[i] - 'a'));
            var used = 0;
            for (var i = 0; i < s.Length; i++) {
                used |= 1 << (s[i] - 'a');

                var bit = (ulong)hist[rhash.CalcHash(i + 1, s.Length)];
                ans += PopCount((ulong)used & bit);
            }

            hist[rhash.CalcHash(1, s.Length)] |= 1 << (s[0] - 'a');
        }
        cout.WriteLine(ans);
    }

    private static int PopCount(ulong x) {
        x = (x & 0x5555555555555555UL) + ((x >> 1) & 0x5555555555555555UL);
        x = (x & 0x3333333333333333UL) + ((x >> 2) & 0x3333333333333333UL);
        x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fUL;
        x += x >> 8;
        x += x >> 16;
        x += x >> 32;
        return (int)(x & 0x7FLU);
    }
}


public class HashMap<TKey, TValue> : Dictionary<TKey, TValue> {
    public new TValue this[TKey index] {
        get {
            if (TryGetValue(index, out var value)) {
                return value;
            } else {
                return base[index] = default(TValue);
            }
        }
        set => base[index] = value;
    }
}

public class RabinKarpHash {
    private const ulong MASK30 = (1UL << 30) - 1;
    private const ulong MASK31 = (1UL << 31) - 1;
    private const ulong MOD = (1UL << 61) - 1;
    private const ulong MASK61 = MOD;
    private ulong[] Data;
    private ulong[] Power;
    private readonly uint Base;
    private static readonly uint defaultBase = (uint)new Random().Next(1024, int.MaxValue);

    public RabinKarpHash() : this(defaultBase) { }

    public RabinKarpHash(uint @base) {
        Base = @base;
        Power = new ulong[64];
        Power[0] = 1;
        for (var i = 1; i < Power.Length; i++) {
            Power[i] = CalcMod(Mul(Power[i - 1], Base));
        }
    }

    public void Build(IList<uint> array) {
        Build(array.Count, i => array[i]);
    }

    public void Build(int n, Func<int, uint> create) {
        Data = new ulong[n + 1];
        if (Power.Length < n + 1) {
            var originalSize = Power.Length;
            Array.Resize(ref Power, n + 1);
            for (var i = originalSize; i < Power.Length; i++) {
                Power[i] = CalcMod(Mul(Power[i - 1], Base));
            }
        }
        ulong hash = 0;
        for (var i = 0; i < n; i++) {
            Data[i + 1] = hash = CalcMod(Mul(hash, Base) + create(i) + 1);
        }
    }

    private ulong Mul(ulong a, ulong b) {
        ulong ua = a >> 31, la = a & MASK31;
        ulong ub = b >> 31, lb = b & MASK31;
        var mid = la * ub + ua * lb;
        var midu = mid >> 30;
        var midd = mid & MASK30;
        return ua * ub * 2 + midu + (midd << 31) + la * lb;
    }

    private ulong CalcMod(ulong x) {
        var res = (x >> 61) + (x & MASK61);
        if (res >= MOD) {
            res -= MOD;
        }
        return res;
    }

    public ulong CalcHash(int begin, int end) { // [begin, end)
        return CalcMod(Data[end] + (MOD << 2) - Mul(Data[begin], Power[end - begin]) % MOD);
    }
}
// PREWRITEN CODE BEGINS FROM HERE

public static class StringExtensions {
    public static string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    public static void Main() {
#if LOCAL
        var inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) {
            return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        } else {
            return obj.ToString().Dump();
        }
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        var i = 0;
        var negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) {
                throw new FormatException();
            }
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        var array = new string[size];
        for (var i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        var array = new int[size];
        for (var i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        var array = new long[size];
        for (var i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        var array = new double[size];
        for (var i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) {
                        break;
                    }
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}