1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-8; typedef complex<double> Complex; const int maxn = 2e6 + 10;
Complex p[maxn], t[maxn]; Complex a[maxn], b[maxn], c[maxn], d[maxn]; Complex ans[maxn]; string str; int m, n; int bit = 2, rev[maxn];
void get_rev(){ memset(rev, 0, sizeof(rev)); while(bit <= n + m) bit <<= 1; for(int i = 0; i < bit; ++i) { rev[i] = (rev[i >> 1] >> 1) | (bit >> 1) * (i & 1); } }
void FFT(Complex *a, int op) { for(int i = 0; i < bit; ++i) { if(i < rev[i]) swap(a[i], a[rev[i]]); } for(int mid = 1; mid < bit; mid <<= 1) { Complex wn = Complex(cos(PI / mid), op * sin(PI / mid)); for(int j = 0; j < bit; j += mid<<1) { Complex w(1, 0); for(int k = 0; k < mid; ++k, w = w * wn) { Complex x = a[j + k], y = w * a[j + k + mid]; a[j + k] = x + y, a[j + k + mid] = x - y; } } } }
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> m >> n; cin >> str; for(int i = 0; i < m; ++i) { p[m - i - 1] = str[i] == '*' ? 0 : (str[i] - 'a' + 1); } cin >> str; for(int i = 0; i < n; ++i) { t[i] = str[i] == '*' ? 0 : (str[i] - 'a' + 1); } get_rev(); for(int i = 0; i < bit; ++i) { a[i] = p[i] * p[i] * p[i]; b[i] = t[i]; } FFT(a, 1); FFT(b, 1); for(int i = 0; i < bit; ++i) { ans[i] += a[i] * b[i]; }
for(int i = 0; i < bit; ++i) { a[i] = p[i]; b[i] = t[i] * t[i] * t[i]; } FFT(a, 1); FFT(b, 1); for(int i = 0; i < bit; ++i) { ans[i] += a[i] * b[i]; }
for(int i = 0; i < bit; ++i) { a[i] = p[i] * p[i]; b[i] = t[i] * t[i]; } FFT(a, 1); FFT(b, 1); for(int i = 0; i < bit; ++i) { ans[i] -= a[i] * b[i] * Complex(2, 0); }
FFT(ans, -1); queue<int> q; for(int i = m - 1; i < n; ++i) { if((int)(ans[i].real() / bit + 0.5) == 0) q.push(i - m + 2); } cout << q.size() << endl; while(q.size()) { cout << q.front() << " "; q.pop(); } cout << endl; return 0; }
|