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

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

  • 相关阅读:
    ZYB建围墙
    换根DP
    原码,反码,补码的计算
    字符串游戏-博弈论-trie
    【十连赛day8】神炎皇
    生成序列
    【USACO 2006 February Silver】产奶安排Stall Reservations-贪心
    tar 压缩解压命令
    java 注解
    回溯算法解决全排列问题
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10184557.html
Copyright © 2011-2022 走看看