結果

問題 No.291 黒い文字列
ユーザー krotonkroton
提出日時 2015-06-18 00:27:42
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,206 bytes
コンパイル時間 457 ms
コンパイル使用メモリ 59,340 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-07 03:36:50
合計ジャッジ時間 1,488 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
using namespace std;

bool useable(char c, char required) {
	return (c == required || c == '?');
}
void update(int &a, int b){
	if(b > a)a = b;
}

int dp[2][21][21][21][21];

int main() {
	string S;
	cin >> S;

	int crr = 0, nxt = 1;

	memset(dp[crr], -1, sizeof(dp[crr]));
	dp[crr][0][0][0][0] = 0;

	for(char c : S) {
		memset(dp[nxt], -1, sizeof(dp[nxt]));

		for(int K=0;K<=20;K++)
		for(int U=0;U<=K;U++)
		for(int R=0;R<=U;R++)
		for(int O=0;O<=R;O++){
			int now = dp[crr][K][U][R][O];
			if(now == -1)
				continue;

			update(dp[nxt][K][U][R][O], now);

			if(useable(c, 'K') && K + 1 <= 20)
				update(dp[nxt][K+1][U][R][O], now);
			if(useable(c, 'U') && U + 1 <= K)
				update(dp[nxt][K][U+1][R][O], now);
			if(useable(c, 'R') && R + 1 <= U)
				update(dp[nxt][K][U][R+1][O], now);
			if(useable(c, 'O') && O + 1 <= R)
				update(dp[nxt][K][U][R][O+1], now);

			if(useable(c, 'I') && now + 1 <= O)
				update(dp[nxt][K][U][R][O], now + 1);
		}

		swap(crr, nxt);
	}

	int res = 0;
	for(int K=0;K<=20;K++)
	for(int U=0;U<=K;U++)
	for(int R=0;R<=U;R++)
	for(int O=0;O<=R;O++){
		res = max(res, dp[crr][K][U][R][O]);
	}

	cout << res << endl;
	return 0;
}
0