Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (string+multiset+思维)

C. Prefixes and Suffixes

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan wants to play a game with you. He picked some string ss of length nn 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 n−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 2n−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 nn (2≤n≤1002≤n≤100) — the length of the guessed string ss.

The next 2n−22n−2 lines are contain prefixes and suffixes, one per line. Each of them is the string of length from 11 to n−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 n−1n−1. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length nn.

Output

Print one string of length 2n−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 ii-th character of this string should be 'P' if the ii-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的字符串的所有n-1个前缀和n-1个后缀,要求判断每个子串是前缀还是后缀

题解:首先按字符串长度排序,可知相同长度的一定一个是前缀一个是后缀(题目保证),可以枚举最后两个哪个是前缀,然后构造出答案串,再用multiset存储所有答案串的前缀和后缀判断是否和输入的一致即可,确定最后两个哪个是前缀串之后便可以确定每一个是否是前缀还是后缀。

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c))c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()

pair<string,int>p[maxn];
int nn,n;

int cmp(pair<string,int> a,pair<string,int> b){
    return a.first.length()<b.first.length();
}

char ans[maxn];
multiset<string>sst;
bool judge(string pre,string suf){
    string s=pre+suf.substr(suf.length()-1);
    multiset<string>st;
    for(int i=0;i<n-1;i++){
        st.insert(s.substr(0,i+1));
        st.insert(s.substr(i+1));
    }
    if(st!=sst) return false;

    for(int i=1;i<=nn;i+=2){
        string ss=s.substr(0,p[i].first.length());
        string sss=s.substr(n-p[i].first.length());
        if(p[i].first==ss && p[i+1].first==sss){
            ans[p[i].second]='P';ans[p[i+1].second]='S';
        }else{
            ans[p[i+1].second]='P';ans[p[i].second]='S';
        }
    }
    return true;
}

int Sheryang(){
    n=read;
    nn=2*n-2;
    for(int i=1;i<=nn;i++){
        cin>>p[i].first;
        p[i].second=i;
        sst.insert(p[i].first);
    }
    sort(p+1,p+1+nn,cmp);

    if(judge(p[nn].first,p[nn-1].first)){
        for(int i=1;i<=nn;i++){
            printf("%c",ans[i]);
        }
    }else if(judge(p[nn-1].first,p[nn].first)){
        for(int i=1;i<=nn;i++){
            printf("%c",ans[i]);
        }
    }   

    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/85130433