#include #include //WはG,Rのペアに一つづつ必要 //それ以外のWは最後に出てくるG,Rのペアに所属することにしてもんだいない //最後から見ていったときにRの数>Gの数になっているか? => G,Rの順にペアが作れることが保証される //先頭から見ていったときにWの数>Gの数>Rの数になっているか? => W,G,RとなるWが必ず選べる //最後に出てくるGの後ろにWが入っていないか? => ****GWR, ****GRWのパターンを消す int isPossibleOneLine(char * S){ int len = strlen(S); if(len < 3) return 0; int count_w = 0, count_g = 0, count_r = 0; int last_g = 0; //char possible = 1; for(int i = len - 1; i >= 0; i--){ if (S[i] == 'R') count_r++; else if(S[i] == 'G'){ count_g++; if(last_g <= i) last_g = i; } else /*S[i] == 'W'*/ count_w++; if(count_r < count_g) return 0; } count_w = 0; count_g = 0; count_r = 0; for(int i = 0; i < len; i++){ if (S[i] == 'R') count_r++; else if(S[i] == 'G') count_g++; else /*S[i] == 'W'*/ { count_w++; if(last_g < i) return 0; } if(count_w < count_g || count_w < count_r) return 0; } if(count_g == count_r) return 1; else return 0; } int main(){ char S[1001]; int N; scanf("%d",&N); while(N--){ scanf("%s",S); if(isPossibleOneLine(S)) puts("possible"); else puts("impossible"); } }