2015
07-16

# Good Article Good sentence

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?

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’.

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’.

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

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

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>

using namespace std;
typedef long long LL;
const int N=550005;

struct State
{
State *pre,*go[26];
int step,deep;
void clear()
{
pre=0;
deep=0;
step=0;
memset(go,0,sizeof(go));
}
}*root,*last;

State statePool[N*2],*b[2*N],*cur;

void init()
{
cur=statePool;
root=last=cur++;
root->clear();
}

void Insert(int w)
{
State *p=last;
State *np=cur++;
np->clear();
np->step=p->step+1;
while(p&&!p->go[w])
p->go[w]=np,p=p->pre;
if(p==0)
np->pre=root;
else
{
State *q=p->go[w];
if(p->step+1==q->step)
np->pre=q;
else
{
State *nq=cur++;
nq->clear();
memcpy(nq->go,q->go,sizeof(q->go));
nq->step=p->step+1;
nq->pre=q->pre;
q->pre=nq;
np->pre=nq;
while(p&&p->go[w]==q)
p->go[w]=nq, p=p->pre;
}
}
last=np;
}

char str[N];
int cnt[N];

void Solve(int Q,int n)
{
memset(cnt,0,sizeof(cnt));
for(State *p=statePool; p!=cur; p++)
cnt[p->step]++;
for(int i=1; i<=n; i++)
cnt[i]+=cnt[i-1];
for(State *p=statePool; p!=cur; p++)
b[--cnt[p->step]]=p;
while(Q--)
{
scanf("%s",str);
int len=0;
int m=strlen(str);
State *p=root;
for(int i=0; i<m; i++)
{
int x=str[i]-'a';
if(p->go[x])
{
len++;
p=p->go[x];
p->deep=max(p->deep,len);
}
else
{
while(p&&!p->go[x]) p=p->pre;
if(!p) p=root,len=0;
else
{
len=p->step+1;
p=p->go[x];
p->deep=max(p->deep,len);
}
}
}
}
LL sum=0;
int num=cur-statePool;
for(int i=num-1; i>0; i--)
{
State *q=b[i];
if(q->deep>0)
{
q->pre->deep=max(q->pre->deep,q->deep);
if(q->deep<q->step) sum+=q->step-q->deep;
}
else sum+=q->step-q->pre->step;
}
printf("%I64d\n",sum);
}

int main()
{
int t,k=1,Q;
scanf("%d",&t);
while(t--)
{
printf("Case %d: ",k++);
scanf("%d",&Q);
scanf("%s",str);
int n=strlen(str);
init();
for(int i=0; i<n; i++)
Insert(str[i]-'a');
Solve(Q,n);
}
return 0;
}


1. 呵呵。原来女人也很色,那天看到办公室女-同事在偷偷的上这种网站：www。g9z。net 如果她老公知道了一定会被气死…………吱吱