CodeForces Round #527 (Div3) C. Prefixes and Suffixes

Posted zlrrrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces Round #527 (Div3) C. Prefixes and Suffixes相关的知识,希望对你有一定的参考价值。

http://codeforces.com/contest/1092/problem/C

 

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 (2n1002≤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.

代码:

#include <bits/stdc++.h>
using namespace std;

int N;
vector<string> v;
vector<string> l1, l2;
string ans;

int main() {
    scanf("%d", &N);
    int cnt = 0;
    for(int i = 0; i < 2 * N - 2; i ++) {
        string s;
        cin >> s;
        v.push_back(s);
        if(s.size() == N - 1)
            l1.push_back(s);
    }

    string l = "";
    for(int i = 0; i < v.size(); i ++) 
        if(l1[0].substr(1, N - 2) == l1[1].substr(0, N - 2) && v[i] == l1[0].substr(0, v[i].size()))
            cnt ++;

    if(cnt >= N - 1) l = l1[0] + l1[1][N - 2];
    else l = l1[1] + l1[0][N - 2];


    map<int, int> mp;
    for(int i = 0; i < v.size(); i ++) {
        if(v[i] == l.substr(0, v[i].size()) && !mp[v[i].size()]) {
            ans += "P";
            mp[v[i].size()] ++;
        }
        else
            ans += "S";
    }

    cout << ans << endl;
    return 0;
}

  两个小时。。。改的想吐 口区!

以上是关于CodeForces Round #527 (Div3) C. Prefixes and Suffixes的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces Round #527 (Div3) C. Prefixes and Suffixes

cf codeforces round#527F. Tree with Maximum Cost树形dp

Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)

Error Correct System CodeForces - 527B