Codeforences 17C Balance

数组,开小了,暴力调了2小时才发现。。。。。

【AC代码】

//
//Created by just_sort 2016/12/13
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define MP(x,y) make_pair(x,y)
#define PI acos(-1)
const int maxn = 1020;
const int maxm = 1<<12;
const int inf = 0x3f3f3f3f;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head
const int mod = 51123987;
int n, cnt, dp[200][55][55][55], nex[200][3];
char s1[200], s2[200];

void add(int &x, int y)
{
    (x += y) %= mod;
    if(x < 0) x += mod;
}
int main()
{
    while(scanf("%d", &n) != EOF){
        scanf("%s", s1);
        memset(dp, 0, sizeof(dp));
        memset(nex, -1, sizeof(nex));
        cnt = 0;
        s2[cnt++] = s1[0];
        for(int i = 1; i < n; i++){
            if(s1[i] != s1[i-1]){
                s2[cnt++] = s1[i];
            }
        }
        for(int i = 0; i < cnt; i++){
            for(int j = i; j < cnt; j++){
                if(s2[j] == 'a' && nex[i][0] == -1){
                    nex[i][0] = j;
                }
                if(s2[j] == 'b' && nex[i][1] == -1){
                    nex[i][1] = j;
                }
                if(s2[j] == 'c' && nex[i][2] == -1){
                    nex[i][2] = j;
                }
            }
        }
        //
        int m = n / 3 + 1;
        dp[0][0][0][0] = 1;
        for(int i  = 1; i <= cnt; i++){
            for(int ca = 0; ca <= m; ca++){
                for(int cb = 0; cb <= m; cb++){
                    for(int cc = 0; cc <= m; cc++){
                        if(nex[i-1][0] != -1){
                            //add(dp[nex[i-1][0]][ca+1][cb][cc], dp[i-1][ca][cb][cc]);
                            dp[nex[i-1][0]][ca+1][cb][cc] += dp[i-1][ca][cb][cc];
                            dp[nex[i-1][0]][ca+1][cb][cc] %= mod;
                        }
                        if(nex[i-1][1] != -1){
                            //add(dp[nex[i-1][1]][ca][cb+1][cc], dp[i-1][ca][cb][cc]);
                            dp[nex[i-1][1]][ca][cb+1][cc] += dp[i-1][ca][cb][cc];
                            dp[nex[i-1][1]][ca][cb+1][cc] %= mod;
                        }
                        if(nex[i-1][2] != -1){
                            //add(dp[nex[i-1][2]][ca][cb][cc+1], dp[i-1][ca][cb][cc]);
                            dp[nex[i-1][2]][ca][cb][cc+1] += dp[i-1][ca][cb][cc];
                            dp[nex[i-1][2]][ca][cb][cc+1] %= mod;
                        }
                    }
                }
            }
        }
        int ans = 0;
        if(n % 3 == 0){
            for(int i = 0; i < cnt; i++){
                //add(ans, dp[i][n/3][n/3][n/3]);
                ans += dp[i][n/3][n/3][n/3]; ans %= mod;
            }
        }
        else if(n % 3 == 1){
            for(int i = 0; i < cnt; i++){
//                add(ans, dp[i][n/3+1][n/3][n/3]);
//                add(ans, dp[i][n/3][n/3+1][n/3]);
//                add(ans, dp[i][n/3][n/3][n/3+1]);
                  ans += dp[i][n/3+1][n/3][n/3]; ans %= mod;
                  ans += dp[i][n/3][n/3+1][n/3]; ans %= mod;
                  ans += dp[i][n/3][n/3][n/3+1]; ans %= mod;
            }
        }
        else{
            for(int i = 0; i < cnt; i++){
//                add(ans, dp[i][n/3+1][n/3+1][n/3]);
//                add(ans, dp[i][n/3+1][n/3][n/3+1]);
//                add(ans, dp[i][n/3][n/3+1][n/3+1]);
                  ans += dp[i][n/3+1][n/3+1][n/3]; ans %= mod;
                  ans += dp[i][n/3+1][n/3][n/3+1]; ans %= mod;
                  ans += dp[i][n/3][n/3+1][n/3+1]; ans %= mod;
            }
        }
        ans = ans % mod;
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值