codejam 2019 round 1C Robot Programming Strategy (构造)

链接:https://codingcompetitions.withgoogle.com/codejam/round/00000000000516b9/0000000000134c90

 


 

 

题目:

Problem

After many sleepless nights, you have finally finished teaching a robotic arm to make the hand gestures required for the Rock-Paper-Scissors game. Now you just need to program it to compete in the upcoming robot tournament!

In this tournament, each robot uses a program that is a series of moves, each of which must be one of the following: R (for "Rock"), P (for "Paper"), or S (for "Scissors"). Paper beats Rock and loses to Scissors; Rock beats Scissors and loses to Paper; Scissors beats Paper and loses to Rock.

When two robots face off in a match, the first robot to play a winning move wins. To start, each robot plays the first move of its program. If the two moves are different, one of the moves beats the other and thus one of the robots wins the match. If the moves are the same, each robot plays the next move in its program, and so on.

Whenever a robot has reached the end of its program and needs its next move, it returns to the start of its program. So, for example, the fifth move of a robot with the program RSSP would be R. If a match goes on for over a googol (10100) of moves, the judges flip a fair coin to determine the winner.

Once a match is over, the winning robot resets, so it has no memory of the that match. In its next match, it starts by playing the first move of its program, and so on.

The tournament is played in K rounds and has a single-elimination "bracket" structure. There are N = 2K robots in total, numbered 0 through N - 1. In the first round, robot 0 plays a match against robot 1, robot 2 plays a match against robot 3, and so on, up to robots N - 2 and N - 1. The losers of those matches are eliminated from the tournament. In the second round, the winner of the 0-1 match faces off against the winner of the 2-3 match, and so on. Once we get to the K-th round, there is only one match, and it determines the overall winner of the tournament.

All of the other contestants are so confident that they have already publicly posted their robots' programs online. However, the robots have not yet been assigned numbers, so nobody knows in advance who their opponents will be. Knowing all of the other programs, is it possible for you to write a program that is guaranteed to win the tournament, no matter how the robot numbers are assigned?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case begins with one line containing an integer A: the number of adversaries (other robots) in the tournament. Then, there are A more lines; the i-th of these contains a string Ci of uppercase letters that represent the program of the i-th opponent's robot.

Output

For each test case, output one line containing Case #x: y. If there is a string of between 1 and 500 characters that is guaranteed to win the tournament, as described above, then y should be the string of uppercase letters representing that program. Otherwise, y should be IMPOSSIBLE, in uppercase letters.

Limits

1 ≤ T ≤ 100.
Time limit: 20 seconds per test set.
Memory limit: 1GB.
Each character in Ci is uppercase RP, or S, for all i.
A = 2K - 1 for some integer K ≥ 1.

Test set 1 (Visible)

1 ≤ A ≤ 7.
Ci is between 1 and 5 characters long, for all i.

Test set 2 (Hidden)

1 ≤ A ≤ 255.
Ci is between 1 and 500 characters long, for all i.

Sample


Input 
 

Output 
 
3
1
RS
3
R
P
S
7
RS
RS
RS
RS
RS
RS
RS

  
Case #1: RSRSRSP
Case #2: IMPOSSIBLE
Case #3: P

  

Note: Although all the opponents in each of these sample cases have programs of the same length, this is not necessarily the case. Opponents within a test case might have programs of different lengths.

In Sample Case #1, there is only one opponent, with the program RS. Our answer matches the opponent's moves for a while, and the opponent loops through its program several times. As is starts its fourth pass through its program, we beat it with P. Other valid solutions exist, like PRR, and R.

In Sample Case #2, there are three opponents, with the programs RP, and S. It is up to you to figure out why this case is IMPOSSIBLE!

In Sample Case #3, all seven opponents use the same program. Using the program P, for example, guarantees that you will win. Remember that each robot begins at the start of its program at the start of each match against a new opponent.

 


题意:

给出R,S,P三者的制衡关系 每个机器人有一串由R,S,P组成的字符串代表机器人的程序 需要为自己的机器人写一串字符串以保证可以打败所有机器人 如果不存在这样的字符串 输出IMPOSSIBLE

 

思路:

先将所有机器人的字符串都扩展到maxx 然后对每一位都进行判断操作 如果在某一位上所有还没有被打败的机器人的字符串出现的字母种类到达了三种 即表示不管怎么构造都无法必赢 直接不可能 如果只出现两种字母 采用打败一部分和另一部分打平的策咯 如果只出现一种字母 直接打败 每一位判断完后都把被打败的机器人标志 下一位的判断不再考虑这些被打败的


代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=550;
int t,n,len[maxn],cnt[maxn],vis[maxn];
char s[maxn][maxn],ss[maxn][maxn],ans[maxn];

int get(char c){
    if(c=='R') return 0;
    if(c=='S') return 1;
    if(c=='P') return 2;
}

int main(){
    scanf("%d",&t);
    for(int id=1;id<=t;id++){
        memset(ss,0,sizeof(ss));
        memset(ans,0,sizeof(ans));
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        int maxx=505;
        for(int i=1;i<=n;i++){
            scanf("%s",s[i]);
            len[i]=strlen(s[i]);
        }
        printf("Case #%d: ",id);
        for(int i=1;i<=n;i++){
            for(int j=0;j<maxx;j++){
                ss[i][j]=s[i][j%len[i]];
            }
        }
        int flag=0,tot=0;
        for(int j=0;j<maxx;j++){
            cnt[0]=cnt[1]=cnt[2]=0;
            for(int i=1;i<=n;i++){
                if(!vis[i]) cnt[get(ss[i][j])]=1;
            }
            if(cnt[0]==1 && cnt[1]==1 && cnt[2]==1){
                flag=1;
                break;
            }
            if(cnt[0]==1 && cnt[1]==0 && cnt[2]==0){
                ans[++tot]='P';
                break;
            }
            if(cnt[0]==0 && cnt[1]==1 && cnt[2]==0){
                ans[++tot]='R';
                break;
            }
            if(cnt[0]==0 && cnt[1]==0 && cnt[2]==1){
                ans[++tot]='S';
                break;
            }
            if(cnt[0]==1 && cnt[1]==1 && cnt[2]==0){
                ans[++tot]='R';
            }
            if(cnt[0]==1 && cnt[1]==0 && cnt[2]==1){
                ans[++tot]='P';
            }
            if(cnt[0]==0 && cnt[1]==1 && cnt[2]==1){
                ans[++tot]='S';
            }
            for(int i=1;i<=n;i++){
                if(!vis[i]){
                    if(ans[tot]=='P' && ss[i][j]=='R') vis[i]=1;
                    if(ans[tot]=='R' && ss[i][j]=='S') vis[i]=1;
                    if(ans[tot]=='S' && ss[i][j]=='P') vis[i]=1;
                }
            }
        }
        if(flag==1) printf("IMPOSSIBLE\n");
        else printf("%s\n",ans+1);
    }
    return 0;
} 

 

posted @ 2019-05-05 16:08  周园顾  阅读(286)  评论(0编辑  收藏  举报