Repository navigation
Expand file tree
/
Copy pathKNeighboringSubString.cpp
More file actions
153 lines (146 loc) · 3.54 KB
/
Copy pathKNeighboringSubString.cpp
File metadata and controls
153 lines (146 loc) · 3.54 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <iostream>
#include <set>
using namespace std;
const double PI = acos(-1.0);
//?????
struct complex
{
double r,i;
complex(double _r = 0.0,double _i = 0.0)
{
r = _r; i = _i;
}
complex operator +(const complex &b)
{
return complex(r+b.r,i+b.i);
}
complex operator -(const complex &b)
{
return complex(r-b.r,i-b.i);
}
complex operator *(const complex &b)
{
return complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
/*
* ??FFT?IFFT???????
* ??i? (i????????)??
* len???2??
*/
void change(complex y[],int len)
{
int i,j,k;
for(i = 1, j = len/2;i < len-1; i++)
{
if(i < j) swap(y[i],y[j]);
//???????????,i<j??????
//i????+1,j??????+1,????i?j????
k = len/2;
while( j >= k)
{
j -= k;
k /= 2;
}
if(j < k) j += k;
}
}
/*
* ?FFT
* len???2^k??,
* on==1??DFT,on==-1??IDFT
*/
void fft(complex y[],int len,int on)
{
change(y,len);
for(int h = 2; h <= len; h <<= 1)
{
complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j = 0;j < len;j+=h)
{
complex w(1,0);
for(int k = j;k < j+h/2;k++)
{
complex u = y[k];
complex t = w*y[k+h/2];
y[k] = u+t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if(on == -1)
for(int i = 0;i < len;i++)
y[i].r /= len;
}
/////////////////////
typedef unsigned long long LL;
const int maxn = 100007;
const int MAGIC = 3;
const int M = 1e9+7;
complex x1[maxn*4],x2[maxn*4];
string s1,s2;
int sum1[maxn*4],sum2[maxn*4];
int k;
int len,len1,len2;
LL Hash[maxn],Pow[maxn];
set<LL> s;
void solve(int res[],char c){
len1 = s1.length(),len2 = s2.length();
len = 1;
while(len < 2*len1 || len<2*len2) len <<= 1;
for(int i=0;i<len1;i++){
if(s1[i] == c) x1[i] = complex(1,0);
else x1[i] = complex(0,0);
}
for(int i=len1;i<len;i++){
x1[i] = complex(0,0);
}
for(int i=0;i<len2;i++){
if(s2[len2 - i -1] == c) x2[i] = complex(1,0);
else x2[i] = complex(0,0);
}
for(int i=len2;i<len;i++){
x2[i] = complex(0,0);
}
//
fft(x1,len,1);
fft(x2,len,1);
for(int i=0;i<len;i++) x1[i] = x1[i]*x2[i];
fft(x1,len,-1);
//cout << "len:" << len << endl;
for(int i=0;i<len;i++){
res[i] = (int) (x1[i].r + 0.5);
//cout<< res[i] << " ";
}
//cout << endl;
}
int main(){
int nCase = 1;
Pow[0] = 1;
for(int i=1;i<maxn;i++) Pow[i] = Pow[i-1]*MAGIC;
while(cin >> k){
if(k == -1) break;
cin >> s1 >> s2;
solve(sum1,'a');
solve(sum2,'b');
Hash[0] = 0;
s.clear();
for(int i=0;i<=len1;i++) Hash[i+1] = Hash[i]*MAGIC + s1[i];
for(int i=len2-1;i<len1;i++){
//cout << i <<" " << len2 + i<< endl;
LL now = Hash[i+1] - Hash[i - len2 + 1]*Pow[len2];
if(len2 - sum1[i] - sum2[i] <= k){
s.insert(now);
}
}
printf("Case %d: %d\n",nCase++,s.size());
}
return 0;
}