You are on page 1of 2

#include <bits/stdc++.

h>

using namespace std;

const int L = 305;


const int A = 260;

void printSolution(const int testNumber, const string &solution)


{
cout<<"Case #"<<testNumber<<": "<<solution<<'\n';
}

void solveTestCase(int number)


{
int a, i, j;
string c[A];
bool defeated[A];

memset(defeated, 0, sizeof(defeated));
// memset(hasR, 0, sizeof(hasR));
// memset(hasP, 0, sizeof(hasP));
// memset(hasS, 0, sizeof(hasS));

cin>>a;
for(i = 0; i < a; i++)
{
string base;
cin>>base;
c[i] = base;
while(c[i].length() < L)
c[i] += base;

/* int len = c.length();

for(j = 0; j < L; j++)


switch(c[j % len])
{
case 'R': hasR[j] = true; break;
case 'P': hasP[j] = true; break;
case 'S': hasS[j] = true; break;
} */
}

string sol;
for(i = 0; i < L; i++)
{
bool hasR, hasP, hasS;
hasR = hasP = hasS = false;
for(j = 0; j < a; j++)
{
if(defeated[j])
continue;

if(c[j][i] == 'R')
hasR = true;
else if(c[j][i] == 'P')
hasP = true;
else if(c[j][i] == 'S')
hasS = true;
}
int cnt = hasR + hasP + hasS;

if(cnt == 1)
{
if(hasR)
sol += "P";
else if(hasP)
sol += "S";
else if(hasS)
sol += "R";

printSolution(number, sol);
return;
}

if(cnt == 2)
{
if(hasR && hasP)
sol += "P";
else if(hasR && hasS)
sol += "R";
else if(hasP && hasS)
sol += "S";
}

else
{
printSolution(number, "IMPOSSIBLE");
return;
}

char x;
if(sol[i] == 'R')
x = 'S';
else if(sol[i] == 'P')
x = 'R';
else if(sol[i] == 'S')
x = 'P';

for(j = 0; j < a; j++)


if(c[j][i] == x)
defeated[j] = true;
}

printSolution(number, sol);
}

int main()
{
int t;
cin>>t;
for(int x = 1; x <= t; x++)
solveTestCase(x);

return 0;
}

You might also like