HDU 4416 good article good sentence (suffix array & Thinking)

Source: Internet
Author: User
Good article good sentence Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2308 accepted submission (s): 649


Problem descriptionin middle school, teachers used to encourage us to pick up pretty sentences so that we cocould 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. 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 cocould 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?
 
Inputthe 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 '.
 
Outputfor each case, print the case number and the number of substrings that Zengxiao Xian can find.
Sample Input
32abababba1aaabbb2aaaaaaaaa
 
Sample output
Case 1: 3Case 2: 3Case 3: 1
 
Source2012 ACM/ICPC Asia Regional Hangzhou online
Recommendliuyiding | we have carefully selected several similar problems for you: 5041 5040 5039 5038 question: Give You A string with a maximum length of 1 E5. Then there are n B strings. The total length of all B strings cannot exceed 1e5. Now you are asked how many different and not any B substrings A has. Idea: it is similar to how many different substrings the suffix array requires. The difficulty lies in how to deal with the problem that it is not a substring of B. First, it must be to concatenate all the strings. Separated by the $ symbol. Note that the array must be larger. For this reason, re-launch. Then, for all SA [I] <Lena positions, Lena is the string length of string. Lena-sa [I] is the total number of substrings corresponding to the I Extension of string. Lena-sa [I]-height [I] is the number of different substrings that are separated from the previous position. Lena-sa [I]-height [I]-LCP [I, j]. J is the suffix of the first B under I. The number of different substrings left after removing the same substring as B. How to find LCP [I, j. You can just scan it from the back to the front. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=350010;typedef long long ll;char txt[maxn];int sa[maxn],T1[maxn],T2[maxn],ct[maxn],he[maxn],rk[maxn],ans,n,m,lena;int bi[maxn];void getsa(char *st){    int i,k,p,*x=T1,*y=T2;    for(i=0; i<m; i++) ct[i]=0;    for(i=0; i<n; i++) ct[x[i]=st[i]]++;    for(i=1; i<m; i++) ct[i]+=ct[i-1];    for(i=n-1; i>=0; i--)        sa[--ct[x[i]]]=i;    for(k=1,p=1; p<n; k<<=1,m=p)    {        for(p=0,i=n-k; i<n; i++) y[p++]=i;        for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k;        for(i=0; i<m; i++) ct[i]=0;        for(i=0; i<n; i++) ct[x[y[i]]]++;        for(i=1; i<m; i++) ct[i]+=ct[i-1];        for(i=n-1; i>=0; i--) sa[--ct[x[y[i]]]]=y[i];        for(swap(x,y),p=1,x[sa[0]]=0,i=1; i<n; i++)            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;    }}void gethe(char *st){    int i,j,k=0;    for(i=0;i<n;i++) rk[sa[i]]=i;    for(i=0;i<n-1;i++)    {        if(k) k--;        j=sa[rk[i]-1];        while(st[i+k]==st[j+k]) k++;        he[rk[i]]=k;    }}int main(){    int t,cas=1,nm,i,nl,tp,bb;    ll ans;    scanf("%d",&t);    while(t--)    {        scanf("%d",&nm);        scanf("%s",txt);        n=lena=strlen(txt);        for(i=0;i<nm;i++)        {            txt[n++]='$';            scanf("%s",txt+n);            nl=strlen(txt+n);            n+=nl;        }        m=150;        n++;        getsa(txt);        gethe(txt);        n--;        ans=bb=0;        for(i=n;i>=1;i--)        {            if(sa[i]>=lena)                bb=he[i];            else               bi[i]=bb,bb=min(bb,he[i]);        }        for(i=1;i<=n;i++)        {            if(sa[i]<lena)            {                tp=lena-sa[i]-min(lena-sa[i],max(he[i],bi[i]));                ans+=tp;            }        }        printf("Case %d: %I64d\n",cas++,ans);    }    return 0;}


HDU 4416 good article good sentence (suffix array & Thinking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.