|
| 1 | +//Problem Link: https://codingcompetitions.withgoogle.com/codejam/round/00000000000516b9/0000000000134c90 |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | +const char R = 'R'; |
| 5 | +const char P = 'P'; |
| 6 | +const char S = 'S'; |
| 7 | +map<char,char> win, l; |
| 8 | + |
| 9 | +char M[300][603]; |
| 10 | +int len[300], ign[300]; |
| 11 | +char winner(set<char> a) |
| 12 | +{ |
| 13 | + char x = *a.begin(); |
| 14 | + char y = *a.rbegin(); |
| 15 | + string z = ""; z += x; z += y; |
| 16 | + if (z == "PR") return R; |
| 17 | + if (z == "PS") return P; |
| 18 | + if (z == "RS") return S; |
| 19 | +} |
| 20 | + |
| 21 | +int main() { |
| 22 | + win[R]=P; |
| 23 | + win[P]=S; |
| 24 | + win[S]=R; |
| 25 | + |
| 26 | + l[R]=S; |
| 27 | + l[P]=R; |
| 28 | + l[S]=P; |
| 29 | + ios_base::sync_with_stdio(0); |
| 30 | + int t, i, j, nj, n; |
| 31 | + string s; |
| 32 | + cin >> t; |
| 33 | + int T = t; |
| 34 | + while(t--) { |
| 35 | + for(i=0; i < 300; i++) len[i] = ign[i] = 0; |
| 36 | + cout << "Case #" << T-t << ": "; |
| 37 | + cin >> n; |
| 38 | + int mxl = 0; |
| 39 | + for(i = 0; i < n; i++) { |
| 40 | + cin >> s; |
| 41 | + len[i] = s.length(); len[i] = min(len[i], 500); |
| 42 | + mxl = max(mxl, len[i]); mxl = min(mxl, 500); |
| 43 | + |
| 44 | + for(j = 0; j < 500; j++) { |
| 45 | + nj = j%len[i]; |
| 46 | + M[i][j] = s[nj]; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + int flag = 0;int count = 0; |
| 51 | + string ans = ""; |
| 52 | + for( j = 0; j < 500; j++) { |
| 53 | + set<char> choices = {R, P, S}; |
| 54 | + set<char> want = {}; |
| 55 | + |
| 56 | + for( i = 0; i < n; i++) { |
| 57 | + if(ign[i]) continue; |
| 58 | + char c = M[i][j]; |
| 59 | + choices.erase(l[c]); |
| 60 | + want.insert(win[c]); |
| 61 | + } |
| 62 | + if(choices.empty() or want.size() == 3) { |
| 63 | + flag = -1; //IMPOSSIBLE |
| 64 | + break; |
| 65 | + } |
| 66 | + if(want.size() == 0) { |
| 67 | + flag = 1; // WE WON |
| 68 | + break; |
| 69 | + } |
| 70 | + if(want.size() == 1) { |
| 71 | + flag = 1; // WE WON |
| 72 | + ans += *want.begin(); |
| 73 | + break; |
| 74 | + } |
| 75 | + //want 2 |
| 76 | + char chala = winner(want); |
| 77 | + ans += chala; |
| 78 | + for( i = 0; i < n; i++) { |
| 79 | + if(l[chala] == M[i][j]) ign[i] = 1; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + for(i = 0; i < n; i++) if(ign[i] == 1) count++; |
| 84 | + if(count == n) flag = 1; |
| 85 | + if(flag == -1) cout << "IMPOSSIBLE\n"; |
| 86 | + else cout << ans << "\n"; |
| 87 | + } |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
0 commit comments