# 【bzoj4259/bzoj4503】残缺的字符串/两个串 FFT

bzoj4259

3 7
a*b
aebr*ob

2
1 5

bzoj4503

FFT

bzoj4259:

#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 1 << 20
#define pi acos(-1)
#define tra(ch) (ch == '*' ? 0 : ch - 'a' + 1)
using namespace std;
struct data
{
double x , y;
data() {x = y = 0;}
data(double x0 , double y0) {x = x0 , y = y0;}
data operator+(const data a)const {return data(x + a.x , y + a.y);}
data operator-(const data a)const {return data(x - a.x , y - a.y);}
data operator*(const data a)const {return data(x * a.x - y * a.y , x * a.y + y * a.x);}
}a1[N] , b1[N] , a2[N] , b2[N] , a3[N] , b3[N];
char sa[N] , sb[N];
int ans[N] , tot;
void fft(data *a , int n , int flag)
{
int i , j , k;
for(i = k = 0 ; i < n ; i ++ )
{
if(i > k) swap(a[i] , a[k]);
for(j = (n >> 1) ; (k ^= j) < j ; j >>= 1);
}
for(k = 2 ; k <= n ; k <<= 1)
{
data wn(cos(2 * pi * flag / k) , sin(2 * pi * flag / k));
for(i = 0 ; i < n ; i += k)
{
data t , w(1 , 0);
for(j = i ; j < i + (k >> 1) ; j ++ , w = w * wn)
t = w * a[j + (k >> 1)] , a[j + (k >> 1)] = a[j] - t , a[j] = a[j] + t;
}
}
}
void work(data *a , data *b , int len)
{
int i;
fft(a , len , 1) , fft(b , len , 1);
for(i = 0 ; i < len ; i ++ ) a[i] = a[i] * b[i];
fft(a , len , -1);
for(i = 0 ; i < len ; i ++ ) a[i].x = a[i].x / len;
}
int main()
{
int m , n , i , len;
double tmp;
scanf("%d%d%s%s" , &m , &n , sa , sb);
for(i = 0 ; i < m ; i ++ ) tmp = tra(sa[i]) , a1[m - i - 1].x = tmp , a2[m - i - 1].x = tmp * tmp , a3[m - i - 1].x = tmp * tmp * tmp;
for(i = 0 ; i < n ; i ++ ) tmp = tra(sb[i]) , b1[i].x = tmp * tmp * tmp , b2[i].x = tmp * tmp , b3[i].x = tmp;
for(len = 1 ; len < n + m ; len <<= 1);
work(a1 , b1 , len) , work(a2 , b2 , len) , work(a3 , b3 , len);
for(i = 0 ; i <= n - m ; i ++ )
if(!(int)(a1[i + m - 1].x - 2 * a2[i + m - 1].x + a3[i + m - 1].x + 0.1))
ans[++tot] = i;
printf("%d\n" , tot);
for(i = 1 ; i <= tot ; i ++ ) printf("%d " , ans[i] + 1);
return 0;
}


bzoj4503:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 1 << 20
#define tra(ch) (ch == '?' ? 0 : ch - 'a' + 1)
#define pi acos(-1)
using namespace std;
struct data
{
double x , y;
data() {x = y = 0;}
data(double x0 , double y0) {x = x0 , y = y0;}
data operator+(const data a)const {return data(x + a.x , y + a.y);}
data operator-(const data a)const {return data(x - a.x , y - a.y);}
data operator*(const data a)const {return data(x * a.x - y * a.y , x * a.y + y * a.x);}
}a1[N] , b1[N] , a2[N] , b2[N];
char sa[N] , sb[N];
int ans[N] , tot;
void fft(data *a , int n , int flag)
{
int i , j , k;
for(i = k = 0 ; i < n ; i ++ )
{
if(i > k) swap(a[i] , a[k]);
for(j = (n >> 1) ; (k ^= j) < j ; j >>= 1);
}
for(k = 2 ; k <= n ; k <<= 1)
{
data wn(cos(2 * pi * flag / k) , sin(2 * pi * flag / k));
for(i = 0 ; i < n ; i += k)
{
data t , w(1 , 0);
for(j = i ; j < i + (k >> 1) ; j ++ , w = w * wn)
t = w * a[j + (k >> 1)] , a[j + (k >> 1)] = a[j] - t , a[j] = a[j] + t;
}
}
}
void work(data *a , data *b , int len)
{
int i;
fft(a , len , 1) , fft(b , len , 1);
for(i = 0 ; i < len ; i ++ ) a[i] = a[i] * b[i];
fft(a , len , -1);
for(i = 0 ; i < len ; i ++ ) a[i].x /= len;
}
int main()
{
int la , lb , i , len;
double tmp , a3 = 0;
scanf("%s%s" , sa , sb) , la = strlen(sa) , lb = strlen(sb);
for(i = 0 ; i < la ; i ++ ) tmp = tra(sa[i]) , a1[i].x = tmp * tmp , a2[i].x = tmp;
for(i = 0 ; i < lb ; i ++ ) tmp = tra(sb[i]) , b1[lb - i - 1].x = tmp , b2[lb - i - 1].x = tmp * tmp , a3 += tmp * tmp * tmp;
for(len = 1 ; len < 2 * la || len < 2 * lb ; len <<= 1);
work(a1 , b1 , len) , work(a2 , b2 , len);
for(i = 0 ; i < la - lb + 1 ; i ++ )
if(!(int)(a1[i + lb - 1].x - 2 * a2[i + lb - 1].x + a3 + 0.1))
ans[++tot] = i;
printf("%d\n" , tot);
for(i = 1 ; i <= tot ; i ++ ) printf("%d\n" , ans[i]);
return 0;
}


posted @ 2017-05-19 15:51  GXZlegend  阅读(265)  评论(0编辑  收藏  举报