結果

問題 No.154 市バス
ユーザー startcppstartcpp
提出日時 2016-06-28 18:44:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,630 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 54,080 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 11:52:51
合計ジャッジ時間 1,895 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
4,376 KB
testcase_01 AC 68 ms
4,376 KB
testcase_02 AC 70 ms
4,380 KB
testcase_03 AC 57 ms
4,380 KB
testcase_04 AC 57 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 213 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//自信ない
#include <iostream>
#include <string>
using namespace std;

int t;
string s;

bool check(string s) {
	int i, j;
	int n = s.length();
	bool used[1000] = {false};	//s[i]をGと対応させたかのフラグ
	
	//Gと対応するRがあるかを判定する. 実は、後ろからGRのペアを作れれば「ある」, 作れなければ「ない」となる.
	for (i = n - 1; i >= 0; i--) {
		if (s[i] != 'G') continue;
		for (j = i + 1; j < n; j++) {
			if (s[j] == 'R' && !used[j]) break;
		}
		if (j == n) return false;	//Gと対応するRが見つからなかった
		used[j] = true;				//s[i]とs[j]でペアを作る
	}
	//Gと対応するWがあるかを判定する. 実は、前からWGのペアを作れれば「ある」, 作れなければ「ない」となる.
	for (i = 0; i < n; i++) {
		if (s[i] != 'G') continue;
		for (j = i - 1; j >= 0; j--) {
			if (s[j] == 'W' && !used[j]) break;
		}
		if (j < 0) return false;	//Gと対応するWが見つからなかった
		used[j] = true;				//s[i]とs[j]でペアを作る
	}
	//Rが余っていたり、最後のGよりも後にWが余っていたらダメ, それ以外ならOK (自信ない)
	for (i = 0; i < n; i++) if (s[i] == 'R' && !used[i]) return false;
	for (i = n - 1; i >= 0 && s[i] != 'G'; i--);
	for (; i < n; i++) if (s[i] == 'W' && !used[i]) return false;
	return true;
}

int main() {
	cin >> t;
	while (t--) {	/*tが0以外なら続行→tを1減らす→ループ内の処理→…, つまりt回繰り返す*/
		cin >> s;
		if (check(s)) {
			cout << "possible" << endl;
		}
		else {
			cout << "impossible" << endl;
		}
	}
	return 0;
}
0