結果
| 問題 | 
                            No.154 市バス
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2015-03-31 01:46:27 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 27 ms / 2,000 ms | 
| コード長 | 1,469 bytes | 
| コンパイル時間 | 556 ms | 
| コンパイル使用メモリ | 68,556 KB | 
| 実行使用メモリ | 6,824 KB | 
| 最終ジャッジ日時 | 2024-10-13 07:17:35 | 
| 合計ジャッジ時間 | 1,203 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 8 | 
ソースコード
#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;
}