1. 程式人生 > >Codeforces1092C. Prefixes and Suffixes——————思維,字串

Codeforces1092C. Prefixes and Suffixes——————思維,字串

C. Prefixes and Suffixes

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 1 to n−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−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≤n≤100) — the length of the guessed string s.

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

It is guaranteed that there are exactly 2 strings of each length from 1 to n−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 2n−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

5
ba
a
abab
a
aba
baba
ab
aba

output

SPPSPSPS

input

3
a
aa
aa
a

output

PPSS

input

2
a
c

output

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到n-1),但他沒有告訴你哪些字串是字首,哪些是字尾。
伊萬希望你猜測給定的2n-2個字串中哪一個是給定字串的字首,哪些是字尾。你可能無法猜出伊萬選擇的字串(因為多個字串可能會給出相同的字尾和字首集),但如果至少有一個字串與它一致,伊萬將接受您的答案。,讓遊戲開始吧!
輸入
輸入的第一行包含一個整數n(2≤n≤100) - 字串s的長度。
接下來的2n-2行,包含字首和字尾,每行一個。,它們中的每一個都是長度從1到n-1的字串,僅由小寫拉丁字母組成。,它們可以任意順序給出。
保證每個長度恰好有2個字串,從1到n-1。,還保證這些字串是一些長度為n的現有字串的字首和字尾。
輸出
列印一串長度為2n-2的字串 - 僅由字元“P”和“S”組成的字串。,字元“P”的數量應該等於字元“S”的數量。,如果輸入字串的第i個是字首,則該字串的第i個字元應為“P”,否則為“S”。 ,如果有多個可能的答案,您可以列印任何答案。


思路:
把所有字串記錄下位置,然後按照長度排序;
將長度為1和n-1的字串連起來,這樣就構成了一個長度為n的字串,一共有4中情況。
然後和給定的2n-2個字首字尾比較,如果能夠全部匹配,那就說明構造的這個長度為n的字串(可能)是原串,比較的過程中記錄一下那些是字首,那些是字尾。最後輸出就好

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 307;
struct node{
        string s;
        int id;
}a[MAXN];
char ans[MAXN];
int n;
bool cmp(node x,node y)
{
        return x.s.size()<y.s.size();
}

bool isPre(string s,string t)//是字首
{
        for(int i=0;i<t.size();++i)
                if(s[i]!=t[i])
                        return false;
        return true;
}
bool isSuf(string s,string t)//是字尾
{
        for(int i=t.size()-1;i>=0;--i)
                if(s[s.size()-t.size()+i]!=t[i])
                        return false;
        return true;
}

void slove(string S)
{
        for(int i=1;i<=n;i+=2)
        {
                if(isPre(S,a[i].s) && isSuf(S,a[i+1].s))//兩個長度相同的子串必定有一個是字首,一個字尾
                {
                        ans[a[i].id]='P';
                        ans[a[i+1].id]='S';
                        continue;
                }
                if(isPre(S,a[i+1].s) && isSuf(S,a[i].s))
                {
                        ans[a[i].id]='S';
                        ans[a[i+1].id]='P';
                        continue;
                }
                return ;
        }
        for(int i=1;i<=n;++i)
                cout<<ans[i];
        cout<<endl;
        exit(0);
}
int main()
{

        cin>>n;
        n = n*2 - 2;
        for(int i=1;i<=n;++i)
        {
                cin>>a[i].s;
                a[i].id = i;
        }
        sort(a+1,a+n+1,cmp);
        if(n==2)
        {
                puts("PS");
                return 0;
        }
        slove(a[1].s+a[n-1].s);
        slove(a[1].s+a[n].s);
        slove(a[2].s+a[n-1].s);
        slove(a[2].s+a[n].s);
        return 0;
}