首页 > 代码库 > LightOJ 1427 Substring Frequency (II) (AC自动机)

LightOJ 1427 Substring Frequency (II) (AC自动机)

题意:给定一个文本串和 n 个子串,问你子串在文本串出现的次数。

析:很明显的AC自动机,只要把先把子串进行失配处理,然后再去用文本串去匹配,在插入子串时就要标记每个串,注意串可能是相同的,这个我错了两次,最后匹配一次就OK了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

char t[maxn];
char s[510][510];
const int maxnode = 510 * 510 + 10;
struct Aho{
  int ch[maxnode][26];
  int f[maxnode];
  int val[maxnode];
  int last[maxnode];
  int cnt[510];
  int sz;

  void init(){
    sz = 1;
    memset(ch[0], 0, sizeof ch[0]);
    memset(cnt, 0, sizeof cnt);
  }

  int idx(char c){ return c - ‘a‘; }

  void insert(char *s, int v){
    int u = 0;
    while(*s){
      int c = idx(*s);
      if(!ch[u][c]){
        memset(ch[sz], 0, sizeof ch[sz]);
        val[sz] = 0;
        ch[u][c] = sz++;
      }
      u = ch[u][c];
      ++s;
    }
    val[u] = v;
  }

  void getFail(){
    queue<int> q;
    f[0] = 0;
    for(int c = 0; c < 26; ++c){
      int u = ch[0][c];
      if(u){ f[u] = 0; q.push(u); last[u] = 0; }
    }
    while(!q.empty()){
      int r = q.front();  q.pop();
      for(int c = 0; c < 26; ++c){
        int u = ch[r][c];
        if(!u)  continue;
        q.push(u);
        int v = f[r];

        while(v && !ch[v][c]) v = f[v];
        f[u] = ch[v][c];
        last[u] = val[f[u]] ? f[u] : last[f[u]];
      }
    }
  }

  void find(char *s){
    int j = 0;
    while(*s){
      int c = idx(*s);
      while(j && !ch[j][c])  j = f[j];
      j = ch[j][c];
      if(val[j])  print(j);
      else if(last[j])  print(last[j]);
      ++s;
    }
  }

  void print(int j){
    if(j){
      ++cnt[val[j]];
      print(last[j]);
    }
  }

};

Aho aho;
map<string, int> mp;

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    scanf("%d", &n);
    scanf("%s", t);
    aho.init();
    mp.clear();
    for(int i = 1; i <= n; ++i){
      scanf("%s", s+i);
      if(mp.count(s[i]))  continue;
      mp[s[i]] = i;
      aho.insert(s[i], i);
    }
    aho.getFail();
    aho.find(t);
    printf("Case %d:\n", kase);
    for(int i = 1; i <= n; ++i)
      printf("%d\n", aho.cnt[mp[s[i]]]);
  }
  return 0;
}

  

LightOJ 1427 Substring Frequency (II) (AC自动机)