zoukankan      html  css  js  c++  java
  • HDU 4416 Good Article Good sentence

    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?

    solution

    以A建立SAM
    在字符串中一个节点 (p) 可以接受的子串为 (len[p]),但是有一些被父亲节点接受了,所以他实际只可以接受 (len[p]-len[fa[p]]),所以我们拿 (bi...bn) 在上面跑,记录一个在一个节点最多被匹配的长度 (f[i]),每一个节点的贡献就是 (Max(len[i]-Max(f[i],len[fa[i]]),0)),注意 (f[i]) 要向父亲上传

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    const int N=200005;
    char s[N];int m,n,cnt=1,len[N],ch[N][27],p,cur=1,fa[N];
    void build(int c,int id){
       p=cur;cur=++cnt;len[cur]=id;
       for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur;
       if(!ch[p][c])fa[cur]=1;
       else{
          int q=ch[p][c];
          if(len[p]+1==len[q])fa[cur]=q;
          else{
             int nt=++cnt;len[nt]=len[p]+1;
             memcpy(ch[nt],ch[q],sizeof(ch[q]));
             fa[nt]=fa[q];fa[q]=fa[cur]=nt;
             for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt;
          }
       }
    }
    int kase=0,f[N];
    void matcher(){
       int c,l=strlen(s+1),p=1,x=0;
       for(int i=1;i<=l;i++){
          c=s[i]-'a';
          if(!ch[p][c]){
             while(p && !ch[p][c])p=fa[p];
             if(!p)p=1,x=0;
             else x=len[p]+1,p=ch[p][c];
          }
          else x++,p=ch[p][c];
          if(x>f[p])f[p]=x;
       }
    }
    int c[N],sa[N];
    void Clear(){
       memset(c,0,sizeof(c));cnt=1;cur=1;
       memset(ch,0,sizeof(ch));memset(f,0,sizeof(f));
       memset(fa,0,sizeof(fa));
    }
    void work()
    {
       Clear();
       scanf("%d%s",&m,s+1);
       n=strlen(s+1);
       for(int i=1;i<=n;i++)build(s[i]-'a',i);
       for(int i=1;i<=m;i++){
          scanf("%s",s+1);
          matcher();
       }
       for(int i=1;i<=cnt;i++)c[len[i]]++;
       for(int i=1;i<=n;i++)c[i]+=c[i-1];
       for(int i=cnt;i;i--)sa[c[len[i]]--]=i;
       for(int i=cnt,x;i;i--){
          x=sa[i];
          f[fa[x]]=Max(f[fa[x]],f[x]);
       }
       long long ans=0,tmp;
       for(int i=1;i<=cnt;i++){
          tmp=Max(f[i],len[fa[i]]);
          ans+=Max(len[i]-tmp,0);
       }
       printf("Case %d: %lld
    ",++kase,ans);
    }
    
    int main()
    {
    	int T;cin>>T;
            while(T--)work();
    	return 0;
    }
    
    
  • 相关阅读:
    QT 双缓冲
    ubuntu 安装SVN客户端
    高并发linux内核网络参数调优
    c aes 解密
    qt 字符类型转换
    c++ ado 操作类
    Qt中将QString转换为char *或者相反
    c++ aes 加密 解密
    [转载]python编码转换遇到的非法字符的解决方法
    [转载] python异常如何全面捕获
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7672461.html
Copyright © 2011-2022 走看看