devit121's blog

By devit121, history, 2 months 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

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by devit121 (previous revision, new revision, compare).

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

First, submit to us.

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

    I already did test 2 test 3

    • »
      »
      »
      2 months 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.

      • »
        »
        »
        »
        2 months 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

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

          Since we found your submission, its ok.

          • »
            »
            »
            »
            »
            »
            2 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            • »
              »
              »
              »
              »
              »
              »
              2 months 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.

              • »
                »
                »
                »
                »
                »
                »
                »
                2 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Alright thanks

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  2 months 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.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  2 months 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.

»
2 months 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.