devit121's blog

By devit121, history, 3 years ago, In English

I've been trying to do this problem, but it keeps failing on the 2nd test on my computer it gives the correct answer, but when I try to submit it fails. I tried to work around that test case and failed on the 3rd one so what am I doing wrong? It's standard input/output. here's my code:

also, this is my first post so I apologize if some janky sh!t happens

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

First, submit to us.

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I already did test 2 test 3

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 5   Vote: I like it 0 Vote: I do not like it

      PS. This is how link is usually given: submission Hm, interesting. Why this submission is not in your submission pool? Are you doing Virtual Contest?

      PS. Ah! Now I know — I should have pressed "Show unofficial" checkbox to see your submissions.

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Are you trying to say that 'uninitialized value usage' is causing all my problems? or I'm just not able to understand PS. No, I'm not doing a virtual contest

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
          Rev. 4   Vote: I like it 0 Vote: I do not like it

          Since we found your submission, its ok.

          • »
            »
            »
            »
            »
            »
            3 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            • »
              »
              »
              »
              »
              »
              »
              3 years ago, # ^ |
              Rev. 3   Vote: I like it 0 Vote: I do not like it

              In c++ you can use string data type for variable s. Don't use char* — you'd need to manage it in c-style, and it shouldn't be of length ASCII_SIZE — problem states that n is up to 10^5.

              • »
                »
                »
                »
                »
                »
                »
                »
                3 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Alright thanks

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  3 years ago, # ^ |
                  Rev. 2   Vote: I like it 0 Vote: I do not like it

                  And ofcourse, your logic (cnt*cnt)+(b-cnt) is wrong — try to make few examples on a paper. 6 4 AABBCC would give you 4+(4-2), but the answer should be 8.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  3 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Nice. And to add: since n < 10^5 you don't need ll in map<char, ll>. And since you are counting only ASCII letters (big letters to be precise) it's enough to use vector v(26,0) — it works faster — thought it's not a problem here, making it your habit will help you latter.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's because if you declare s[ASCII_SIZE] as a local variable, so the values in s[ASCII_SIZE] are uninitialized which could contain any random values. Meanwhile you read the string char by char, which means it's not guaranteed that your string ends with '\0' (null character = 0). If you use C-string (string with form of array of char), you should always ensure that it ends with '\0' as the terminating character.

In strlen(str), it tries to find '\0' but character '\0' could not be found and it ends up reading an uninitialized memory. That's why you got Error #1: UNINITIALIZED READ in strlen(str).

You could move s[ASCII_SIZE] as global variable, so all chars will be initialized to '\0'. Or, you can read a string by cin >> s, because as I know it will automatically add null terminated to the end of the string.