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
| int n , m , k; int A[MAXN] , B[MAXN] , b[MAXN] , F[MAXN]; char s[MAXN] , t[MAXN]; int bac[129];
int wn[2][MAXN]; int Pow( int x , int y ) { int res = 1; while (y) { if (y & 1) res = res * (ll) x % P; x = x * (ll) x % P, y >>= 1; } return res; } void getwn(int l) { for (int i = 1; i < (1 << l); i <<= 1) { int w0 = Pow(3, (P - 1) / (i << 1)), w1 = Pow(3, P - 1 - (P - 1) / (i << 1)); wn[0][i] = wn[1][i] = 1; for (int j = 1; j < i; ++j) wn[0][i + j] = wn[0][i + j - 1] * (ll) w0 % P, wn[1][i + j] = wn[1][i + j - 1] * (ll) w1 % P; } } int rev[MAXN]; void getr(int l) { for(int i=1;i<(1<<l);++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<l-1); } void NTT(int *A,int len,int f) { for (int i = 0; i < len; ++i) if (rev[i] < i) swap(A[i], A[rev[i]]); for (int l = 1; l < len; l <<= 1) for (int i = 0; i < len; i += (l << 1)) for (int k = 0; k < l; ++k) { int t1 = A[i + k], t2 = A[i + l + k] * (ll) wn[f][l + k] % P; A[i + k] = (t1 + t2) % P; A[i + l + k] = (t1 - t2 + P) % P; } if (f == 1) for (int inv = Pow(len, P - 2), i = 0; i < len; ++i) A[i] = A[i] * (ll) inv % P; } int len , l; void Just_DOIT( ) { for( int i = 0 ; i <= m ; ++ i ) b[i] = B[m - i]; NTT( A , len , 0 ) , NTT( b , len , 0 ); rep( i , 0 , len ) A[i] = 1ll * A[i] * b[i] % P; NTT( A , len , 1 ); rep( i , m , m + n - 1 ) F[i - m] += A[i]; }
void solve() {
cin >> k; for( int i = 0 ; i < 9 ; ++ i ) bac["syfaknoi"[i]] = i; scanf("%s",s) , scanf("%s",t); n = strlen( s ) , m = strlen( t ); len = 1 , l = 0; while( len <= n + m ) len <<= 1 , ++ l; getr( l ) , getwn( l ); rep( i , 0 , n - 1 ) s[i] = bac[s[i]]; rep( i , 0 , m - 1 ) t[i] = bac[t[i]]; rep( i , 0 , 9 ) { rep( j , 0 , len ) A[j] = b[j] = B[j] = 0; rep( j , 0 , n - 1 ) A[j] = ( s[j] == i ); rep( j , 0 , m - 1 ) B[j] = ( t[j] == i ); Just_DOIT(); } int re = 0; rep( i , 0 , n - m ) if( m - F[i] <= k ) ++ re; cout << re << endl; }
signed main() {
solve(); }
|