版本比较
基础字符串算法杂讲
正文
增加 22 行删除 22 行
60 行未改动616263646566676869707111 行未改动838485868788899091729 行未改动全部展开8218228238248258268278288296 行未改动836837838839840841842843844133 行未改动全部展开978979980981982983984985986392 行未改动全部展开1379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140410 行未改动141514161417141814191420142114221423315 行未改动全部展开1739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176433 行未改动17981799180018011802180318041805180618071808180932 行未改动18421843184418451846184718481849185018511852185318541855185618571858185918601861186246 行未改动1909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193451 行未改动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$,求有多少个不同的模式串在文本串里出现过。两个模式串不同当且仅当他们 **编号** 不同。::::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 behaviorvoid 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 behaviorvoid 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 behaviorvoid 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 行未改动60 行未改动616263646566676869707111 行未改动838485868788899091729 行未改动全部展开8218228238248258268278288296 行未改动836837838839840841842843844133 行未改动全部展开978979980981982983984985986392 行未改动全部展开1379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140410 行未改动141514161417141814191420142114221423315 行未改动全部展开1739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176433 行未改动17981799180018011802180318041805180618071808180932 行未改动18421843184418451846184718481849185018511852185318541855185618571858185918601861186246 行未改动1909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193451 行未改动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$,求有多少个不同的模式串在文本串里出现过。两个模式串不同当且仅当他们 **编号** 不同。::::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 behaviorvoid 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 behaviorvoid 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 behaviorvoid 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 行未改动