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 (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.

代码:

#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;
}

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

原文地址:https://www.cnblogs.com/zlrrrr/p/10184557.html

时间: 2024-10-08 13:39:22

CodeForces Round #527 (Div3) C. Prefixes and Suffixes的相关文章

【赛时总结】◇赛时&#183;V◇ Codeforces Round #486 Div3

◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了--为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Substrings Sort +传送门+   [暴力模拟] 题意 给出n个字符串,你需要将它们排序,使得对于每一个字符串,它前面的字符串都是它的子串(对于字符串i,则字符串 1~i-1 都是它的子串). 解析 由于n最大才100,所以 O(n3) 的算法都不会爆,很容易想到暴力模拟. 如果字符串i是字符串j的子串

Codeforces Round #605(Div3)A~E

Codeforces Round #605(Div3)A~E A. Three Friends 题意: 给三个数\(a,b,c\),对每一个数字可以进行一次操作,加一减一或者不变. 求三个数两两组合的差值绝对值的最小值. 思路: 先排个序. 假设排序后三个数从小到大是\(a,b,c\). 那么他们的两两差就是\((c-b)+(c-a)+(b-a)=2c-2a\). 为了让他们的\(2c-2a\)最小,那就缩小\(c\),增大\(a\). #include<bits/stdc++.h> usin

CodeForces Round #498 div3

A: 题目没读, 啥也不会的室友帮我写的. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define f

Codeforces Round #527

D1. Great Vova Wall (Version 1) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-g

Codeforces Round #527 (Div. 3)D2(栈,思维)

#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){    int n;    int mn=0;    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        if(a[i]>mn)            mn=a

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

传送门 A 贪心的取 每个字母n/k次 令r=n%k 让前r个字母各取一次 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define rep(i, a, b) for (int i = a; i <= b; ++i) 5 6 int t, n, k; 7 8 int main() { 9 cin >> t; 10 11 while (t--) { 12 cin >

D. Circular Dance codeforces round#529(div3)

这道题类似与经典题目素数和环,每次找到后两个数字a,b,如果a没被访问过且a是b的父节点(a记得b),就继续把a当下一个节点搜,否则把b当下一个节点搜 值得注意的是当搜到只剩下一个节点的时候,要选取那个没有访问过的节点做最后一个节点,因此需要标记数组 #include<bits/stdc++.h> using namespace std; int a[200010],b[200010]; bool vis[200010]; int n; void dfs(int step,int num) {

Codeforces Round #555 div3 C2

题目大意 给出一个序列,可以从左或从右侧取数,要求取出的序列严格上升 思路 贪心取左右最小的,如果相等则之后只能从一侧取,直接选能取最长的一侧 Code: #include<bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f using namespace std; int ma[10]; int n; int a[(int)(2*1e5)+10]; int main(){ scanf("%d",&n

Codeforces Round #560 div3 (C,D)

C 题目大意: 给出一个字符串,可以删除任意位置上的字符,得到一个好字符串. 长度为偶数,且奇数位置i上的字符与\(i+1\)上的字符不相等. 求最小的操作次数 思路: 暴力,遇到奇数位置与后面位置相同直接删除,注意是答案字符串上的奇数位置 #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<iostream> #include<algor