HDU_4125 Moles 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4125

题意:

给你N个数字,先按照给数的顺序建一棵二叉查找树,然后按中序遍历树的结点,并记录访问结点的奇偶顺序,这样就会得到一个序列,再给你一个0,1序列,现在要你求给出的序列在遍历出来的序列中出现的次数,序列允许相互覆盖。

思路:

暴力建树如果二叉树退化成一个树链的话,时间复杂度就会变成O(N^2),可能会超时。这样我们就要求找一种logn的建树策略,很容易就会想到线段树,对于每个元素x,我们找到在它前面的比x小的最大值y和比x大的最小值z,可以证明的是x要么插在y的右边,要么插在z的左边。 做一我们可以用线段树在O( n*logn )的时间内将树建好,然后就是dfs求遍历序列, 最后kmp就ok 了。

代码:

#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int inf = (1 << 30) ;
const int NN = 600010 ;
int N ;
int lft[NN<<2]  ,rgh[NN<<2] ;

void build(int l ,int r, int idx){
    lft[idx] = rgh[idx] = -1 ;
    if(l == r)  return ;
    int mid = (l + r) >> 1 , ls = idx<<1 , rs = idx<<1|1 ;
    build(l , mid ,ls) ;
    build(mid+1 , r , rs) ;
}

int left[NN] , right[NN] ;
char seq[NN * 3] , str[7010];
int cnt , len1 , len2 ;
int next[7010] ;

struct Node{
    int now , f;
    Node(){}
    Node( int _now , int _f)
    :now( _now ) , f( _f ){}
} ;
stack< Node > ss ;

void dfs(int now){
aaa :
    seq[cnt++] = ( now&1 ) + '0'  ;
    int l = left[now] ;
    if(l != -1 ){
        ss.push( Node(now , 0) ) ;
        now = l ;
        goto aaa ;
bbb1 :
        seq[ cnt++ ] = ( now&1 ) + '0' ;
    }
    int r = right[now] ;
    if( r!=-1 ){
        ss.push( Node(now , 1) )  ;
        now = r ;
        goto aaa ;
bbb2 :
        seq[ cnt++ ] = ( now&1 ) + '0' ;
    }
    if( ss.empty() )    return ;
    now = ss.top().now  ;
    int ff = ss.top().f ; ss.pop() ;
    if( ff )    goto bbb2 ;
    else        goto bbb1 ;
}

void get_next(){
    len1 = strlen( str ) ;
    int i , j ;
    next[0] = -1 ;
    i = 1 ; j = -1 ;
    for( ; i<len1 ;i ++ ){
        while( j!=-1 && str[i]!=str[j+1] )  j = next[j]  ;
        if( str[i] == str[j+1] )    j++ ;
        next[i] = j ;
    }
}

int kmp(){
    get_next() ;
    len2 = strlen( seq )  ;
    int i, j ; i = 0 ; j = -1 ;
    int cc = 0 ;
    for( ;i<len2 ;i++ ){
        while( j!=-1 && str[j+1]!=seq[i] )  j = next[j]  ;
        if( seq[i] == str[j+1]) j++ ;
        if( j==len1-1 ){
            cc ++ ;
            j = next[j] ;
        }
    }
    return cc ;
}

inline int MIN(int a, int b){ return a < b ? a : b ;}
inline int MAX(int a, int b){ return a > b ? a : b ;}

void up(int l , int r, int idx){
    int ls = idx<<1 ,rs = idx<<1|1 ;
    if( lft[ls] == -1 ) lft[idx] = lft[rs] ;
    else    lft[idx] = lft[ls] ;
    if( rgh[rs] == -1 ) rgh[idx] = rgh[ls] ;
    else        rgh[idx] = rgh[rs] ;
}
void update(int l , int r, int idx, int pos ){
    if(l == r){
        lft[idx] = rgh[idx] = pos  ;return ;
    }
    int mid = (l + r) >> 1 , ls = idx<<1 ,rs = idx<<1|1 ;
    if( pos<=mid )  update(l , mid , ls , pos) ;
    else    update(mid+1 , r , rs , pos) ;
    up(l , r , idx);
}

int ask_small(int l , int r, int idx , int a, int b){
    if( rgh[idx] == -1 )    return -1;
    if(l==a && r==b)    return rgh[idx] ;
    int mid = (l + r) >> 1 , ls = idx<<1 , rs = idx<<1|1 ;
    if( b<=mid )    return ask_small(l , mid , ls , a , b) ;
    else if( mid<a )    return ask_small( mid+1 ,r ,rs , a ,b) ;
    else{
        int aa = ask_small( mid+1 , r  , rs , mid+1 , b) ;
        if( aa != -1 )  return aa ;
        aa = ask_small( l , mid , ls , a , mid ) ;
        return aa ;
    }
}

int ask_big(int l , int r, int idx, int a, int b){
    if( lft[idx] == -1 )    return -1 ;
    if(l==a && r==b)    return lft[idx] ;
    int mid = (l + r) >> 1 , ls = idx<<1 , rs = idx<<1|1 ;
    if( b<=mid )    return ask_big(l , mid , ls , a , b) ;
    else if( mid<a )    return ask_big( mid+1 ,r ,rs , a ,b) ;
    else{
        int aa = ask_big( l , mid  , ls , a , mid ) ;
        if( aa != -1 )  return aa ;
        aa = ask_big( mid+1  , r , rs , mid+1 , b ) ;
        return aa ;
    }
}

int main(){
    int T , a , b; scanf("%d",&T)  ;
    int s_pos , m_pos , cas = 0 ;
    while( T-- ){
        scanf("%d",&N) ;
        build(1 , N ,1);
        for(int i=1;i<=N;i++)   left[i] = right[i] = -1 ;
        for(int i=1;i<=N;i++){
            scanf("%d",&a);
            if( i == 1 )    b = a ;
            if( a == 1 )   s_pos = -1 ;
            else    s_pos = ask_small( 1 , N , 1 , 1 , a-1 ) ;
            if( a == N )   m_pos = -1 ;
            else    m_pos = ask_big( 1 , N , 1 , a+1 , N ) ;
            if( s_pos == -1 && m_pos == -1 ){
                ;
            }
            else if( s_pos==-1 && m_pos!=-1 ){
                left[ m_pos ] = a ;
            }
            else if( s_pos!=-1 && m_pos==-1 ){
                right[ s_pos ] = a ;
            }
            else{
                if( right[ s_pos ] == -1 ) right[ s_pos ] = a ;
                else        left[ m_pos ] = a ;
            }
            update(1 , N , 1 , a ) ;
        }
        cnt = 0 ;
        while( !ss.empty() )    ss.pop() ;
        dfs( b ) ;
        seq[cnt] = 0 ;
        scanf("%s",str);
        printf("Case #%d: %d\n",++cas,kmp() );
    }
    return 0 ;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值