fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #include <deque>
  7. #include <map>
  8. using namespace std;
  9.  
  10. #define pb push_back
  11. string imp()
  12. {
  13. return "IMPOSSIBLE";
  14. }
  15. // 0 - R
  16. // 1 - P
  17. // 2 - S
  18. const int MAXCNT = 501;
  19. int ccnt[MAXCNT];
  20. int cnt[MAXCNT][3];
  21. void solve()
  22. {
  23. for(int i = 0; i < MAXCNT; i++)
  24. {
  25. ccnt[i] = 0;
  26. for(int j = 0; j < 3; j++)
  27. {
  28. cnt[i][j] = 0;
  29. }
  30. }
  31. int a; cin >> a;
  32. vector<string> opp(a);
  33. for(int i = 0; i < a; i++)
  34. {
  35. cin >> opp[i];
  36. }
  37. for(int i = 0; i < a; i++)
  38. {
  39. string st = opp[i];
  40. int stl = st.size();
  41. for (int j = 0; j < MAXCNT; j++)
  42. {
  43. if (st[j % stl] == 'R')
  44. {
  45. cnt[j][0] += 1;
  46. }
  47. else if (st[j % stl] == 'P')
  48. {
  49. cnt[j][1] += 1;
  50. }
  51. else
  52. {
  53. cnt[j][2] += 1;
  54. }
  55. }
  56. }
  57. for (int i = 0; i < MAXCNT; i++)
  58. {
  59. int temp = 0;
  60. for (int j = 0; j < 3; j++)
  61. if (cnt[i][j] != 0)
  62. temp += 1;
  63. ccnt[i] = temp;
  64. }
  65.  
  66. map<char,char> mp1;
  67. mp1['R'] = 'P';
  68. mp1['P'] = 'S';
  69. mp1['S'] = 'R';
  70. map<string, char> mp2;
  71. mp2["RP"] = 'P';
  72. mp2["PR"] = 'P';
  73. mp2["RS"] = 'R';
  74. mp2["SR"] = 'R';
  75. mp2["SP"] = 'S';
  76. mp2["PS"] = 'S';
  77.  
  78.  
  79. int x1 = 0;
  80. for (int i = 0; i < MAXCNT; i++)
  81. {
  82. if(ccnt[i] == 1)
  83. x1 += 1;
  84. }
  85. if(x1 == 0)
  86. {
  87. cout << imp() << "\n";
  88. return;
  89. }
  90. int x3 = 0;
  91. int i = 0;
  92. while (i < MAXCNT && ccnt[i] != 1)
  93. {
  94. if (ccnt[i] == 3)
  95. x3 += 1;
  96. i += 1;
  97. }
  98. if (x3 >= 1)
  99. {
  100. cout << imp() << "\n";
  101. return;
  102. }
  103. string fans = "";
  104. i = 0;
  105. while (i < MAXCNT && ccnt[i] != 1)
  106. {
  107. string ss = "";
  108. if (cnt[i][0] != 0)
  109. ss.pb('R');
  110. if (cnt[i][1] != 0)
  111. ss.pb('P');
  112. if (cnt[i][2] != 0)
  113. ss.pb('S');
  114. i += 1;
  115. fans.pb(mp2[ss]);
  116. }
  117. char ch;
  118. if (cnt[i][0] != 0)
  119. ch = 'R';
  120. if (cnt[i][1] != 0)
  121. ch = 'P';;
  122. if (cnt[i][2] != 0)
  123. ch = 'S';
  124. fans.pb(mp1[ch]);
  125. cout << fans << "\n";
  126. }
  127.  
  128. int main()
  129. {
  130. int num_test_cases;
  131. cin >> num_test_cases;
  132. for (int t = 1; t <= num_test_cases; ++t)
  133. {
  134. cout << "Case #" << t << ": ";
  135. solve();
  136. }
  137. return 0;
  138. }
  139.  
Success #stdin #stdout 0s 15256KB
stdin
3
1
RS
3
R
P
S
7
RS
RS
RS
RS
RS
RS
RS
stdout
Case #1: P
Case #2: IMPOSSIBLE
Case #3: P