编程介的小学生 2017-02-09 16:43 采纳率: 20.5%
浏览 987
已采纳

Good Article Good sentence

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

  • 写回答

3条回答 默认 最新

  • devmiao 2017-02-13 18:09
    关注

    #include
    #include
    #include
    #include

    using namespace std;

    const int maxn=110000;

    struct SAM_Node
    {
    SAM_Node *fa,*next[26];
    int len,id,pos;
    SAM_Node(){}
    SAM_Node(int _len)
    {
    len=_len;
    fa=0; memset(next,0,sizeof(next));
    }
    };

    SAM_Node SAM_node[maxn*2],*SAM_root,*SAM_last;
    int SAM_size;

    SAM_Node *newSAM_Node(int len)
    {
    SAM_node[SAM_size]=SAM_Node(len);
    SAM_node[SAM_size].id=SAM_size;
    return &SAM_node[SAM_size++];
    }

    SAM_Node *newSAM_Node(SAM_Node *p)
    {
    SAM_node[SAM_size]=*p;
    SAM_node[SAM_size].id=SAM_size;
    return &SAM_node[SAM_size++];
    }

    void SAM_init()
    {
    SAM_size=0;
    SAM_root=SAM_last=newSAM_Node(0);
    SAM_node[0].pos=0;
    }

    void SAM_add(int x,int len)
    {
    SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);
    np->pos=len; SAM_last=np;
    for(;p&&!p->next[x];p=p->fa)
    p->next[x]=np;
    if(!p)
    {
    np->fa=SAM_root;
    return ;
    }
    SAM_Node *q=p->next[x];
    if(q->len==p->len+1)
    {
    np->fa=q;
    return ;
    }
    SAM_Node *nq=newSAM_Node(q);
    nq->len=p->len+1;
    q->fa=nq; np->fa=nq;
    for(;p&&p->next[x]==q;p=p->fa)
    p->next[x]=nq;
    }

    char A[maxn],B[maxn];
    int c[maxn*2],LCS[maxn*2];
    SAM_Node *top[maxn*2];

    int main()
    {
    int T_T,T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
    scanf("%d",&T);
    scanf("%s",A);
    int len=strlen(A);
    SAM_init();
    for(int i=0;i<len;i++)
    SAM_add(A[i]-'a',i+1);

        memset(c,0,sizeof(c));
        memset(LCS,0,sizeof(LCS));
        memset(top,0,sizeof(top));
    
        for(int i=0;i<SAM_size;i++)
            c[SAM_node[i].len]++;
        for(int i=1;i<=len;i++)
            c[i]+=c[i-1];
        for(int i=0;i<SAM_size;i++)
            top[--c[SAM_node[i].len]]=&SAM_node[i];
        while(T--)
        {
            scanf("%s",B);
            int len2=strlen(B);
            int temp=0; SAM_Node *now=SAM_root;
    
            for(int i=0;i<len2;i++)
            {
                int x=B[i]-'a';
                if(now->next[x])
                {
                    temp++;
                    now=now->next[x];
                    LCS[now->id]=max(LCS[now->id],temp);
                }
                else
                {
                    while(now&&!now->next[x])
                        now=now->fa;
                    if(now)
                    {
                        temp=now->len+1;
                        now=now->next[x];
                        LCS[now->id]=max(LCS[now->id],temp);
                    }
                    else
                    {
                        temp=0; now=SAM_root;
                    }
                }
            }
        }
        long long int ans=0;
        for(int i=SAM_size-1;i>=1;i--)
        {
            SAM_Node *p=top[i];
            if(LCS[p->id])
            {
                if(p->fa)
                    LCS[p->fa->id]=max(LCS[p->fa->id],LCS[p->id]);
                if(LCS[p->id]<p->len)
                {
                    ans+=p->len-LCS[p->id];
                }
            }
            else
            {
                ans+=p->len-p->fa->len;
            }
        }
        printf("Case %d: %I64d\n",cas++,ans);
    }
    return 0;
    

    }
    http://blog.csdn.net/ck_boss/article/details/37346949

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?