#include #include #include #include using namespace std; bool judge(string); int main(int argc, char *argv[]) { int T; string pattern; cin >> T; for (int i = 0; i < T; i++) { cin >> pattern; cout << (judge(pattern) ? "possible" : "impossible") << endl; } } bool judge(string s) { int numOfWhite = 0; int numOfGreen = 0; int lastW; int lastG; int len = s.length(); char *c = (char *)malloc(sizeof(char) * len + 1); strcpy(c, s.c_str()); for (int i = 0; i < len; i++) { if (c[i] == 'W') { numOfWhite++; lastW = i; } else if (c[i] == 'G') { if (numOfWhite < 1) { return false; } numOfGreen++; lastG = i; } else { if (numOfGreen > 0) { numOfWhite--; numOfGreen--; } else { return false; } } } if (numOfGreen > 0) { return false; } if (lastW > lastG) { return false; } return true; }