Блог пользователя Ziad0_0

Автор Ziad0_0, история, 7 лет назад, По-английски

Hi, I am trying to solve 118A - String Task and the output I have in my pc is different than the output in codeforces. My submission:26568938

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Why I am downvoted like that?

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

declare str1 and str2 as global variable ;) 26571652

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thank you very much ^_^ , I really appreciate your help, but if you know can you please tell me why the output has changed ? or what was the issue ? Thank you again :)

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I think unintitialized string is undefined behavior (UB) so anything could happen. Different compliers (even in same compliers) may generate different outputs. In order to avoid this problem, You can initialize the array elements to 0 by declare as global or static variables.

      • »
        »
        »
        »
        7 лет назад, # ^ |
          Проголосовать: нравится +3 Проголосовать: не нравится

        Just give str2 an initializer like char str2[101] = {0}; is OK.

        But in competitive programming it's recommended to declare large arrays static/global to avoid stack overflow. This array is small enough and Codeforces is using -Wl,--stack=xxxxxxxxxx so this won't happen.