zoukankan      html  css  js  c++  java
  • 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的。上午gty讲的并没有听懂,只好自己慢慢对着模板理解。

    在HDU上为什么相同的程序提交有时T有时A!!!

    奉上sth神犇的模板(不是这道题):

    var
       ch:char;
       q,g,num:array [0..500001] of longint;
       st:string;
       son:array [0..500001,'a'..'z'] of longint;
       ts:array [0..1000001] of char;
       l,s,t,n,i,j,m,k,ans,head,tail:longint;
    begin
       readln(t);
       while t<>0 do
       begin
          for ch:='a' to 'z' do begin son[0,ch]:=1; son[1,ch]:=0; end; 
          dec(t);
          s:=1;
          g[1]:=0;
          fillchar(num,sizeof(num),0);
          readln(n);
          for i:=1 to n do begin
             readln(st);
             m:=length(st);
             l:=1;
             for j:=1 to m do
                if son[l,st[j]]<>0 then l:=son[l,st[j]]
                   else begin
                      inc(s);
                      g[s]:=0;
                      son[l,st[j]]:=s;
                      l:=s;
                      for ch:='a' to 'z' do son[s,ch]:=0;
                   end;
             inc(num[l]);
          end;
          s:=1;
          head:=0;
          tail:=1;
          q[1]:=1;
          n:=0;
          while head<tail do
          begin
             inc(head);
             k:=q[head];
             for ch:='a' to 'z' do
                if son[k,ch]=0 then son[k,ch]:=son[g[k],ch]
                             else begin g[son[k,ch]]:=son[g[k],ch];
                                        inc(tail);
                                        q[tail]:=son[k,ch]; 
                                  end;
          end;
    

      

    然后是我的ACcode:

    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i));
    using namespace std;
    char a[10010],s[1000010];
    int ch[500010][30],fail[500010],w[500010],ans,cnt;
    inline void ins(char *b){
        int u=0,v,len=strlen(b);
        for2(i,0,len){
            v=b[i]-'a';
            if (!ch[u][v])ch[u][v]=cnt++;
            u=ch[u][v];
        }w[u]++;
    }
    inline void bfs(){
        queue<int>q;
        q.push(0);
        int u,p;
        while (!q.empty()){
            u=q.front();q.pop();
            for2(i,0,26) if (ch[u][i]){
                q.push(ch[u][i]);
                if (!u) continue;
                p=fail[u];
                while (p&&!ch[p][i])p=fail[p];
                fail[ch[u][i]]=ch[p][i];
            }
        }
    }
    inline void ac(char *b){
        int u=0,v,len=strlen(b),t;
        for2(i,0,len){
            v=b[i]-'a';
            while (u&&!ch[u][v])u=fail[u];
            u=ch[u][v];
            t=u;
            while (t){
                ans+=w[t];
                w[t]=0;
                t=fail[t];
            }
        }
    }
    inline void init(){CC(fail,0);CC(ch,0);CC(w,0);ans=0;cnt=1;}
    int main(){
        int T;scanf("%d",&T);
        while (T--){
            init(); int n;
            scanf("%d",&n);
            for1(i,1,n){
                scanf("%s",a);
                ins(a);
            }bfs();
            scanf("%s",s);
            ac(s);
            printf("%d
    ",ans);
        }return 0;
    }
    

      

  • 相关阅读:
    系统维护相关问题
    Python环境维护
    哈希表解决字符串问题
    论文笔记二:《A Tutoral on Spectral Clustering》
    论文笔记之哈希学习比较--《Supervised Hashing with Kernels》《Towards Optimal Binary Code Learning via Ordinal Embedding》《Top Rank Supervised Binary Coding for Visual Search》
    Java中String、StringBuffer、StringBuilder的比较与源 代码分析
    浙大pat1040 Longest Symmetric String(25 分)
    浙大pat1039 Course List for Student(25 分)
    浙大pat---1036 Boys vs Girls (25)
    百炼oj-4151:电影节
  • 原文地址:https://www.cnblogs.com/abclzr/p/5201597.html
Copyright © 2011-2022 走看看