yamero's blog

By yamero, history, 5 days ago, In English

Hi, im a complete noob in cpp and did this problem 540A - Combination Lock

Here is my submission: 119121702

The test case that the code fails is

12
102021090898
010212908089

The code works perfectly on my pc but when i upload it to codeforces i get a runtime error and i cant figure out why. Any help would be appreciated...

Thanks in advance.

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

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 days ago, # |
  Vote: I like it +1 Vote: I do not like it

stol tries to convert to long.
Type long is depended on machine and compiler. Maybe on your env it is 64-byte, when in CF it is 32 byte.

But in your problem there up to 1000 digits there is no integer type that can hold numbers like that. So your solution is not correct in general

  • »
    »
    5 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yup, that is pretty much the problem.
    What you can do instead, is just use indexing. You can access a single character of any string using same way as you access array's elements state_orig[i] (Think string as an array of chars, and indexing will return a character).
    Another quality of life recommendation would be to use state_orig.pop_back() instead of state_orig.erase(state_orig.length() - 1). Both will do the same thing, but it is easier to understand, and is more cleaner.

    • »
      »
      »
      4 days ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Thank you so much. You were very helpful. I did it correctly this time. 119140129

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

you are using stol which will convert the string into a int and store this value in a int type. Its all fun and easy, untill you come across some string which wont fit in int data type. here in this example you are using a string of length 12, which will definitely not fit in int. instead of converting string into int or long long. just iterate over the chars of the string. check this submission for more understanding 79072424