結果

問題 No.154 市バス
ユーザー ty70ty70
提出日時 2015-05-27 13:58:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,287 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 93,584 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 09:59:56
合計ジャッジ時間 1,517 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
4,376 KB
testcase_01 AC 20 ms
4,380 KB
testcase_02 AC 20 ms
4,380 KB
testcase_03 AC 19 ms
4,376 KB
testcase_04 AC 20 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 12 ms
4,376 KB
testcase_08 AC 2 ms
4,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 INF 1<<10
using namespace std;

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

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

	return INF;
}

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;
}

bool is_order (string S, char c1, char c2, bool flag ){
	
	stack<char> st;
	rep (i, S.length() ){
		if (S[i] == c1 )
			st.push (S[i] );
		else
		if (S[i] == c2 ){
			if (st.empty() ){
				return false;
			}else{
				st.pop();
			} // end if
		} // end if
	} // end rep
	if (flag && !st.empty() ) return false;;

	return true;
}

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 &= (S[0] == 'W' );
		ok &= (S[S.length()-1] == 'R' );
		ok &= (w > 0 && g > 0 && r > 0 && g == r && w >= g );
		ok &= is_order (S, 'W', 'G', false );
		ok &= is_order (S, 'G', 'R', true );

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

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

		cout << (ok ? "possible" : "impossible" ) << endl;
	} // end while

	return 0;
}
0