Bitset字符匹配
Regular Number
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define endl '\n'using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48);
}const int N = 5e6 + 10;char str[N];
int n;vector<int> a[1010];bitset<1010> b[10], ans;void shift_and() {for(int i = 0; i < 10; i++) b[i].reset();ans.reset();for(int i = 0; i < n; i++) {for(int j = 0; j < a[i].size(); j++) {b[a[i][j]].set(i);}}int m = strlen(str);for(int i = 0; i < m; i++) {int now = str[i] - '0';ans <<= 1;ans.set(0);ans = ans & b[now];if(ans[n - 1]) {char c = str[i + 1];str[i + 1] = '\0';puts(str + i + 1 - n);str[i + 1] = c;}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);while(scanf("%d", &n) != EOF) {for(int i = 0; i < n; i++) {int num = read();for(int j = 0; j < num; j++) {int x = read();a[i].pb(x);}}scanf("%s", str);// cout << str << endl;shift_and();for(int i = 0; i < n; i++) a[i].clear();}return 0;
}
带可选字符的多字符串匹配
输入有点坑,一定要用gets()才行。
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define endl '\n'using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48);
}int pos[150], tot;const int N = 2e6 + 10;char str[N], s[100];
int n, m, flag;bitset<510> b[70], ans;void shift_and() {n = strlen(str);for(int i = 0; i < n; i++) {ans <<= 1;ans.set(0);ans &= b[pos[str[i]]];if(ans[m - 1]) {flag = 1;printf("%d\n", i + 1 - m + 1);}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);for(int i = '0'; i <= '9'; i++) pos[i] = ++tot;for(int i = 'a'; i <= 'z'; i++) pos[i] = ++tot;for(int i = 'A'; i <= 'Z'; i++) pos[i] = ++tot;while(gets(str)) {scanf("%d", &m);for(int i = 1; i <= tot; i++) b[i].reset();ans.reset();for(int i = 0; i < m; i++) {int x; scanf("%d %s", &x, s);for(int j = 0; j < x; j++)b[pos[s[j]]].set(i);}flag = 0;shift_and();if(!flag) puts("NULL");getchar();}return 0;
}
F. Substrings in a String
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define endl '\n'using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48);
}const int N = 1e5 + 10;char str1[N], str2[N];bitset<N> b[26], ans;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> str1;int n = strlen(str1);for(int i = 0; i < n; i++) b[str1[i] - 'a'].set(i);int q; cin >> q;while(q--) {int op; cin >> op;if(op & 1) {int x; char c; cin >> x >> c;b[str1[x - 1] - 'a'].reset(x - 1);str1[x - 1] = c;b[str1[x - 1] - 'a'].set(x - 1);}else {int l, r;cin >> l >> r >> str2;int m = strlen(str2);if(r - l + 1 < m) {puts("0");continue;}int sum = 0;ans.set();for(int j = 0; j < m; j++) ans &= b[str2[j] - 'a'] >> j;print((int)(ans >> (l - 1)).count() - (int)(ans >> (r - m + 1)).count()), putchar('\n');}}return 0;
}