HDU 6096 String (AC自动机)

题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s 。要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量。

析:真是觉得没有思路啊,看了官方题解,真是好复杂。

假设原始的字符串 数组为A,首先将A中的每个字符串都进行翻转,得到字符串数组B,然后,将A和B按字典序排序。

对于一个查询来说有一个前缀p和后缀s, 所有包含前缀p的字符串在A中是连续的,可通过二分求出该区间 设为[Lp,Rp],同样,所有包含后缀s的字符串在B中也是连续的,设为[Ls,Rs]

接下来只需求解 有多少个字符串前缀是在[Lp,Rp] 同时后缀在[Ls,Rs]。对于每个字符串,假设在A中是第x个,在B中是第y个 ,那么我们只需要判断有多少个字符串 Lp<=x<=Rp 同时 Ls<=y<=Rs

该问题转化为,有一些点(每个字符串相当于一个点,x是按前缀排完序的位置,y是按后缀排序),现给定一些矩形(每个查询可转化为 Lp<=x<=Rp,Ls<=y<=Rs),问矩形中包含多少个点,该问题是经典的矩形覆盖问题,线段树+扫描线 即可求出。

按上述方法求出后,会存在重叠的问题 。如有一个字符串 aaa 查询如果为 aa aa的话也会查到 aaa。 那么我们需要进行去重,可直接对查询的前缀或者后缀做一个遍历,枚举重叠的长度,然后再哈希判断是否存在这样的原始字符串即可。

时间复杂度 O(n\log(n)+|S|)O(nlog(n)+∣S∣)

避免hash可以离线暴力在字典树上建线段树,查询在字典树上找到后缀对应节点查找前缀区间和。空间O(|S|log(n))O(∣S∣log(n))。时间复杂度 O(n\log(n)+|S|)O(nlog(n)+∣S∣)

也可以直接hash离线做。

后来还是在网上看到了大佬们的解法,真是奇妙。

离线操作,先把前缀和后缀通过 s + ‘{‘ + p,作为模板插入到AC 自动机中去,然后再对原来的文本进行查询,先把文本构造成(假设文本是abcd) abcd{abcd,这样的就可以使用AC自动机进行查询了,还要判断是不是重叠,这可以通过比较字符串的长度来判断。注意去重,因为没有去重WA到死、、、

代码如下:

#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 <assert.h>
#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 = 0xffffffffffLL;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 100000 + 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;
}

const int sigma = 27;
const int maxnode = 2200000 + 1000;

struct Aho{
  int ch[maxnode][sigma];
  int val[maxnode];
  int f[maxnode], last[maxnode];
  int ans[maxn], len[maxnode];
  int sz;
  void init(){
    sz = 1;
    memset(ch[0], 0, sizeof ch[0]);
    memset(ans, 0, sizeof ans);
  }

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

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

  void getFail(){
    queue<int> q;
    f[0] = 0;
    for(int c = 0; c < sigma; ++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 < sigma; ++c){
        int u = ch[r][c];
        if(!u){ ch[r][c] = ch[f[r]][c];  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 query(const char *T, int n){
    int j = 0;
    for(int i = 0; T[i]; ++i){
      int c = idx(T[i]);
      j = ch[j][c];
      if(val[j])  print(j, n);
      else if(last[j])  print(last[j], n);
    }
  }

  void print(int j, int n){
    if(!j)  return ;
    if(len[j] <= n)  ++ans[val[j]];
    print(last[j], n);
  }
};

Aho aho;
char *s[maxn];
char str[maxnode];
char s1[maxn], s2[maxn];
int len[maxn], pos[maxn];

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d %d", &n, &m);
    int cnt = 0;
    for(int i = 1; i <= n; ++i){
      s[i] = str + cnt;
      scanf("%s", s[i]);
      len[i] = strlen(s[i]) + 1;
      cnt += len[i];
      strcpy(str+cnt, s[i]);
      str[cnt-1] = ‘{‘;
      cnt += len[i];
    }

    aho.init();
    for(int i = 1; i <= m; ++i){
      scanf("%s %s", s1+1, s2);
      s1[0] = ‘{‘;
      strcat(s2, s1);
      pos[i] = aho.insert(s2, i);
    }

    aho.getFail();
    for(int i = 1; i <= n; ++i)
      aho.query(s[i], len[i]);
    for(int i = 1; i <= m; ++i)
      printf("%d\n", aho.ans[pos[i]]);
  }
  return 0;
}

/*
2

4 4
aba
cde
acdefa
cdef
a a
cd ef
ac a
ce f

1 1
aaa
aa aa

*/

  

时间: 2024-08-27 03:27:56

HDU 6096 String (AC自动机)的相关文章

2017多校第6场 HDU 6096 String AC自动机

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: 因为要求前缀后缀都包含的个数,所以可以把字符串a转换成a#a这样一个字符串,比如abca就转换成abca#abca 然后对于一组前缀a后缀b转换成b{a,比如ab ca,就是ca{ab, 然后对前缀后缀的串建立AC自动机,让主串去匹配,如上述例子,ca{ab满足为abca{abca的一个子串,也就

HDU 6096 String(AC自动机)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6096 [题目大意] 给出一些字符串,给出前缀后缀模式询问,问有多少字符串符合该模式 [题解] 我们将字符串变为双倍,在中间增加拼接符, 对于每个前后缀模式,我们将其处理为[后缀+拼接符+前缀]的形式, 那么原题等价于统计前后缀模式在多少个字符串中出现, 我们用所有的前后缀模式建立AC自动机,用每个字符串在AC自动机上跑匹配, 最后统计fail链前继累加和即可. [代码] #include <cs

HDU 6096 String(AC自动机+树状数组)

题意 给定 \(n\) 个单词,\(q\) 个询问,每个询问包含两个串 \(s_1,s_2\),询问有多少个单词以 \(s_1\) 为前缀, \(s_2\) 为后缀,前后缀不能重叠. \(1 \leq n,q \leq 10^5\) 思路 字符串题有一个小技巧,拼接字符串,中间加上连接符.如这道题,可以将查询变成 \(s_2+\text{\{}+s_1\) 的形式,相应的,把单词 \(T\) 变为 \(T+\text{\{}+T\) 的形式.那么就是普通的匹配问题了. 对于询问建立\(\text

HDU - 6086 Rikka with String AC自动机 + dp

HDU - 6086 前缀和后缀分别建AC自动机, 考虑从两端往中间dp dp[ o ][ i ][ j ][ mask ] 表示放了前面和后面o个, 第一个自动机在 i 位置, 第二个自动机在 j 位置, 拥有的目标串的状态是mask的方案数. 对于跨过两端的东西, 我们最后处理就好了. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #def

HDU 2222(AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题思路: AC自动机模板题. 一开始使用LRJ的坑爹静态模板,不支持重复的模式串. 在做AC自动机+DP的时候,扒了zcwwzdjn大神的动态优化(失配指向root)写法,以及借鉴了网上的AC自动机模板, 搞出了这么一个支持重复串的模板. #include "cstdio" #include

HDU 2296 Ring [AC自动机 DP 打印方案]

Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3536 Accepted Submission(s): 1153 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic

zoj 3228 Searching the String(AC自动机)

题目连接:zoj 3228 Searching the String 题目大意:给定一个字符串,然后现在有N次询问,每次有一个type和一个子串,问说子串在字符串中出现几次,type 为0时为可重叠,为1时为不可重叠. 解题思路:不过没有type=1的限制,那么就是普通的AC自动机匹配问题,对于不可重叠问题,可以对于每个节点记录 一下上一次匹配到的pos,用当前匹配的i减掉pos看有没有超过长度,有超过即为合法匹配,否则忽略. 题目中有很多相同的子串,一开始我用jump数组用类似链表的形式记录每

HDU 2222 (AC自动机模板题)

题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string&g

HDU 2296 Ring AC自动机 + DP

题意:给你n个模式串,每个模式串有一个得分,让你构造出一个长度为N之内且分数最高的文本串;输出字典序列最小的. 解题思路:  AC自动机 + DP , 不过要输出字典序列最小,多开一个 一个三维字符串来辅助二维DP(新思路) , DP[i][j] ,表示到i位置状态为j的最大得分. 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月11日 星期四 15时18分4秒 4 5 #inclu