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 3: 1


用A串弄个后缀自动机,然后像lcs那样搞,最后统计一下个数

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 300005;
char s[maxn];
int T, n, t = 0;

class SAM
{
const static int maxn = 500005; //节点个数
const static int size = 26; //字符的范围
const static char base = 'a'; //字符的基准

class node
{
public:
node *fa, *next[size];
int len, cnt;
node* clear(int x)
{
fa = 0; len = x;
cnt = 0;
memset(next, 0, sizeof(next));
return this;
}
}nd[maxn], *u[maxn]; //节点的设置

node *root, *last; //根节点,上一个节点
int tot, f[maxn], len; //总节点数
public:
void clear()
{
last = root = &nd[tot = 0];
nd[0].clear(len = 0);
} //初始化
void insert(char ch)
{
len = last->len + 1;
node *p = last, *np = nd[++tot].clear(p->len + 1);
last = np;
int x = ch - base;
while (p&&p->next[x] == 0) p->next[x] = np, p = p->fa;
if (p == 0) { np->fa = root; return; }

node* q = p->next[x];
if (p->len + 1 == q->len) { np->fa = q; return; }

node *nq = nd[++tot].clear(p->len + 1);
for (int i = 0; i < size; i++)
if (q->next[i]) nq->next[i] = q->next[i];
nq->fa = q->fa;
q->fa = np->fa = nq;
while (p &&p->next[x] == q) p->next[x] = nq, p = p->fa;
} //插入操作

void find(char *ch)
{
node *s = root;
int maxlen = 0;
for (int i = 0; ch[i]; i++)
{
int x = ch[i] - base;
if (s->next[x])
{
s = s->next[x];
s->cnt = max(s->cnt, ++maxlen);
}
else
{
while (s&&!s->next[x]) s = s->fa;
if (s == 0) s = root, maxlen = 0;
else
{
maxlen = s->len + 1; s = s->next[x];
s->cnt = max(s->cnt, maxlen);
}
}
}
}
void query()
{
for (int i = 0; i <= len; i++) f[i] = 0;
for (int i = 1; i <= tot; i++) f[nd[i].len]++;
for (int i = 1; i <= len; i++) f[i] += f[i - 1];
for (int i = 1; i <= tot; i++) u[f[nd[i].len]--] = &nd[i];
for (int i = tot; i; i--) u[i]->fa->cnt = max(u[i]->fa->cnt, u[i]->cnt);
LL ans = 0;
for (int i = tot; i; i--)
{
ans += max(0, nd[i].len - max(nd[i].cnt, nd[i].fa->len));
}
printf("%I64d\n", ans);
}
}sam;

int main()
{
scanf("%d", &T);
while (T--)
{
sam.clear();
scanf("%d%s", &n, s);
for (int i = 0; s[i]; i++) sam.insert(s[i]);
while (n--)
{
scanf("%s", s);
sam.find(s);
}
printf("Case %d: ", ++t);
sam.query();
}
return 0;
}


重温了一遍,用后缀数组解决,把A串和全部B串连起来,求出h数组以后,把每一位开头的后缀都处理一遍,再去重。



#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e8;
const int N = 3e5 + 10;
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
int T, cas = 0;

struct Sa
{
char s[N];
int rk[2][N], sa[N], h[N], w[N], now, n, m, len;
//int rmq[N][20], lg[N];
int bel[N], f[N];

bool GetS()
{
scanf("%d%s", &m, s + 1);
for (n = 1; s[n]; n++) bel[n] = 0;
len = n - 1; s[n] = 1; bel[n++] = 2;
rep(i, 1, m)
{
scanf("%s", s + n);
for (; s[n]; n++) bel[n] = 1;
s[n] = 2; bel[n++] = 2;
}
--n;
return true;
}

void getsa(int z, int &m)
{
int x = now, y = now ^= 1;
rep(i, 1, z) rk[y][i] = n - i + 1;
for (int i = 1, j = z; i <= n; i++)
if (sa[i] > z) rk[y][++j] = sa[i] - z;

rep(i, 1, m) w[i] = 0;
rep(i, 1, n) w[rk[x][rk[y][i]]]++;
rep(i, 1, m) w[i] += w[i - 1];
per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
for (int i = m = 1; i <= n; i++)
{
int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
}
}

void getsa(int m)
{
//n = strlen(s + 1);
rk[1][0] = now = sa[0] = s[0] = 0;
rep(i, 1, m) w[i] = 0;
rep(i, 1, n) w[s[i]]++;
rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
rep(i, 1, m) w[i] += w[i - 1];
rep(i, 1, n) rk[0][i] = rk[1][s[i]];
rep(i, 1, n) sa[w[s[i]]--] = i;

rk[1][n + 1] = rk[0][n + 1] = 0; //多组的时候容易出bug
for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);
for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
{
if (rk[now][i] == 1) continue;
int k = n - max(sa[rk[now][i] - 1], i);
while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
}
}

/*void getrmq()
{
h[n + 1] = h[1] = lg[1] = 0;
rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
for (int i = 1; (1 << i) <= n; i++)
{
rep(j, 2, n)
{
if (j + (1 << i) > n + 1) break;
rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
}
}
}

int lcp(int x, int y)
{
int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}
*/
void work()
{
GetS(); getsa(256);
LL ans = 0;
rep(i, 1, n) f[i] = 0;
rep(i, 1, n)
{
if (bel[sa[i]] != 1) continue;
int k = h[i];
per(j, i - 1, 1)
{
if (bel[sa[j]] == 1) break;
if (!bel[sa[j]]) f[sa[j]] = max(f[sa[j]], k);
k = min(k, h[j]);
}
k = h[i + 1];
rep(j, i + 1, n)
{
if (bel[sa[j]] == 1) break;
if (!bel[sa[j]]) f[sa[j]] = max(f[sa[j]], k);
k = min(k, h[j + 1]);
}
}
rep(i, 1, n)
{
if (!bel[i]) ans += len - i + 1 - f[i];
if (i > 1 && !bel[sa[i]] && !bel[sa[i - 1]])
{
ans -= max(0, h[i] - f[sa[i - 1]]);
}
}
printf("Case %d: %lld\n", ++cas, ans);
}
}sa;

int main()
{
T = read(); while (T--) sa.work();
return 0;
}