C. Prefixes and Suffixes (codefoces)

Ivan wants to play a game with you. He picked some string ?s of length ?n consisting only of lowercase Latin letters. 

You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from 11 to ?−1n−1), but he didn't tell you which strings are prefixes and which are suffixes.

Ivan wants you to guess which of the given 2?−22n−2 strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!

Input

The first line of the input contains one integer number ?n (2≤?≤1002≤n≤100) — the length of the guessed string ?s.

The next 2?−22n−2 lines are contain prefixes and suffixes, one per line. Each of them is the string of length from 11 to ?−1n−1 consisting only of lowercase Latin letters. They can be given in arbitrary order.

It is guaranteed that there are exactly 22 strings of each length from 11 to ?−1n−1. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length ?n.

Output

Print one string of length 2?−22n−2 — the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The ?i-th character of this string should be 'P' if the ?i-th of the input strings is the prefix and 'S' otherwise.

If there are several possible answers, you can print any.

Examples

input

Copy

5
ba
a
abab
a
aba
baba
ab
aba

output

Copy

SPPSPSPS

input

Copy

3
a
aa
aa
a

output

Copy

PPSS

input

Copy

2
a
c

output

Copy

PS

Note

The only string which Ivan can guess in the first example is "ababa".

The only string which Ivan can guess in the second example is "aaa". Answers "SPSP", "SSPP" and "PSPS" are also acceptable.

In the third example Ivan can guess the string "ac" or the string "ca". The answer "SP" is also acceptable.

思路: 考虑到长度为n-1的两个字符串大部分情况下可以确定整个字符串(通过比较哪一部分为共同部分)即比较str1(1~n-1)与str2(0~n-2)是否相同,以及比较str1(0~n-2)与str2(1~n-1)是否相同。   若两者都相同则还不能确定整个字符串。考虑到。若两个都相同 一定是例如:abababa这种交替的形式。则这时可以可以通过长度为1和长度为2的字符串来确定。具体要分奇偶讨论(这个自己想一下应该能明白,详细实现见代码)

# include <iostream>
# include <cstdio>
#include<algorithm>
#include<string>
using namespace std;
string str[205],str2[205],ch1,ch2,sh1,sh2,ans;
int ok[205];
bool cmp(string s1,string s2){
    return s1.length()<s2.length();
}
int main(){
    int n;
    cin>>n;
    int cnt1=0,cnt2=0;
    for(int i=0;i<2*n-2;i++){
        cin>>str[i];
        str2[i]=str[i];
    }
    sort(str,str+2*n-2,cmp);
    for(int i=0,j=1;i<n-2;i++,j++){//判断相同部分
        if(str[2*n-4][i]!=str[2*n-3][j]){
            cnt1=1;
            break;
        }
    }
    for(int i=0,j=1;i<n-2;i++,j++){
        if(str[2*n-3][i]!=str[2*n-4][j]){
            cnt2=1;
            break;
        }
    }
    
    
    if(cnt1+cnt2==0){//若两部分都相同
        if(str[0]!=str[1]){//若为偶数 则根据长度为1的判断
            if(str[2][1]==str[2*n-3][0]){
                ans=str[2][0]+str[2*n-3];
            }
            else{
                ans=str[2*n-3]+str[2][0];
            }
        }
        else{//若为奇数   则根据长度为2的判断
            if(str[0][0]==str[2*n-3][0]){
                ans=str[2*n-3]+str[1];
            }
            else{
                ans=str[0]+str[2*n-3];
            }
        }
    }
    else if(cnt1==0){
        
        ans=str[2*n-3][0]+str[2*n-4];
    }
    else{
        ans=str[2*n-4][0]+str[2*n-3];
    }
    int flag=0;
    for(int i=0;i<2*n-2;i++){
        flag=0;
        for(int j=0;j<str2[i].length();j++){
            if(str2[i][j]!=ans[j]){
                flag=1;
                break;
            }
        }
        if(ok[str2[i].length()]==0){//由于长度相同的两个串要求必须一个‘P’一个‘S’ 用ok【】记录一下
            if(flag){
                cout<<'S';
                ok[str2[i].length()]=-1;
            }
            else{
                cout<<'P';
                ok[str2[i].length()]=1;
            }
        }
        else{
            if(ok[str2[i].length()]==1){
                cout<<'S';
            }
            else{
                cout<<'P';
            }
        }
    }
    cout<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值