結果

問題 No.38 赤青白ブロック
ユーザー kurenai3110
提出日時 2016-09-29 22:35:19
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 1,275 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 85,092 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 13:03:07
合計ジャッジ時間 1,821 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;

int Kr,Kb;
string S;
vector<string>SP;
int mx;

int dfs(string word) {
	if (word.size() <= mx)return 0;
	else if (find(SP.begin(), SP.end(), word) != SP.end())return 0;
	else SP.push_back(word);
	set<int> s;
	queue<int> que;


	for (int i = 0; i < word.size(); i++) {
		if (i + Kr < word.size() && word[i] == 'R'&&word[i + Kr] == 'R') {
			for (int j = i; j <= i + Kr; j++) {
				if (word[j] != 'W') s.insert(j);
			}
		}
		else if (i + Kb < word.size() && word[i] == 'B'&&word[i + Kb] == 'B') {
			for (int j = i; j <= i + Kb; j++) {
				if (word[j] != 'W') s.insert(j);
			}
		}
	}
	if (s.size() == 0) return max(mx, (int)word.size());
	for (auto itr = s.begin(); itr != s.end(); itr++) {
		que.push(*itr);
		//cout << *itr << endl;
	}
	//cout << word << endl;
	while (que.size()) {
		//cout << st.size() << endl;
		string word2 = word;
		int p = que.front(); que.pop();
		//cout << p << endl;
		word2.erase(word2.begin() + p);
		mx = max(mx, dfs(word2));
		//cout << word2 << endl;
		//cout << mx << endl;
	}
	return mx;
}


int main()
{
	cin >> Kr >> Kb;
	cin >> S;

	cout << dfs(S) << endl;

	return 0;
}
0