序列自动机

#include 
using namespace std;
const int MaxN = 1000000 + 5;
int N, L;
char str[MaxN], str2[MaxN];
struct SAM {
  int cntv;
  int nxt[MaxN];
  int last[26], ch[26][MaxN];
  SAM() { nxt[0] = -1; }
  inline void insert(int c) {
    cntv++; nxt[cntv] = last[c];
    for (int i = 0; i < 26; ++i)
      for (int p = last[i]; p != -1 && ch[c][p] == 0; p = nxt[p])
        ch[c][p] = cntv;
    last[c] = cntv;
  }
  inline bool check(int u, int x) {
    if (x == L) return true;
    int c = str2[x] - 'a';
    if (ch[c][u] == 0) return false;
    return check(ch[c][u], x + 1);
  }
} T;
void init() {
  scanf("%s", str);
  scanf("%d", &N);
  for (int i = 0; str[i]; ++i) T.insert(str[i] - 'a');
}
void solve() {
  for (int i = 1; i <= N; ++i) {
    scanf("%s", str2);
    L = strlen(str2);
    puts(T.check(0, 0) ? "Yes" : "No");
  }
}
int main() {
  init();
  solve();
  return 0;
}

网页名称:序列自动机
文章位置:http://pwwzsj.com/article/godige.html