Codeforces Round #527 (Div. 3) C.Prefixes and Suffixes

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 n1n−1 ), but he didn't tell you which strings are prefixes and which are suffixes.

Ivan wants you to guess which of the given 2n22n−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 2n22n−2 lines are contain prefixes and suffixes, one per line. Each of them is the string of length from 11 to n1n−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 n1n−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 2n22n−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的两个串,一个作为前缀一个作为后缀,就可以拼凑出一个完整的串,从拼出的串中拆出所有前缀和后缀存入multiset中,逐个判断给出的串是否存在集合中。若有不存在的就把最开始n-1的串前后缀反一下。

 

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int n;
vector<string>v,s;
string ans;
int judge(string pre,string suf)
{
    string yuan=pre+suf.substr(n-2);
    //cout<<yuan<<"\n";
    multiset<string> tpre,tsuf,vv;
    for(int i=0;i<n-1;i++)
    {
        tsuf.insert(yuan.substr(i+1));
        vv.insert(yuan.substr(i+1));

        tpre.insert(yuan.substr(0,n-i-1));
        vv.insert(yuan.substr(0,n-i-1));
    }
    if(vv == multiset<string>(v.begin(),v.end()))
    {
        for(int i=0;i<2*n-2;i++)
        {
            if(tpre.count(v[i]))
            {
                ans+='P';
                tpre.erase(tpre.find(v[i]));
            }
            else if(tsuf.count(v[i]))
            {
                ans+='S';
                tsuf.erase(tsuf.find(v[i]));
            }
            else
                return 0;
        }
        return 1;
    }

    return 0;
}
int main()
{
    scanf("%d",&n);
    v = vector<string>(2 * n - 2);
    for(int i=0;i<2*n-2;i++)
    {
        cin>>v[i];
        if(v[i].size()==n-1)
        {
            //cout<<"--"<<t<<"--"<<endl;
            s.push_back(v[i]);
        }
    }
    //cout<<s[0]<<' '<<s[1]<<"\n";

    if(judge(s[0],s[1]))
        cout<<ans;
    else
    {
        judge(s[1],s[0]);
        cout<<ans;
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Kissheart/p/10156293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值