WA in count substrings

I tried to solve the COUNT SUBSTRINGS problem. My program counts the number of 1’s in the string and prints the output (count*(count+1))/2. I dont know where I am going wrong. This is the link to my


(http://www.codechef.com/viewsolution/4267717).  Someone please help me.

Hi @shrutis1051,

You are storing the result in double,which is not necessary as answer is always going to be an integer,you can you int data types…

like long int or probably long long int

Hope it helps…:slight_smile:
Happy coding.

[Edit]

you can refer it…

http://www.codechef.com/viewsolution/4179005

I tried using long long int, still it gives the wrong answer :frowning:

No but I check the string only upto its size. But it gave the correct answer when I used the printf statement instead of cout. I dont know what difference it made. Thanks a lot for your help.

I checked your last solution,
http://www.codechef.com/viewsolution/4337435

You are taking long int not long long int, the size of both long int
and int is 32 bits.

We need count to 8 bytes(64 bits).

So, use long long int instead of long int

1 Like

@ prtkul123 I used long int coz someone mentioned here that using long long causes overflow. Thanks for your help though.

Your code is working well check here,

http://www.codechef.com/viewsolution/4337449

P.S.

Just a general tip use scanf,printf for fast I/O as cin cout works slow…

Happy coding… :slight_smile:

1 Like

Thank you so much :slight_smile:

The difference was not due to printf as you mentioned above. It was due to long long int.

Here, http://www.codechef.com/viewsolution/4337454

I have corrected your last WA, and the only correction needed was long long int. So, it was not due to printf.

Thanks…That was really helpful to clear my doubts :slight_smile:

Your welcome…:slight_smile:

Yeah i just didnt saw you are using C++,while in C you have to reinitialize your char[] to ‘\0’… :slight_smile:

Welcome
Happy Coding

I thought I should also share this as fast I/O is mentioned.
cout can be made faster. Read this link below.
http://discuss.codechef.com/questions/43534/enormous-input-test-c?page=1#43536
Also in addition to this post use cout<<"\n"; instead of cout<<endl; it is faster.

2 Likes