简体   繁体   English

测试 10 代码力超出时间限制

[英]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.你好,我是编程初学者,正在学习数组课程,我只知道非常基础的知识,例如 if 条件、循环和数据类型,以及何时尝试解决此问题。

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. Serezha 立即开始和他们一起玩,因为他还不能阅读,所以他们洗牌。 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.第一行包含一个整数 n (1⩽n⩽10 5 )——字符串的长度。 The second line contains a string consisting of English lowercase letters: 'z', 'e', 'r', 'o' and 'n'.第二行包含一个由英文小写字母组成的字符串:“z”、“e”、“r”、“o”和“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.保证可以重新排列字母,使它们形成一个单词序列,每个单词要么是对应于数字 00 的“零”,要么是对应于数字 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我在测试 10 次代码强制时超出了时间限制,这就是我的代码

#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.您在堆栈上放置了一个糟糕的 10K 数组,但问题描述说您最多可以有 100K 个字符。

After your array fills up, you start overwriting the stack, including the variable n .数组填满后,您开始覆盖堆栈,包括变量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.与其在堆栈上放置一个更糟糕的 100K 数组,不如在读取输入时计算 z 和 n 的数量,根本不用费心存储字符串。

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.为了修复 TLE,您需要提高效率。
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.你只需要在输入和一个输出循环中计算两件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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