Code not accepted in contest why?what's the error?

Continuing the discussion from July Cook-Off 2019 - Discussion:

my solution was not accepted. what’s the error in my solution for PLAYSTR problem?

The size of your char array should be n+1 instead of n. 1 extra character ‘\0’ is appended to mark the end of the string

but i tried with many examples and got correct outputs

You’re taking input as cin>>s
Since the string s has a size of length n, it will try to fit in n bytes starting from the base address of string s. Now, it is quite possible that the (n+1)th byte is also free and hence the compiler will mark the end of your string by appending ‘\0’ at the (n+1)th byte. But this is not mandatory. In case the (n+1)th byte is already allocated to some other process, your string won’t be saved successfully

You should take input like this if you don’t want to waste two extra bytes:


you should make the size n+1 as mentioned by @utkarsh911.
char s[n+1],r[n+1];
and talking about “i tried with many examples and got correct outputs”, where did you get that ACs? :slightly_smiling_face:. You have to make it n+1 for occupying null character, else you won’t get Correct Answer. Or use:
string str;

1 Like

ok i got it. Thank’s bro.

but have u got any wrong output yet

have u got any wrong output

It all depends on the memory configuration. Even such a case is possible where your code gets correct output when run on your PC and gets wrong output when run on my PC or on CodeChef’s judge. Basically, your code has “undefined behaviour” on different or same inputs as well.

1 Like