Codeforces 1092C Prefixes and Suffixes【字符串+思维】

题目链接:点这里

题意:理解错了题意导致WA好几发,QAQ暴击

题意是判断给你的2*n-2个字符串是前缀还是后缀,不是判断这个字符串的内容...我真的欲哭无泪,理解能力太菜了

思路:将两个n-1长的字符串取出,先判断第一个取出的字符串和给出的字符串前缀的匹配程度。如果匹配程度大于半数,则这个为所需字符串-1,否则就是另外一个。同时要注意回文串的情况,所以开了一个cnt数组进行标记,其中p和s的数量要刚好等于n-1···

#include <bits/stdc++.h>
using namespace std;
int cnt[300], n, f;
int main() {
    string mx1 = "", mx2 = "", pr, s[300];
    scanf("%d", &n);
    getchar();
    for (int i = 1; i <= 2 * n - 2; i++) {
        cin >> s[i];
        if (s[i].size() == n - 1) {
            if (mx1.size() == 0)
                mx1 = s[i];
            else
                mx2 = s[i];
        }
    }
    for (int i = 1; i <= 2 * n - 2; i++)
        if (mx1.substr(0, s[i].size()) == s[i])
            f++;

    if (f >= (2 * n - 2) / 2 && mx1.substr(1, n - 2) == mx2.substr(0, n - 2))
        pr = mx1;
    else
        pr = mx2;

    for (int i = 1; i <= n * 2 - 2; i++)
        if (s[i] == pr.substr(0, s[i].size()) && cnt[s[i].size()] != 1) {
            cnt[s[i].size()] = 1;
            printf("P");
        } else {
            printf("S");
        }
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/14003673.html