简体   繁体   中英

Time limit exceeded on test 10 code forces

hello i am a beginner in programming and am in the array lessons ,i just know very basics like if conditions and loops and data types , and when i try to solve this problem.

Problem Description

When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided to rearrange them. Help him restore the original number, on condition that it was the maximum possible one.

Input Specification

The first line contains a single integer n (1⩽n⩽10 5 ) — the length of the string. The second line contains a string consisting of English lowercase letters: 'z', 'e', 'r', 'o' and 'n'.

It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either "zero" which corresponds to the digit 00 or "one" which corresponds to the digit 11.

Output Specification

Print the maximum possible number in binary notation. Print binary digits separated by a space. The leading zeroes are allowed.

  1. Sample input:

     4 ezor

    Output:

     0
  2. Sample Input:

     10 nznooeeoer

    Output:

     1 1 0

i got Time limit exceeded on test 10 code forces and that is my code

#include <iostream>
using namespace std;
int main()
{
    int n;
    char arr[10000];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    for (int i = 0; i < n; i++) {
        if (arr[i] == 'n') {
            cout << "1"
                 << " ";
        }
    }
    for (int i = 0; i < n; i++) {
        if (arr[i] == 'z') {
            cout << "0"
                 << " ";
        }
    }
}

Your problem is a buffer overrun. You put an awful 10K array on the stack, but the problem description says you can have up to 100K characters.

After your array fills up, you start overwriting the stack, including the variable n . This makes you try to read too many characters. When your program gets to the end of the input, it waits forever for more.

Instead of putting an even more awful 100K array on the stack, just count the number of z's and n's as you're reading the input, and don't bother storing the string at all.

According to the compromise (applicable to homework and challenge questions) described here
How do I ask and answer homework questions?
I will hint, without giving a code solution.

In order to fix TLEs you need to be more efficient.
In this case I'd start by getting rid of one of the three loops and of all of the array accesses.
You only need to count two things during input and one output loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM