Luo Gu P5595 [XR-4] singing contest

Portal

Ideas && Code

This is a popular - topics

So there is a question and a question very similar, you can do this question, come back to do this, link:

\ (P3742 \ umi \) function

Then we consider the practice of solving the problem

Since it is \ (Special \ Judge \) , we can directly fucks up, only \ (0 \) and \ (1 \) two numbers, we can \ (A \) out of it friends ~ ~

In fact, the first thing is to get the subject a little bit ignorant force, and then you will find is not difficult to read, we can easily think of an idea: to sweep from front to back, this time if the character is \ (X \) , let \ (X \) the number of this one than \ (Y \) big if at this time the characters \ (Y \) , let \ (Y \) that this number bits than \ (X \) big if at this time the characters \ (the Z-\) , then let \ (X \) and \ (Y \) this bit two numbers are equal

The idea is simply not perfect

Then I jubilation of the situation got to pay, \ (74 \) points

why? ? Because there is no judgment without a solution, I forgot there are examples \ (4 \) that no solution, this is the place I most dishes ......

Then we look at, what kind of situation considered no solution

In fact, this is also more obvious, as in the following case \ (XXXZZYYZ \) , if there is a front \ (the Z-\) , rather than behind \ (the Z-\) , but then appeared \ (the Z-\) , is no solution, why? Because, according to the meaning of problems is, \ (Z \) represented are: from \ (Z \) this one all numbers are equal behind the start that far \ (X \) and \ (Y \) two equal numbers. If the front with \ (the Z-\) , while the middle there has been \ (X \) or \ (the Y-\) , is clearly wrong, so we can first \ (O (n) \) to determine what will not there will be no solution to the situation, as follows

for(int i = 1; i <= len; i++) {
        if(s[i] == 'Z') now = 1;//now表示有没有Z
        if(s[i] != 'Z' && now) return cout << "-1\n", 0;
        //如果从前往后扫,已经出现了Z,后面就不能再出现X或Y,如果出现,即是无解
    }

Then finished, complete code is as follows

/*
By:Loceaner
*/
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
    return x * f;
}

const int N = 1e6 + 11;

char s[N], a[N], b[N];
int now;

int main() {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    for(int i = 1; i <= len; i++) {
        if(s[i] == 'Z') now = 1;
        if(s[i] != 'Z' && now) return cout << "-1\n", 0;
    }
    for(int i = 1; i <= len; i++) {
        if(s[i] == 'X') a[i] = '1', b[i] = '0';
        else if(s[i] == 'Y') a[i] = '0', b[i] = '1';
        else if(s[i] == 'Z') a[i] = '1', b[i] = '1';
    }
    printf("%s\n%s", a + 1, b + 1);
    return 0;
}

Guess you like

Origin www.cnblogs.com/loceaner/p/11711331.html