c_cpp https://arena.topcoder.com/#/u/practiceCode/13581/7587/8480/1/298265

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp https://arena.topcoder.com/#/u/practiceCode/13581/7587/8480/1/298265相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>

#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)

using namespace std;

const int MAX = 35;
const int OO = 1e9;

int n;
ll cache[MAX][26];

ll fs(int i, string s) {
    if(i == n) {
        //cout << s << endl;
        return 1;
    }
    int x;
    if(i >= 2) {
        x = ((s[0]+'\0')-48)*10;
        x += (s[1]+'\0')-48;
    } else {
        x = i+3;
    }
    //cout << x << endl;
    ll &ret = cache[i][x];
    if(ret != -1)
        return ret;

    ret = 0;
    string tmp = s;
    if(i >= 2) {

        if(!((s[0] == 'B' && s[1] == 'C' ) || (s[0] == 'C' && s[1] == 'B'))) {
            tmp[0] = tmp[1];
            tmp[1] = 'A';
            //cout << tmp << endl;
            ret += fs(i+1, tmp);
        }

        tmp = s;
        if(!((s[0] == 'A' && s[1] == 'C' ) || (s[0] == 'C' && s[1] == 'A'))) {
            tmp[0] = tmp[1];
            tmp[1] = 'B';
            ret += fs(i+1, tmp);
        }

        tmp = s;
        if(!((s[0] == 'B' && s[1] == 'A' ) || (s[0] == 'A' && s[1] == 'B'))) {
            tmp[0] = tmp[1];
            tmp[1] = 'C';
            ret += fs(i+1, tmp);
        }
    } else {
        tmp[i] = 'A';
        ret += fs(i+1, tmp);
        tmp =s;
        tmp[i] = 'B';
        ret += fs(i+1, tmp);
        tmp =s;
        tmp[i] = 'C';
        ret += fs(i+1, tmp);
    }

    return ret;
}
/*
int main() {
    clr(cache, -1);
    cin>>n;
    cout << fs(0, "  ") <<endl;
    return 0;
}*/


class ForbiddenStrings {
public:
    ll countNotForbidden(int n1) {        
        clr(cache, -1);
        n = n1;
        return fs(0, "  ");
    }
};

以上是关于c_cpp https://arena.topcoder.com/#/u/practiceCode/13581/7587/8480/1/298265的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *

c_cpp 54.螺旋矩阵