题意:给出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 */

  

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  2. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

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

    题意 给定 \(n\) 个单词,\(q\) 个询问,每个询问包含两个串 \(s_1,s_2\),询问有多少个单词以 \(s_1\) 为前缀, \(s_2\) 为后缀,前后缀不能重叠. \(1 \leq ...

  4. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  7. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  8. hdu 6096---String(AC自动机)

    题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...

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

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

随机推荐

  1. Go - 常量与运算符

    常量的定义 1. 常量的值在编译的时候就已经确定.所以,在定义的时候就必须赋值 2. 使用 const 关键字来声明常量.赋值形式与变量类似: // 标准定义 const PI int = 3.14 ...

  2. 关闭easyui Tabs,有意思的JS异步处理

    因业务需要,需要将关闭windows窗口内的所有Tabs关闭掉,因此写了个方法,执行结果把我惊了一下. function closeAllTabs() { var tabsCount = $(&quo ...

  3. 我为什么一直不愿意用bootstrap

    做前端有2年多的时间了,知道bootstrap已经很久了. 第一次了解bootstrap是1年前,公司的一次培训中. 当时感到非常的愤怒,因为对框架的了解不够深入产生了这样的一个想法: 怎么会有这种框 ...

  4. 使用GET方式提交的表单遇到的问题

    经常使用表单,一直使用的都是POST方式,POST将数据封装到请求体中,相对于GET安全一点:而POST处理中文编码问题也比GET简单(GET需要将URL编码,后台接受到后还需要解码).今天我想要使用 ...

  5. 数据结构和算法之:二分法demo

    package com.js.ai.modules.pointwall.testxfz; class OrdArray{ private long[] a; private int nElems; p ...

  6. sublime text安装插件

    http://www.sublimetext.com/ 安装Sublime Text 2插件的方法: 1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Package ...

  7. django之分页器

    view from django.shortcuts import render,HttpResponse # Create your views here. from app01.models im ...

  8. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  9. 第6章Zabbix分布式监控

    Zabbix是一个分布式的监控系统.分布式监控适合跨机房.跨地域的网络监控.从多个Proxy收集数据,而每个Proxy可以采集多个设备的数据,从而轻松地构建分布式监控系统. ZabbixProxy可以 ...

  10. Python实践练习:strip()的正则表达式版本

    题目: 写一个函数,它接受一个字符串,做的事情和 strip()字符串方法一样.如果只传入了要去除的字符串,没有其他参数,那么就从该字符串首尾去除空白字符.否则,函数第二个参数指定的字符将从该字符串中 ...