HDU - 2222 HDU - 2896 HDU - 3065 AC自动机模板-数组实现

记一下数组+node实现+封装的新板子

http://acm.hdu.edu.cn/showproblem.php?pid=2222

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=1e6+10,maxm=2e6+10;
int casn,n,m;
const int csize=26,minc='a';
class autom{public:
#define nd node[now]
  struct acnode{int cnt,son[csize],fail;}node[maxn];
  int sz=0;
  void clear(int n=maxn-5){
    memset(node,0,sizeof(acnode)*n);
    sz=0;
  }
  void insert(char *s,int len){
    int now=0;
    rep(i,0,len-1){
      int ch=s[i]-minc;
      if(!nd.son[ch]) nd.son[ch]=++sz;
      now=nd.son[ch];
    }
    node[now].cnt++;
  }
  void init(){
    queue<int> que;
    int now=0;
    rep(i,0,csize-1) if(nd.son[i])
      que.push(nd.son[i]);
    while(!que.empty()){
      now=que.front();que.pop();
      rep(i,0,csize-1){
        if(nd.son[i]) {
          node[nd.son[i]].fail=node[nd.fail].son[i];
          que.push(nd.son[i]);
        }else nd.son[i]=node[nd.fail].son[i];
      }
    }
  }
  int query(char *t,int len){
    int now=0,res=0;
    rep(i,0,len-1) {
      now=nd.son[t[i]-minc];
      for(int j=now;j&&node[j].cnt!=-1;j=node[j].fail)
        res+=node[j].cnt,node[j].cnt=-1;
    }
    return res;
  }
}acam;
char s[maxm];
int main(){IO;
  cin>>casn;
  while(casn--){
    cin>>n;
    acam.clear();
    rep(i,1,n){
      cin>>s;
      acam.insert(s,strlen(s));
    }
    acam.init();
    cin>>s;
    cout<<acam.query(s,strlen(s))<<endl;
  }
}

 http://acm.hdu.edu.cn/showproblem.php?pid=2896

#include <bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(ii,x) for(int ii=head[x];ii;ii=e[ii].next)
#pragma GCC optimize("Ofast")
#define show(x) cout<<#x<<"="<<x<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show5(v,w,x,y,z) cout<<#v<<"="<<v<<" "<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="<<a[b]<<endl
using namespace std;
const int maxn=1e6+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double PI=acos(-1.0);
//head
int casn,n,m;
const int csize=128,minc=0;
class autom{public:
#define nd node[now]
  struct acnode{int id,son[csize],fail;}node[maxn];
  int sz=0;
  queue<int> que;
  void clear(int n=maxn-5){
    memset(node,0,sizeof(acnode)*n);
    sz=0;
  }
  void insert(int id,char *s,int len=0){
    if(!len)len=strlen(s);
    int now=0;
    rep(i,0,len-1){
      int ch=s[i]-minc;
      if(!nd.son[ch]) nd.son[ch]=++sz;
      now=nd.son[ch];
    }
    nd.id=id;
  }
  void init(){
    int now=0;
    rep(i,0,csize-1) if(nd.son[i])
      que.push(nd.son[i]);
    while(!que.empty()){
      now=que.front();que.pop();
      rep(i,0,csize-1){
        if(nd.son[i]) {
          node[nd.son[i]].fail=node[nd.fail].son[i];
          que.push(nd.son[i]);
        }else nd.son[i]=node[nd.fail].son[i];
      }
    }
  }
  vector<int> query(char *t,int len=0){
    if(!len)len=strlen(t);
    int now=0;
    vector<int> ans;
    rep(i,0,len-1) {
      now=nd.son[t[i]-minc];
      for(int j=now;j;j=node[j].fail)
        if(node[j].id)ans.push_back(node[j].id);
    }
    return ans;
  }
}acam;
vector<int> ans;
char s[maxm];
int main(){IO;
  cin>>n;
  rep(i,1,n) {
    cin>>s;
    acam.insert(i,s);
  }
  acam.init();
  cin>>m;
  int cnt=0;
  rep(i,1,m){
    cin>>s;
    vector<int>ans=acam.query(s);
    sort(all(ans));
    int num=unique(all(ans))-ans.begin();
    if(ans.size()){
      cout<<"web "<<i<<":";
      rep(i,0,num-1) cout<<' '<<ans[i];
      cout<<endl;
      cnt++;
    }
  }
  cout<<"total: "<<cnt<<endl;
}

 http://acm.hdu.edu.cn/showproblem.php?pid=3065

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(ii,x) for(int ii=head[x];ii;ii=e[ii].next)
#pragma GCC optimize("Ofast")
#define show(x) cout<<#x<<"="<<x<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show5(v,w,x,y,z) cout<<#v<<"="<<v<<" "<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="<<a[b]<<endl
using namespace std;
const int maxn=1e6+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double PI=acos(-1.0);
//head
int casn,n,m;
const int csize=128,minc=0;
int cnt[maxn];
class autom{public:
#define nd node[now]
  struct acnode{int id,son[csize],fail;}node[maxn];
  int sz=0;
  queue<int> que;
  void clear(){
    memset(node,0,sizeof(acnode)*(sz+1));
    sz=0;
  }
  void insert(int id,char *s,int len=0){
    if(!len)len=strlen(s);
    int now=0;
    rep(i,0,len-1){
      int ch=s[i]-minc;
      if(!nd.son[ch]) nd.son[ch]=++sz;
      now=nd.son[ch];
    }
    nd.id=id;
  }
  void init(){
    int now=0;
    rep(i,0,csize-1) if(nd.son[i])
      que.push(nd.son[i]);
    while(!que.empty()){
      now=que.front();que.pop();
      rep(i,0,csize-1){
        if(nd.son[i]) {
          node[nd.son[i]].fail=node[nd.fail].son[i];
          que.push(nd.son[i]);
        }else nd.son[i]=node[nd.fail].son[i];
      }
    }
  }
  void count(int now){
    while(now){
      if(nd.id)cnt[nd.id]++;
      now=nd.fail;
    }
  }
  void query(char *t,int len=0){
    if(!len)len=strlen(t);
    int now=0;
    rep(i,0,len-1) {
      now=nd.son[t[i]-minc];
      for(int j=now;j;j=node[j].fail)
        if(node[j].id) {
            cnt[node[j].id]++;
//          count(j);
        }
    }
  }
}acam;
vector<int> ans;
char s[maxm],p[maxn/10][60];
int main(){IO;
  while(cin>>n){
      acam.clear();
      rep(i,1,n) {
        cin>>p[i];
        acam.insert(i,p[i]);
        cnt[i]=0;
      }
      acam.init();
      cin>>s;
      acam.query(s);
      rep(i,1,n)if(cnt[i])
        cout<<p[i]<<": "<<cnt[i]<<endl;
  }
}

 

posted @ 2019-07-23 13:36  nervending  阅读(197)  评论(0编辑  收藏  举报