提出 #2844908


ソースコード 拡げる

Copy
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,variable-expansion-in-unroller")
// {{{ by shik
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REP1(i, a, b) for (int i = (a); i <= int(b); i++)
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

namespace { namespace shik {

template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%" SCNd64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }

template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%" PRId64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }

#ifdef SHIK
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); }
template<class T> inline T bit(T x, int i) { return (x >> i) & 1; }
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

// }}}

const int N=40;
int n;
char s[N];

using P=tuple<int,int,string,string>;
map<P,LL> dp;

LL go( int l, int r, string sl, string sr ) {
    if ( r-l+1<SZ(sl)+SZ(sr) ) return 0;
    if ( l>r ) return sl.empty() && sr.empty();
    auto k=make_tuple(l,r,sl,sr);
    if ( dp.count(k) ) return dp[k];
    if ( !sl.empty() && s[r]!=sl[0] ) return dp[k]=go(l,r-1,sl,sr+s[r]);
    if ( !sr.empty() && s[l]!=sr[0] ) return dp[k]=go(l+1,r,sl+s[l],sr);
    LL ans=0;
    if ( !sl.empty() ) {
        assert(s[r]==sl[0]);
        ans+=go(l,r-1,sl.substr(1),sr);
        ans+=go(l,r-1,sl,sr+s[r]);
    } else if ( !sr.empty() ) {
        assert(s[l]==sr[0]);
        ans+=go(l+1,r,sl,sr.substr(1));
        ans+=go(l+1,r,sl+s[l],sr);
    } else {
        ans+=2*go(l+1,r,sl+s[l],sr);
    }
    return dp[k]=ans;
}

void main() {
    R(n,s);
    auto ans=go(0,2*n-1,"","");
    W(ans);
}

// {{{ main
}}
int main() { shik::main(); return 0; }
// }}}

提出情報

提出日時
問題 C - String Coloring
ユーザ shik
言語 C++14 (GCC 5.4.1)
得点 600
コード長 3121 Byte
結果
実行時間 28 ms
メモリ 512 KB

テストケース

セット名 得点 / 配点 テストケース
Sample 0 / 0 example_0, example_1, example_2, example_3
All 600 / 600 almost_z_0, almost_z_1, almost_z_2, almost_z_3, bigrand_0, bigrand_1, bigrand_2, example_0, example_1, example_2, example_3, handmade_0, handmade_1, nonzero_0, nonzero_1, nonzero_2, nonzero_3, nonzero_4, nonzero_5, nonzero_sc_0, nonzero_sc_1, nonzero_sc_10, nonzero_sc_11, nonzero_sc_2, nonzero_sc_3, nonzero_sc_4, nonzero_sc_5, nonzero_sc_6, nonzero_sc_7, nonzero_sc_8, nonzero_sc_9, nonzero_small_0, nonzero_small_1, nonzero_small_2, nonzero_small_3, rand_0, rand_1, rand_2, runnur_0, runnur_1, runnur_2, runnur_3, runnur_4
ケース名 結果 実行時間 メモリ
almost_z_0 2 ms 384 KB
almost_z_1 1 ms 256 KB
almost_z_2 1 ms 256 KB
almost_z_3 1 ms 256 KB
bigrand_0 1 ms 256 KB
bigrand_1 1 ms 256 KB
bigrand_2 1 ms 256 KB
example_0 1 ms 256 KB
example_1 1 ms 256 KB
example_2 28 ms 384 KB
example_3 1 ms 256 KB
handmade_0 1 ms 256 KB
handmade_1 1 ms 256 KB
nonzero_0 1 ms 256 KB
nonzero_1 1 ms 256 KB
nonzero_2 1 ms 256 KB
nonzero_3 1 ms 256 KB
nonzero_4 1 ms 256 KB
nonzero_5 1 ms 256 KB
nonzero_sc_0 1 ms 256 KB
nonzero_sc_1 2 ms 384 KB
nonzero_sc_10 2 ms 384 KB
nonzero_sc_11 2 ms 384 KB
nonzero_sc_2 2 ms 384 KB
nonzero_sc_3 2 ms 384 KB
nonzero_sc_4 2 ms 384 KB
nonzero_sc_5 1 ms 256 KB
nonzero_sc_6 1 ms 256 KB
nonzero_sc_7 2 ms 384 KB
nonzero_sc_8 3 ms 512 KB
nonzero_sc_9 2 ms 384 KB
nonzero_small_0 1 ms 256 KB
nonzero_small_1 1 ms 256 KB
nonzero_small_2 1 ms 256 KB
nonzero_small_3 1 ms 256 KB
rand_0 1 ms 256 KB
rand_1 1 ms 256 KB
rand_2 1 ms 256 KB
runnur_0 1 ms 256 KB
runnur_1 1 ms 256 KB
runnur_2 1 ms 256 KB
runnur_3 1 ms 256 KB
runnur_4 1 ms 256 KB