ZOJ 3841 Cards

扑克牌排列算法
本文介绍了一个有趣的算法问题:计算剩余扑克牌以字典序较小的方式排列的可能性数量,并提供了两种不同的实现方案,一种为直接枚举法,另一种为深度优先搜索法。
Cards

Time Limit: 2 Seconds      Memory Limit: 65536 KB

EdwardGy has a poker (52 cards excluding jokers). One day, he saw some cards was lined up on the table. He wanted to know how many ways he can place all of the rest cards in a row with a lower lexicographic order than the line of the cards which were already on the table.

The lexicographic order of the cards is A < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K, and the colors should be ignored. If the cards already on the table is

AA22334455667788991010JJKKK

EdwardGy have only five ways:

AA22334455667788991010JJQQQQK
AA22334455667788991010JJQQQKQ
AA22334455667788991010JJQQKQQ
AA22334455667788991010JJQKQQQ
AA22334455667788991010JJKQQQQ

Input

There are multiple test cases. Each test case has only one line, a valid string indicating the cards on the table.

Output

For each test cases, output the remainder of the number of ways modulo by 109+7.

Sample Input
AA22334455667788991010JJKKK
Sample output
5




直接枚举每一个位置。。小于给出的牌。然后将剩下的牌进行排列组合。。

直接枚举版:

#include <bits/stdc++.h>
using namespace std ;
const int mod = 1e9+7;
int cnt[15] , num[60] , cnt1[15] ;
long long f[60] , inv[60] ;
string s ;

int Decode( char op ) {
    if( op == 'A' ) return 1 ;
    else if( op == '1' ) return 10 ;
    else if( op == 'J' ) return 11 ;
    else if( op == 'Q' ) return 12 ;
    else if( op == 'K' ) return 13 ;
    else return ( op-'0' );
}

void Delete0( string &s ) {
    string res = "" ;
    for( int i = 0 ; i < s.length() ; ++i ) if( s[i] != '0' ) {
        res += s[i] ;
    }
    s = res ;
}

long long mypow( long long  a , long long  b ) {
    long long res = 1 ;
    while( b ) {
        if( b&1 ) res = res * a % mod ;
        a = a * a % mod ;
        b >>= 1 ;
    }
    return res ;
}

void init() {
    f[0] = inv[0] = 1 ;
    for( int i = 1 ; i < 55 ; ++i ) {
        f[i] = f[i-1] * i % mod ;
        inv[i] = mypow( f[i] , mod - 2 ) ;
    }
}

int main() {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    ios::sync_with_stdio(0);
    init();
    while( cin >> s ) {
        memset( cnt , 0 , sizeof cnt ) ;
        Delete0(s);
        int n = s.length() ;
        for( int i = 0 ; i < n ; ++i ) {
            num[i] = Decode( s[i] );
            cnt[ num[i] ]++;
        }
        if( n == 52 ) { cout << '0' << endl ; continue ;}
        for( int i = 1 ; i <= 13 ; ++i ) cnt[i] = 4 - cnt[i] ;   // rest card
        long long ans = 0 ;
        int rest_card = 52 - n , c = rest_card , tag = 0 , pos = -1 ;
        for( int i = 0 ; i < n ; ++i ) {
            pos = i ;
            for( int j = 1 ; j < num[i] ; ++j ) if( cnt[j] > 0 ){
                tag = 1 ;
                long long tmp = f[ rest_card - i - 1 ] ;
                for( int k = 1 ; k <= 13 ; ++k ) if( cnt[k] > 0 ) {
                    if( k == j ) tmp = tmp * inv[ cnt[k] - 1 ] % mod ;
                    else tmp = tmp * inv[ cnt[k] ] % mod ;
                }
                ans = ( ans + tmp ) % mod ;
            }
            if( !cnt[num[i]] ) {
                break ;
            } else {
                cnt[ num[i] ]-- ; c--;
            }
        }
        if( !tag ) {
            if( c > 0 || pos == n - 1 ) cout << '0' << endl ;
            else cout << '1' << endl ;
        } else {
            cout << ans << endl ;
        }
    }
    return 0 ;
}
View Code

dfs:

#include <bits/stdc++.h>
using namespace std ;
const int mod = 1e9+7;
int cnt[15] , num[60] , cnt1[15] ;
long long f[60] , inv[60] ;
string s ;

int Decode( char op ) {
    if( op == 'A' ) return 1 ;
    else if( op == '1' ) return 10 ;
    else if( op == 'J' ) return 11 ;
    else if( op == 'Q' ) return 12 ;
    else if( op == 'K' ) return 13 ;
    else return ( op-'0' );
}

void Delete0( string &s ) {
    string res = "" ;
    for( int i = 0 ; i < s.length() ; ++i ) if( s[i] != '0' ) {
        res += s[i] ;
    }
    s = res ;
}

long long mypow( long long  a , long long  b ) {
    long long res = 1 ;
    while( b ) {
        if( b&1 ) res = res * a % mod ;
        a = a * a % mod ;
        b >>= 1 ;
    }
    return res ;
}

void init() {
    f[0] = inv[0] = 1 ;
    for( int i = 1 ; i < 55 ; ++i ) {
        f[i] = f[i-1] * i % mod ;
        inv[i] = mypow( f[i] , mod - 2 ) ;
    }
}
long long ans ;
int n , rest_card ;
void dfs( int dep , int rest ) {
    if( dep == n ) return ;
    if( rest == 0 ) {
        ans = ( ans + 1 ) % mod ;
        return ;
    }
    for( int i = 1 ; i < num[dep] ; ++i ) if( cnt[i] > 0 ) {
        long long tmp = f[ rest - 1 ] ;
        for( int k = 1 ; k <= 13 ; ++k ) if( cnt[k] > 0 ) {
            if( k == i ) tmp = tmp * inv[ cnt[k] - 1 ] % mod ;
            else tmp = tmp * inv[ cnt[k] ] % mod ;
        }
        ans = ( ans + tmp ) % mod ;
    }
    if( cnt[ num[dep] ] > 0 ) {
        cnt[ num[dep] ]-- ;
        dfs( dep + 1 , rest - 1 ) ;
    }
}

int main() {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    ios::sync_with_stdio(0);
    init();
    while( cin >> s ) {
        memset( cnt , 0 , sizeof cnt ) ;
        Delete0(s);
        n = s.length() ;
        for( int i = 0 ; i < n ; ++i ) {
            num[i] = Decode( s[i] );
            cnt[ num[i] ]++;
        }
        for( int i = 1 ; i <= 13 ; ++i ) cnt[i] = 4 - cnt[i] ;
        rest_card = 52 - n ;
        ans = 0 ; dfs( 0 , rest_card );
        cout << ans << endl ;
    }
    return 0 ;
}
View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4365540.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值