Codeforces1092C. Prefixes and Suffixes——————思维,字符串

小编 2026-06-05 阅读:1535 评论:0
C. Prefixes and Suffixes Ivan wants to play a game with you. He picked some string s of length n...

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;
}
版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表