# Sol

$$f[i]=\sum_{j+k=i}[S[k]==T[j]]$$

$$f[i]=\sum_{j+k=i}(S[k]-T[j])^2S[k]T[j]$$

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;

template <class Int>
IL void Input(RG Int &x){
RG int z = 1; RG char c = getchar(); x = 0;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= z;
}

const int maxn(3e5 + 5);
const int oo(1e9);
const double pi(acos(-1));

int n, m, k, r[maxn << 2], len, cnt, v1[maxn], v2[maxn], ans[maxn];
char s[maxn], t[maxn];

struct Complex{
double real, image;

IL Complex(){
real = image = 0;
}

IL Complex(RG double a, RG double b){
real = a, image = b;
}

IL Complex operator +(RG Complex b){
return Complex(real + b.real, image + b.image);
}

IL Complex operator -(RG Complex b){
return Complex(real - b.real, image - b.image);
}

IL Complex operator *(RG Complex b){
return Complex(real * b.real - image * b.image, real * b.image + image * b.real);
}

IL Complex operator *(RG int b){
return Complex(real * b, image * b);
}
} a[maxn << 2], b[maxn << 2], w[maxn << 2], c[maxn << 2];

IL void FFT(RG Complex *p, RG int opt){
for(RG int i = 0; i < len; ++i) if(r[i] < i) swap(p[i], p[r[i]]);
for(RG int i = 1; i < len; i <<= 1)
for(RG int j = 0, l = i << 1; j < len; j += l){
for(RG int k = 0; k < i; ++k){
RG Complex wn = Complex(w[len / i * k].real, w[len / i * k].image * opt);
RG Complex x = p[k + j], y = wn * p[k + j + i];
p[k + j] = x + y, p[k + j + i] = x - y;
}
}
}

IL void Prepare(){
RG int l = 0, tmp = m + n - 1;
for(len = 1; len < tmp; len <<= 1) ++l;
for(RG int i = 0; i < len; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
for(RG int i = 1; i <= len; i <<= 1)
for(RG int k = 0; k < i; ++k)
w[len / i * k] = Complex(cos(pi / i * k), sin(pi / i * k));
}

int main(RG int argc, RG char* argv[]){
Input(m), Input(n), scanf(" %s %s", t, s);
reverse(t, t + m), Prepare();
for(RG int i = 0; i < n; ++i) v1[i] = s[i] == '*' ? 0 : s[i] - 'a' + 1;
for(RG int i = 0; i < m; ++i) v2[i] = t[i] == '*' ? 0 : t[i] - 'a' + 1;

for(RG int i = 0; i < n; ++i) a[i].real = v1[i] * v1[i] * v1[i];
for(RG int i = 0; i < m; ++i) b[i].real = v2[i];
FFT(a, 1), FFT(b, 1);
for(RG int i = 0; i < len; ++i) c[i] = c[i] + a[i] * b[i], a[i] = b[i] = Complex(0, 0);

for(RG int i = 0; i < n; ++i) a[i].real = v1[i] * v1[i] << 1;
for(RG int i = 0; i < m; ++i) b[i].real = v2[i] * v2[i];
FFT(a, 1), FFT(b, 1);
for(RG int i = 0; i < len; ++i) c[i] = c[i] - a[i] * b[i], a[i] = b[i] = Complex(0, 0);

for(RG int i = 0; i < n; ++i) a[i].real = v1[i];
for(RG int i = 0; i < m; ++i) b[i].real = v2[i] * v2[i] * v2[i];
FFT(a, 1), FFT(b, 1);
for(RG int i = 0; i < len; ++i) c[i] = c[i] + a[i] * b[i];

FFT(c, -1);
for(RG int i = m - 1; i < n; ++i)
if(!(int(c[i].real / len + 0.5))) ans[++cnt] = i;
printf("%d\n", cnt);
for(RG int i = 1; i <= cnt; ++i) printf("%d ", ans[i] - m + 2);
return puts(""), 0;
}


posted @ 2018-04-13 15:39  Cyhlnj  阅读(116)  评论(0编辑  收藏  举报