Good Article Good sentence

40 篇文章 0 订阅
28 篇文章 9 订阅

题目描述

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

输出描述:

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

输入

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

输出

Case 1: 3
Case 2: 3
Case 3: 1
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1<<28
#define M 6000005
#define N 1000005
#define maxn 300005
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define MOD 1000000007
using namespace std;
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn]; 
int cmp(int *r,int a,int b,int l) 
{return r[a]==r[b]&&r[a+l]==r[b+l];} 
void da(const int *r,int *sa,int n,int m){ 
    int i,j,p,*x=wa,*y=wb,*t;  
    for(i=0;i<m;i++) Ws[i]=0;  
    for(i=0;i<n;i++) Ws[x[i]=r[i]]++;  
    for(i=1;i<m;i++) Ws[i]+=Ws[i-1];  
    for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;  
    for(j=1,p=1;p<n;j*=2,m=p){  
        for(p=0,i=n-j;i<n;i++) y[p++]=i;  
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;  
        for(i=0;i<n;i++) wv[i]=x[y[i]];  
        for(i=0;i<m;i++) Ws[i]=0;  
        for(i=0;i<n;i++) Ws[wv[i]]++;  
        for(i=1;i<m;i++) Ws[i]+=Ws[i-1];  
        for(i=n-1;i>=0;i--) sa[--Ws[wv[i]]]=y[i];  
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)  
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;  
    }  
    return;  
} 
int sa[maxn],Rank[maxn],height[maxn]; 
void calheight(const int *r,int *sa,int n){ 
    int i,j,k=0; 
    for(i=1;i<=n;i++) Rank[sa[i]]=i; 
    for(i=0;i<n;height[Rank[i++]]=k) 
        for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++); 
    return; 
}
int a[maxn],pos[maxn];
char str[maxn];
int main(){
    int t,cas=0,n;
    scanf("%d",&t);
    while(t--){
        int len,m=0,num=30;
        scanf("%d",&n);
        scanf("%s",str);
        len=strlen(str);
        for(int i=0;i<len;i++) a[m++]=str[i]-'a'+1;
        for(int i=0;i<n;i++){
            a[m++]=num++;
            scanf("%s",str);
            int l=strlen(str);
            for(int j=0;j<l;j++)
                a[m++]=str[j]-'a'+1;
        }
        a[m]=0;
        da(a,sa,m+1,num+1);
        calheight(a,sa,m);
        mem(pos,0);
        int tmp=inf;
        for(int i=1;i<=m;i++){
            if(sa[i]<len){
                if(height[i]<tmp) tmp=height[i];
                if(pos[sa[i]]<tmp) pos[sa[i]]=tmp;
            }
            else tmp=inf;
        }
        tmp=inf;
        for(int i=m;i>=1;i--){
            if(sa[i-1]<len){
                if(height[i]<tmp) tmp=height[i];
                if(pos[sa[i-1]]<tmp) pos[sa[i-1]]=tmp;
            }
            else tmp=inf;
        }
        for(int i=1;i<=m;i++)
             if(sa[i]<len&& sa[i-1]<len)
                if(pos[sa[i-1]]<height[i]) 
                    pos[sa[i-1]]=height[i]; 
        LL ans=(LL)len*(len+1)/2;
        for(int i=0;i<len;i++)
            ans-=pos[i];
        printf("Case %d: %I64d\n",++cas,ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值