zoukankan      html  css  js  c++  java
  • 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<<"
    ";
        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]<<"
    ";
    
        if(judge(s[0],s[1]))
            cout<<ans;
        else
        {
            judge(s[1],s[0]);
            cout<<ans;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Windows快捷键大全
    Windows快捷键大全
    长尾理论
    长尾理论
    长尾理论
    Windows快捷键大全
    Windows快捷键大全
    Windows快捷键大全
    长尾理论
    e2eCapWDM Video Capture 服务启动失败解决方法
  • 原文地址:https://www.cnblogs.com/Kissheart/p/10156293.html
Copyright © 2011-2022 走看看