本篇博客是看着kuangbin大神的题写的

1.HDU 2222 Keywords Search

  • 题意:求目标串中出现了几个模式串
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    struct Trie
    {
      int next[500010][26],fail[500010],end[500010];
      int root,L;
      int newnode()
      {
          for(int i = 0;i < 26;i++)
              next[L][i] = -1;
          end[L++] = 0;
          return L-1;
      }
      void init()
      {
          L = 0;
          root = newnode();
      }
      void insert(char buf[])
      {
          int len = strlen(buf);
          int now = root;
          for(int i = 0;i < len;i++)
          {
              if(next[now][buf[i]-'a'] == -1)
                  next[now][buf[i]-'a'] = newnode();
              now = next[now][buf[i]-'a'];
          }
          end[now]++;
      }
      void build()
      {
          queue<int>Q;
          fail[root] = root;
          for(int i = 0;i < 26;i++)
              if(next[root][i] == -1)
                  next[root][i] = root;
              else
              {
                  fail[next[root][i]] = root;
                  Q.push(next[root][i]);
              }
          while( !Q.empty() )
          {
              int now = Q.front();
              Q.pop();
              for(int i = 0;i < 26;i++)
                  if(next[now][i] == -1)
                      next[now][i] = next[fail[now]][i];
                  else
                  {
                      fail[next[now][i]]=next[fail[now]][i];
                      Q.push(next[now][i]);
                  }
          }
      }
      int query(char buf[])
      {
          int len = strlen(buf);
          int now = root;
          int res = 0;
          for(int i = 0;i < len;i++)
          {
              now = next[now][buf[i]-'a'];
              int temp = now;
              while( temp != root )
              {
                  res += end[temp];
                  end[temp] = 0;
                  temp = fail[temp];
              }
          }
          return res;
      }
      void debug()
      {
          for(int i = 0;i < L;i++)
          {
              printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
              for(int j = 0;j < 26;j++)
                  printf("%2d",next[i][j]);
              printf("]\n");
          }
      }
    };
    char buf[1000010];
    Trie ac;
    int main()
    {
      int T;
      int n;
      scanf("%d",&T);
      while( T-- )
      {
          scanf("%d",&n);
          ac.init();
          for(int i = 0;i < n;i++)
          {
              scanf("%s",buf);
              ac.insert(buf);
          }
          ac.build();
          scanf("%s",buf);
          printf("%d\n",ac.query(buf));
      }
      return 0;
    }

2.HDU 2896 病毒侵袭

  • 题意:和上一题差不多,要求输出模式串的下标
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    struct Trie
    {
      int next[210*500][128],fail[210*500],end[210*500];
      int root,L;
      int newnode()
      {
          for(int i = 0;i < 128;i++)
              next[L][i] = -1;
          end[L++] = -1;
          return L-1;
      }
      void init()
      {
          L = 0;
          root = newnode();
      }
      void insert(char s[],int id)
      {
          int len = strlen(s);
          int now = root;
          for(int i = 0;i < len;i++)
          {
              if(next[now][s[i]] == -1)
                  next[now][s[i]] = newnode();
              now=next[now][s[i]];
          }
          end[now]=id;
      }
      void build()
      {
          queue<int>Q;
          fail[root] = root;
          for(int i = 0;i < 128;i++)
              if(next[root][i] == -1)
                  next[root][i] = root;
              else
              {
                  fail[next[root][i]] = root;
                  Q.push(next[root][i]);
              }
          while(!Q.empty())
          {
              int now = Q.front();
              Q.pop();
              for(int i = 0;i < 128;i++)
                  if(next[now][i] == -1)
                      next[now][i] = next[fail[now]][i];
                  else
                  {
                      fail[next[now][i]] = next[fail[now]][i];
                      Q.push(next[now][i]);
                  }
          }
      }
      bool used[510];
      bool query(char buf[],int n,int id)
      {
          int len = strlen(buf);
          int now = root;
          memset(used,false,sizeof(used));
          bool flag = false;
          for(int i = 0;i < len;i++)
          {
              now = next[now][buf[i]];
              int temp = now;
              while(temp != root)
              {
                  if(end[temp] != -1)
                  {
                      used[end[temp]] = true;
                      flag = true;
                  }
                  temp = fail[temp];
              }
          }
          if(!flag)return false;
          printf("web %d:",id);
          for(int i = 1;i <= n;i++)
              if(used[i])
                  printf(" %d",i);
          printf("\n");
          return true;
      }
    };
    char buf[10010];
    Trie ac;
    int main()
    {
      int n,m;
      while(scanf("%d",&n) != EOF)
      {
          ac.init();
          for(int i = 1;i <= n;i++)
          {
              scanf("%s",buf);
              ac.insert(buf,i);
          }
          ac.build();
          int ans = 0;
          scanf("%d",&m);
          for(int i = 1;i <= m;i++)
          {
              scanf("%s",buf);
              if(ac.query(buf,n,i))
                  ans++;
          }
          printf("total: %d\n",ans);
      }
      return 0;
    }

    3.HDU 3065 病毒侵袭持续中

题意:还是差不多,就是需要输出每个模式串出现的次数

#include <bits/stdc++.h>
using namespace std;
#define ll long long
char str[1010][100];
struct Trie
{
    int next[1010*50][128],fail[1010*50],end[1010*50];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 128;i++)
            next[L][i] = -1;
        end[L++] = -1;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char s[],int id)
    {
        int len = strlen(s);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][s[i]] == -1)
                next[now][s[i]] = newnode();
            now = next[now][s[i]];
        }
        end[now] = id;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 128;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 128;i++)
                if(next[now][i] == -1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int num[1010];
    void query(char buf[],int n)
    {
        for(int i = 0;i < n;i++)
            num[i] = 0;
        int len=strlen(buf);
        int now=root;
        for(int i=0;i<len;i++)
        {
            now=next[now][buf[i]];
            int temp = now;
            while( temp != root )
            {
                if(end[temp] != -1)
                    num[end[temp]]++;
                temp = fail[temp];
            }
        }
        for(int i = 0;i < n;i++)
            if(num[i] > 0)
                printf("%s: %d\n",str[i],num[i]);
    }

};

char buf[2000010];
Trie ac;
void debug()
{
    for (int i = 0; i < ac.L; i++)
    {
        printf("id = %3d ,fail = %3d ,end = %3d, chi = [",i,ac.fail[i],ac.end[i]);
        for (int j = 0; j < 128; j++)
            printf("%2d ",ac.next[i][j]);
        printf("]\n");
    }
}
int main()
{
    int n;
    while(scanf("%d",&n) == 1)
    {
        ac.init();
        for(int i = 0;i < n;i++)
        {
            scanf("%s",str[i]);
            ac.insert(str[i],i);
        }
        ac.build();
        scanf("%s",buf);
        ac.query(buf,n);
    }
    return 0;
}