結果

問題 No.154 市バス
ユーザー やまぞうやまぞう
提出日時 2015-03-31 01:46:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 479 ms
コンパイル使用メモリ 69,212 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 09:53:14
合計ジャッジ時間 1,307 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

void possible()
{
	cout << "possible" << endl;
}

void impossible()
{
	cout << "impossible" << endl;
}

void resolve(const string& s)
{
	vector<string::size_type> index_list(s.length());
	string::size_type indexR = string::npos;
	string::size_type indexG = string::npos;
	string::size_type indexW = string::npos;
	int countR = 0;
	int countG = 0;
	int countW = 0;
	for (string::size_type i = 0; i < s.length(); i++) {
		char ch = s[i];
		switch (ch) {
		case 'R':
			countR++;
			index_list[i] = indexR;
			indexR = i;
			break;
		case 'G':
			countG++;
			index_list[i] = indexG;
			indexG = i;
			break;
		case 'W':
			countW++;
			index_list[i] = indexW;
			indexW = i;
			break;
		}
	}
	if (countR == 0 || countR != countG || countG > countW) {
		impossible();
		return;
	}
	while (countR > 0) {
		if (countG > countW) {
			impossible();
			return;
		}
		if (indexR < indexG || indexG < indexW) {
			impossible();
			return;
		}
		indexR = index_list[indexR];
		countR--;
		indexG = index_list[indexG];
		countG--;
		do {
			if (indexW == string::npos) {
				impossible();
				return;
			}
			indexW = index_list[indexW];
			countW--;
		} while (indexW > indexG);
	}
	possible();
}

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		resolve(s);
	}
	return 0;
}
0