【Live Archive】4671 - K-neighbor substrings【FFT+后缀数组】

传送门:【Live Archive】4671 - K-neighbor substrings

题目分析:

和Ural 1996那题很像,都是通过将一个串反转后就可以将匹配变成卷积了。
由于从 A 串中取出来的串是本质不同的,所以这里我用了后缀数组(用后缀数组纯粹是因为我懒= =)做了处理:处理出的height数组, heighti 表示字典序第 i 小的后缀和字典序第i1小的后缀的最长前缀长度。然我我们可以轻而易举的发现,连续的 heightim 我们只需要取其中一个就行了,因为这几个都是重复的(这里 m 是模式串的长度)。对于小于m的我们每个都要取。还有一点要注意的是取出的串长度一定要大于等于 m

my  code:

#include <stdio.h>
#include <string.h>
#include <set>
#include <map>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define clrs( a , x , size ) memset ( a , x , sizeof ( a[0] ) * ( size ) )
#define cpys( a , x , size ) memcpy ( a , x , sizeof ( a[0] ) * ( size ) )

const int MAXN = 262144 ;
const double pi = acos ( -1.0 ) ;

struct Complex {
    double r , i ;
    Complex () {}
    Complex ( double r , double i ) : r ( r ) , i ( i ) {}
    Complex operator + ( const Complex& p ) const {
        return Complex ( r + p.r , i + p.i ) ;
    }
    Complex operator - ( const Complex& p ) const {
        return Complex ( r - p.r , i - p.i ) ;
    }
    Complex operator * ( const Complex& p ) const {
        return Complex ( r * p.r - i * p.i , r * p.i + i * p.r ) ;
    }
} ;

Complex x1[MAXN] , x2[MAXN] ;
char s[MAXN] , p[MAXN] ;
int t1[MAXN] , t2[MAXN] , xy[MAXN] , c[MAXN] , sa[MAXN] , rank[MAXN] , height[MAXN] ;
int cost[MAXN] ;
int k ;

int cmp ( int r[] , int a , int b , int d ) {
    return r[a] == r[b] && r[a + d] == r[b + d] ;
}

void getHeight ( int n , int k = 0 ) {
    for ( int i = 0 ; i <= n ; ++ i ) rank[sa[i]] = i ;
    for ( int i = 0 ; i < n ; ++ i ) {
        if ( k ) -- k ;
        int j = sa[rank[i] - 1] ;
        while ( s[i + k] == s[j + k] ) ++ k ;
        height[rank[i]] = k ;
    }
}

void build ( int n , int m = 128 ) {
    int *x = t1 , *y = t2 ;
    for ( int i = 0 ; i < m ; ++ i ) c[i] = 0 ;
    for ( int i = 0 ; i < n ; ++ i ) c[x[i] = s[i]] ++ ;
    for ( int i = 1 ; i < m ; ++ i ) c[i] += c[i - 1] ;
    for ( int i = n - 1 ; i >= 0 ; -- i ) {
        sa[-- c[x[i]]] = i ;
    }
    for ( int d = 1 , p = 0 ; p < n ; d <<= 1 , m = p ) {
        p = 0 ;
        for ( int i = n - d ; i < n ; ++ i ) y[p ++] = i ;
        for ( int i = 0 ; i < n ; ++ i ) if ( sa[i] >= d ) y[p ++] = sa[i] - d ;
        for ( int i = 0 ; i < m ; ++ i ) c[i] = 0 ;
        for ( int i = 0 ; i < n ; ++ i ) c[xy[i] = x[y[i]]] ++ ;
        for ( int i = 1 ; i < m ; ++ i ) c[i] += c[i - 1] ;
        for ( int i = n - 1 ; i >= 0 ; -- i ) sa[-- c[xy[i]]] = y[i] ;
        swap ( x , y ) ;
        p = 0 ;
        x[sa[0]] = p ++ ;
        for ( int i = 1 ; i < n ; ++ i ) {
            x[sa[i]] = cmp ( y , sa[i - 1] , sa[i] , d ) ? p - 1 : p ++ ;
        }
    }
    getHeight ( n - 1 ) ;
}

void FFT ( Complex y[] , int n , int rev ) {
    for ( int i = 1 , j , k , t ; i < n ; ++ i ) {
        for ( j = 0 , t = i , k = n >> 1 ; k ; k >>= 1 , t >>= 1 ) {
            j = j << 1 | t & 1 ;
        }
        if ( i < j ) swap ( y[i] , y[j] ) ;
    }
    for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ) {
        Complex wn ( cos ( rev * 2 * pi / s ) , sin ( rev * 2 * pi / s ) ) ;
        for ( int k = 0 ; k < n ; k += s ) {
            Complex w ( 1 , 0 ) , t ;
            for ( int i = k ; i < k + ds ; ++ i ) {
                y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;
                y[i] = y[i] + t ;
                w = w * wn ;
            }
        }
    }
    if ( rev < 0 ) {
        for ( int i = 0 ; i < n ; ++ i ) {
            y[i].r /= n ;
        }
    }
}

void calc ( int n1 , int n2 , int n , char x , char y ) {
    for ( int i = 0 ; i < n ; ++ i ) {
        x1[i] = Complex ( i < n1 ? s[i] == x : 0 , 0 ) ;
        x2[i] = Complex ( i < n2 ? p[n2 - i - 1] == y : 0 , 0 ) ;
    }
    FFT ( x1 , n , 1 ) ;
    FFT ( x2 , n , 1 ) ;
    for ( int i = 0 ; i < n ; ++ i ) {
        x1[i] = x1[i] * x2[i] ;
    }
    FFT ( x1 , n , -1 ) ;
    for ( int i = 0 ; i < n ; ++ i ) {
        cost[i] += ( int ) ( x1[i].r + 0.5 ) ;
    }
}

void solve () {
    int ans = 0 ;
    clr ( cost , 0 ) ;
    scanf ( "%s%s" , s , p ) ;
    int n1 = strlen ( s ) ;
    int n2 = strlen ( p ) ;
    int n = 1 ;
    while ( n < n1 + n2 - 1 ) n <<= 1 ;
    calc ( n1 , n2 , n , 'a' , 'b' ) ;
    calc ( n1 , n2 , n , 'b' , 'a' ) ;
    build ( n1 + 1 ) ;
    for ( int i = 0 ; i <= n1 ; ++ i ) {
        while ( i < n1 && height[i + 1] >= n2 ) ++ i ;
        if ( sa[i] + n2 - 1 < n1 && cost[sa[i] + n2 - 1] <= k ) ++ ans ;
    }
    printf ( "%d\n" , ans ) ;
}

int main () {
    int cas = 0 ;
    while ( ~scanf ( "%d" , &k ) && ~k ) {
        printf ( "Case %d: " , ++ cas ) ;
        solve () ;
    }
    return 0 ;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值