結果

問題 No.154 市バス
ユーザー ty70ty70
提出日時 2015-05-27 13:09:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,124 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 94,872 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 08:33:04
合計ジャッジ時間 1,409 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,248 KB
testcase_01 AC 11 ms
5,376 KB
testcase_02 AC 11 ms
5,376 KB
testcase_03 AC 7 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 9 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define ERR 1<<10
using namespace std;

typedef long long ll;
typedef pair<int, int> P;


int last_char (string S, char c ){
	for (int i = S.length() - 1; i >= 0; i-- ){
		if (S[i] == c ) return i;
	} // end for

	return -1;
}

int first_char (string S, char c ){
	for (int i = 0; i < S.length(); i++ ){
		if (S[i] == c ) return i;
	} // end for

	return -1;
}

int main()
{
	ios_base::sync_with_stdio(0);

	int T; cin >> T;
	while (T-- ){
		string S = ""; cin >> S;
		int w = (int)count (ALL (S ), 'W' );
		int g = (int)count (ALL (S ), 'G' );
		int r = (int)count (ALL (S ), 'R' );

		bool ok = true;
		ok &= (w > 0 && g > 0 && r > 0 && g == r && w >= g );

		int lastW = last_char(S, 'W' );
		int lastG = last_char(S, 'G' );
		int lastR = last_char(S, 'R' );
		ok &= (lastW < lastG && lastG < lastR ); 

		int firstW = first_char (S, 'W' );
		int firstG = first_char (S, 'G' );
		int firstR = first_char (S, 'R' );
		ok &= (firstW < firstG && firstG < firstR );

		if (ok ){
			stack<char> st;
			rep (i, S.length() ){
				if (S[i] == 'G' ) st.push (S[i] );
				else
				if (S[i] == 'R' ){
					if (st.empty() ){
						ok = false;
						break;
					}else{
						st.pop();
					} // end if
				} // end if
			} // end rep
			if (!st.empty() ) ok = false;
		} // end if
		cout << (ok ? "possible" : "impossible" ) << endl;
	} // end while

	return 0;
}
0