版本比较
基础字符串算法杂讲
正文
增加 22 行删除 22 行
60 行未改动6162636465656667676869707111 行未改动83848586878788899091729 行未改动全部展开8218228238248258258268278288296 行未改动836837838839840840841842843844133 行未改动全部展开978979980981982982983984985986392 行未改动全部展开1379138013811382138313831384138513861387138813891390139013911392139313941395139613971398139914001400140114021403140410 行未改动1415141614171418141914191420142114221423315 行未改动全部展开1739174017411742174317431744174517461747174817491750175017511752175317541755175617571758175917601760176117621763176433 行未改动1798179918001801180218021803180418051805180618071808180932 行未改动184218431844184518461846184718481848184918501851185118521853185418551856185718581858185918601861186246 行未改动1909191019111912191319131914191519161917191819191920192019211922192319241925192619271928192919301930193119321933193451 行未改动60 行未改动 struct node { int son[128]; int cnt, tag; }arr[3000030];
int siz = 1;
void insert(const string &s) { int u = 1; for(int i = 0; i < s.size(); i++) { if(!arr[u].son[s[i]]) {11 行未改动 } arr[u].tag = tg; arr[u].cnt++; }
int query(const string &s) { int u = 1; for(int i = 0; i < s.size(); i++) { if(!arr[u].son[s[i]]) {729 行未改动全部展开
struct node { vector<int> num; int len;
node& operator =(int b) { num.clear(); num.push_back(b % kB); if(b >= kB) {6 行未改动 num = b.num; len = b.len; return *this; }
node operator +(node b) { node c; c.num.clear(); c.len = max(len, b.len) + 1;133 行未改动全部展开
# AC 自动机
::::info[[P3808 AC 自动机 I](https://www.luogu.com.cn/problem/P3808)]{open}给定 $n$ 个模式串 $s_i$ 和一个文本串 $t$,求有多少个不同的模式串在文本串里出现过。 给定 $n$ 个模式串 $s_i$ 和一个文本串 $t$,求有多少个不同的模式串在文本串里出现过。两个模式串不同当且仅当他们 **编号** 不同。::::
Trie 能做多模式前缀匹配,但是这玩意是子串匹配;392 行未改动全部展开struct AC { // ch is child in trie, fail is failure pointer, trans is transition edge, occ is occurence, cnt is node count, ord is the BFS order. int ch[kSL + 10][kSigma], fail[kSL + 10], trans[kSL + 10][kSigma], occ[kSL], cnt, fs[kSL]; vector<int> ord;
// RAII(I curse all those people who don't use RAII with a 100% chance of not initializing something) AC() : ch(), fail(), trans(), occ(), cnt(0) { memset(ch, -1, sizeof(ch)); memset(fail, -1, sizeof(fail)); memset(trans, -1, sizeof(trans)); }
// insert an string. DISCLAIMER: if the last called function is insert(), don't call match() or the program may have unexpected behavior void insert(string s) { int u = 0; for(auto i : s) { if(ch[u][ttt(i)] == -1) ch[u][ttt(i)] = ++cnt; u = ch[u][ttt(i)]; } occ[u]++; }
void build() { ord = {0}; fail[0] = 0; queue<int> q;10 行未改动 } } for(auto u : ord) fs[u] = fs[fail[u]] + occ[u]; }
int match(string s) { int u = 0, ans = 0; for(auto i : s) { u = trans[u][ttt(i)];315 行未改动全部展开struct AC { // ch is child in trie, fail is failure pointer, trans is transition edge, occ is occurence, cnt is node count, ord is the BFS order. int ch[kSL + 10][kSigma], fail[kSL + 10], trans[kSL + 10][kSigma], occ[kSL], cnt, fs[kSL]; vector<int> ord;
// RAII(I curse all those people who don't use RAII with a 100% chance of not initializing something) AC() : ch(), fail(), trans(), occ(), cnt(0) { memset(ch, -1, sizeof(ch)); memset(fail, -1, sizeof(fail)); memset(trans, -1, sizeof(trans)); }
// insert an string. DISCLAIMER: if the last called function is insert(), don't call match() or the program may have unexpected behavior void insert(string s) { int u = 0; for(auto i : s) { if(ch[u][ttt(i)] == -1) ch[u][ttt(i)] = ++cnt; u = ch[u][ttt(i)]; } occ[u] = 1; }
void build() { ord = {0}; fail[0] = 0; queue<int> q;33 行未改动 if(!tr.occ[zrr]) { if(!k) dp[i + 1][j][0] = (dp[i + 1][j][0] + dp[i][j][0]) % kMod; if(k && k == ttt(n[i]) && !i) dp[i + 1][zrr][1] = (dp[i + 1][zrr][1] + dp[i][j][0]) % kMod; if(k && (k < ttt(n[i]) || i)) dp[i + 1][zrr][2] = (dp[i + 1][zrr][2] + dp[i][j][0]) % kMod;
if(k == ttt(n[i])) dp[i + 1][zrr][1] = (dp[i + 1][zrr][1] + dp[i][j][1]) % kMod; if(k < ttt(n[i])) dp[i + 1][zrr][2] = (dp[i + 1][zrr][2] + dp[i][j][1]) % kMod;
dp[i + 1][zrr][2] = (dp[i + 1][zrr][2] + dp[i][j][2]) % kMod; } } }32 行未改动using namespace std;using longlong = long long;
struct linear {
const ldouble eps = 1e-17;
int n, nt; ldouble arr[110][110];
int check(int x) { for(int i = 0; i <= n; i++) { if(arr[x][i] >= eps || arr[x][i] <= -eps) return 0; } return (arr[x][n + 1] >= eps || arr[x][n + 1] <= -eps? 1 : 2); }
int gauss() { for(int i = 0; i <= n; i++) { bool f = 1; if(arr[i][i] < eps && arr[i][i] > -eps) {46 行未改动struct AC { // ch is child in trie, fail is failure pointer, trans is transition edge, occ is occurence, cnt is node count, ord is the BFS order. int ch[kSL + 10][kSigma], fail[kSL + 10], trans[kSL + 10][kSigma], occ[kSL], cnt, fs[kSL]; vector<int> ord;
// RAII(I curse all those people who don't use RAII with a 100% chance of not initializing something) AC() : ch(), fail(), trans(), occ(), cnt(0) { memset(ch, -1, sizeof(ch)); memset(fail, -1, sizeof(fail)); memset(trans, -1, sizeof(trans)); }
// insert an string. DISCLAIMER: if the last called function is insert(), don't call match() or the program may have unexpected behavior void insert(string s, int x) { int u = 0; for(auto i : s) { if(ch[u][ttt(i)] == -1) ch[u][ttt(i)] = ++cnt; u = ch[u][ttt(i)]; } occ[u] = x; }
void build() { ord = {0}; fail[0] = 0; queue<int> q;51 行未改动