zoukankan      html  css  js  c++  java
  • 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 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.

    代码:

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

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

  • 相关阅读:
    Linux 网络流量实时监控工具之ntopng详解
    linux wget 命令用法详解(附实例说明)
    Linux curl使用简单介绍
    数据库中随机查询数据
    【算法】验证码识别基础方法及源码(转)
    CentOS 7静默(无图形化界面)安装Oracle 11g
    史上最详细最全的Linux上安装Oracle的教程-centos7
    如何实现Docker镜像和容器实例的备份迁移
    oozie timezone时区配置
    通过hue提交oozie定时任务
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10184557.html
Copyright © 2011-2022 走看看