Good Article Good sentence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2586 Accepted Submission(s): 728

Problem Description
In middle school, teachers used to encourage us to pick up pretty sentences so that we could apply those sentences in our own articles. One of my classmates ZengXiao Xian, wanted to get sentences which are different from that of others, because he thought the distinct pretty sentences might benefit him a lot to get a high score in his article.
Assume that all of the sentences came from some articles. ZengXiao Xian intended to pick from Article A. The number of his classmates is n. The i-th classmate picked from Article Bi. Now ZengXiao Xian wants to know how many different sentences she could pick from Article A which don’t belong to either of her classmates?Article. To simplify the problem, ZengXiao Xian wants to know how many different strings, which is the substring of string A, but is not substring of either of string Bi. Of course, you will help him, won’t you?

Input
The first line contains an integer T, the number of test data.
For each test data
The first line contains an integer meaning the number of classmates.
The second line is the string A;The next n lines,the ith line input string Bi.
The length of the string A does not exceed 100,000 characters , The sum of total length of all strings Bi does not exceed 100,000, and assume all string consist only lowercase characters ‘a’ to ‘z’.

Output
For each case, print the case number and the number of substrings that ZengXiao Xian can find.

Sample Input

3
2
abab
ab
ba
1
aaa
bbb
2
aaaa
aa
aaa

Sample Output

Case 1: 3
Case 2: 3
Case 3: 1

Source
2012 ACM/ICPC Asia Regional Hangzhou Online

Recommend
liuyiding | We have carefully selected several similar problems for you: 5201 5197 5196 5195 5193

先把B的所有串连在一起,然后中间用不同的没有出现过的字符隔开,求出后缀数组,求出不同的子串数目,这是独属于B的,把A和B的所有串连在一起,求出后缀数组,再次求出不同子串数目,这是A,B共有的,由三部分组成:A独有的,B独有的,跨越A,B的(这部分就是(l1+1)⋅(l2+1) )
所以A独有的既可以计算出来了

/*************************************************************************
    > File Name: hdu4416.cpp
    > Author: ALex
    
    > Created Time: 2015年04月09日 星期四 16时36分27秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

char str[210000];

class SuffixArray
{
    public:
        static const int N = 420000;
        int init[N];
        int X[N];
        int Y[N];
        int Rank[N];
        int sa[N];
        int height[N];
        int buc[N];
        int size;

        void clear()
        {
            size = 0;
        }

        void insert(int n)
        {
            init[size++] = n;
        }

        bool cmp(int *r, int a, int b, int l)
        {
            return (r[a] == r[b] && r[a + l] == r[b + l]);
        }

        void getsa(int m = 256) //m一般为最大值+1
        {
            init[size] = 0;
            int l, p, *x = X, *y = Y, n = size + 1;
            for (int i = 0; i < m; ++i)
            {
                buc[i] = 0;
            }
            for (int i = 0; i < n; ++i)
            {
                ++buc[x[i] = init[i]];
            }
            for (int i = 1; i < m; ++i)
            {
                buc[i] += buc[i - 1];
            }
            for (int i = n - 1; i >= 0; --i)
            {
                sa[--buc[x[i]]] = i;
            }
            for (l = 1, p = 1; l <= n && p < n; m = p, l *= 2)
            {
                p = 0;
                for (int i = n - l; i < n; ++i)
                {
                    y[p++] = i;
                }
                for (int i = 0; i < n; ++i)
                {
                    if (sa[i] >= l)
                    {
                        y[p++] = sa[i] - l;
                    }
                }
                for (int i = 0; i < m; ++i)
                {
                    buc[i] = 0;
                }
                for (int i = 0; i < n; ++i)
                {
                    ++buc[x[y[i]]];
                }
                for (int i = 1; i < m; ++i)
                {
                    buc[i] += buc[i - 1];
                }
                for (int i = n - 1; i >= 0; --i)
                {
                    sa[--buc[x[y[i]]]] = y[i];
                }
                int i;

                for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i)
                { 
                    x[sa[i]] = cmp(y, sa[i - 1], sa[i], l) ? p - 1 : p++; 
                }
            }
        }

        void getheight()
        {
            int h = 0, n = size;
            for (int i = 0; i <= n; ++i)
            {
                Rank[sa[i]] = i;
            }
            height[0] = 0;
            for (int i = 0; i < n; ++i)
            {
                if (h > 0)
                {
                    --h;
                }
                int j =sa[Rank[i] - 1];
                for (; i + h < n && j + h < n && init[i + h] == init[j + h]; ++h);
                height[Rank[i] - 1] = h;
            }
        }   

        LL solve()
        {
            LL ans = 0;
            for (int i = 1; i <= size; ++i)
            {
                ans += size - sa[i] - height[i - 1];
            }
            return ans;
        }
}SA1, SA2;

int main()
{
    int t;
    int icase = 1;
    scanf("%d", &t);
    while (t--)
    {
        SA1.clear();
        SA2.clear();
        int n;
        LL ans = 0;
        scanf("%d", &n);
        scanf("%s", str);
        int len = strlen(str);
        int sum = 0;
        for (int i = 0; i < len; ++i)
        {
            SA1.insert(str[i] - 'a' + 1);
        }
        SA1.insert(27);
        int m = len;
        int maxs = 28;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%s", str);
            len = strlen(str);
            sum += len;
            for (int j = 0; j < len; ++j)
            {
                SA1.insert(str[j] - 'a' + 1);
                SA2.insert(str[j] - 'a' + 1);
            }
            if (i < n)
            {
                ++sum;
                SA1.insert(maxs);
                SA2.insert(maxs++);
            }
        }
        SA1.getsa(maxs + 1);
        SA1.getheight();
        SA2.getsa(maxs + 1);
        SA2.getheight();
        ans -= SA2.solve();
        ans += SA1.solve();
        ans -= (LL)(m + 1) * (sum + 1);
        printf("Case %d: %lld\n", icase++, ans);
    }
    return 0;
}