前言
需要的知识:AC 自动机。
注意到哈希、扫描线、求 DFS 序什么的我全都不会,但是现有的无哈希 AC 自动机题解1都没有代码,所以我来补一个。
站在巨人的肩膀上
Register_int 大人已经用一篇简明扼要的题解建好了 AC 自动机解法的大厦,所以我只需在大厦上修修补补即可。
但显然,有大厦就会有两朵乌云。
第一朵乌云:正确性问题
根据互文的修辞手法可知,对于 或 的情况,直接跳过即可。
反向考虑,对于一对替换 ,如何构造出一对满足的 ?
首先,其自身必然可行。接着,同时在 前或后同时加同一个字符可构造的所有字符串对都可行。显然,这种方法也能构造出所有的 。
思考如何从 推出哪些 替换可行。
如果不存在,必然找不到对应的替换。
那么只需讨论存在的情况:我们真正需要的只有能构造出 的最小 。
那直接判断 可行吗?
但是,如果 有着相同的前后缀 ,可能会存在一个更小的 使其为一个合法的替换,此时需要 是 的子串(同时 也会是 的子串,具体读者可自己思考)。
因此,需要使用一些技巧将一对字符串替换的匹配转换为一个字符串的匹配再构造自动机。此事于 KMP 亦有记载,只需使用分隔符(即特殊字符即可)。
一篇简明扼要的题解中已阐明了方法,文中 即为最小替换 , 即为 。中间最小替换满足并且子串相等与替换成立是等价的。
并不是很严格的证明
对于一个替换 ,设转换后为 ,则其自身必然满足匹配条件。对于一个已经成立的 同时在前后加字符的操作相当于在 前或 后加入字符,此时必然也成立。
因此,对于一个合法的替换,必然有一次成功的匹配相对应。
因为有特殊字符 的存在,所以要想匹配成功,中间的 部分必须对齐,而前后的 必须分别为更大 的前后缀,即满足 是 的子串(同时 也会是 的子串,具体读者可自己思考)。
所以,对于一次成功的匹配,必然有一个合法的替换相对应。
综上所述,匹配成功与替换合法是等价的。
第二朵乌云:时间复杂度
这是因为模板题查询是全局的,每次查询复杂度为 ,总的查询复杂度为 。这在本题中是不可接受的。
注意到本题只用求每次询问的总替换数,因此可以提前预处理对答案的贡献来降低复杂度。
最终时间复杂度
代码
Accepted
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <utility>
#include <cassert>
#include <cstring>
#include <unordered_map>
#define shit_moutain
#undef shit_moutain
#define debug
#undef debug
class Aho_Corasick_Automaton
{
typedef unsigned index_type;
typedef unsigned cnt_type;
typedef int mark_type;
private:
struct tr_node
{
index_type tr[27];
index_type fail;
mark_type mark;
tr_node() : fail(0), mark(0)
{
for (index_type i = 0; i < 27; i++)
tr[i] = 0;
}
};
std::vector<tr_node> tr;
bool can_insert;
protected:
inline index_type push_new()
{
tr.emplace_back(tr_node());
return tr.size() - 1;
}
public:
Aho_Corasick_Automaton() : can_insert(1)
{
tr.emplace_back(tr_node());
}
void reserve(std::size_t n)
{
tr.reserve(n);
}
void insert(const std::string &s, const mark_type flag)
{
#ifdef debug
std::cout << s << std::endl;
#endif
assert(can_insert);
index_type u = 0;
for (const char &i : s)
{
const index_type c = i - 'a';
if (!tr[u].tr[c]) // 找不到 s[i]
tr[u].tr[c] = push_new(); // 新建一个
u = tr[u].tr[c]; // 继续往后走
}
tr[u].mark += flag; // 存一个标记 - 该节点有一个 flag 子串
}
// O(slen)
void get_fail_not_insert()
{
can_insert = 0;
std::queue<index_type> q;
tr[0].fail = 0; // 0 的 fail 显然是 0
for (index_type c = 'a' - 'a'; c <= '{' - 'a'; c++) // 对 0 的所有边
{
if (tr[0].tr[c])
{
tr[tr[0].tr[c]].fail = 0; // 失配就到 0
q.push(tr[0].tr[c]); // 入队。队。
}
}
for (index_type u; !q.empty();)
{
u = q.front();
q.pop();
tr[u].mark += tr[tr[u].fail].mark; // 去一个出来
for (index_type c = 'a' - 'a'; c <= '{' - 'a'; c++) // 对于整个字符集
{
if (tr[u].tr[c])
{
tr[tr[u].tr[c]].fail = tr[tr[u].fail].tr[c];
q.push(tr[u].tr[c]);
}
else
tr[u].tr[c] = tr[tr[u].fail].tr[c];
}
}
}
// O(maxlen|charset|)
mark_type query(const std::string &s)
{
mark_type wer = 0;
index_type p = 0;
const size_t slen = s.size();
std::queue<index_type> q;
for (size_t i = 0; i < slen; i++)
{
const index_type c = s[i] - 'a';
p = tr[p].tr[c];
wer += tr[p].mark;
}
return wer;
}
} ac;
using ll = long long;
const int maxn = 2e5, maxq = 2e5;
int n, q;
using std::cin;
using std::cout;
using std::endl;
std::string merge(const std::string &s1, const std::string &s2)
{
std::string s="";
int loc1 = 0, loc2 = (int)s1.size() - 1;
for (int j = 0; j < (int)s1.size(); j++)
{
if (s1[j] != s2[j])
{
loc1 = j;
break;
}
}
for (int j = (int)s1.size() - 1; j >= 0; j--)
{
if (s1[j] != s2[j])
{
loc2 = j;
break;
}
}
s += s1.substr(0,loc1);
s += '{';
s+=s1.substr(loc1,loc2-loc1+1);
s+=s2.substr(loc1,loc2-loc1+1);
s += '{';
s+=s2.substr(loc2+1);
return s;
}
int main()
{
std::vector<char> a;
ac = Aho_Corasick_Automaton();
ac.reserve(5e6 * 2);
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
std::string s1, s2;
cin >> s1 >> s2;
assert(s1.size() == s2.size());
if (s1 == s2)
continue;
ac.insert(merge(s1, s2), 1);
// s1!=s2 因此不用担心越界
}
ac.get_fail_not_insert();
for (int j = 1; j <= q; j++)
{
std::string t1, t2;
cin >> t1 >> t2;
if (t1.size() != t2.size())
{
cout << 0 << endl;
continue;
}
assert(t1 != t2);
cout << ac.query(merge(t1, t2)) << endl;
// t1!=t2 因此不用担心越界
}
return 0;
}参考文献
- OI Wiki,AC 自动机
- Register_int,题解:P14363 [CSP-S 2025] 谐音替换 / replace(暂无数据)
- AmaoFox,本文使用了互文的修辞手法。
- OI Wiki,前缀函数与 KMP 算法
- RainySoul,不是。拓扑和你这个都可以被卡掉。