Page 5 of 5

Re: 902 - Password Search

Posted: Wed Sep 11, 2013 10:04 pm
by brianfry713
Remove line 37:
entrada.close();

Re: 902 - Password Search

Posted: Thu Sep 12, 2013 1:32 pm
by MPC1984
Thank you so much brianfry713!
But I don't understand why I must delete entrada.close();. If I don't close the buffer, next person who prove the same problem can have problems. And in the other 121 problems I have solved, I put that line and I never have had problem with it. :-?

Re: 902 - Password Search

Posted: Fri Sep 13, 2013 12:08 am
by brianfry713
I can't see your code anymore, but when I ran it before it threw a RE on that line. You don't have to close a BufferedReader, but you should close or flush a BufferedWriter. I am able to write AC code in JAVA that closes my BufferedReader, so I would have to see your code again if you want more help.

Re: 902 - Password Search

Posted: Fri Sep 13, 2013 7:21 pm
by MPC1984
Thank you brianfry713 again!, but using Scanner instead of BufferedReader I got AC with entrada.close(); :wink:

Re: 902 - Password Search

Posted: Wed Feb 12, 2014 10:29 pm
by raj
Thanks sir BrainFry for an important issue.. :)

Code: Select all

//Accepted

Re: 902 - Password Search

Posted: Fri Feb 14, 2014 12:24 am
by brianfry713
Try solving it without reading line by line. I think there is an issue with the judge's input, there may be a case like:

Code: Select all

3 baababacb 3 baababacb
or

Code: Select all

3
baababacb
3
baababacb
And you should print

Code: Select all

aba
aba

Re: 902 - Password Search

Posted: Thu Mar 20, 2014 7:29 am
by uDebug
For gathering the input, I did the following

Code: Select all

while(scanf("%d%s\n", &size, message) != EOF) {
    // Do stuff
}
Where "size" is of type "int" and "message" is of type "char[1,000,000]". And this works just fine. It helped when I declared the message array as a global.

Also, the following lines to gather input work

Code: Select all

while(cin >> size >> message) {
    // Do stuff
}
Where "size" is still of type "int" and "message" is now of type "std::string".

Thanks to brianfry713 for his help in troubleshooting.

Update: Post updated after brianfry713's input below. Sections in blue have been reworded since the original post had incorrect information.

Re: 902 - Password Search

Posted: Thu Mar 20, 2014 8:18 pm
by brianfry713
Your code got TLE because of the line:
while(i + size < strlen(message)) {

If message is large, then computing strlen() every loop will take a lot of time. If the length of the string isn't changing then just compute the strlen() once.

You should also declare large arrays as global so they go on the heap (where more memory is available) instead of the stack.

Re: 902 - Password Search

Posted: Fri Mar 21, 2014 7:51 am
by uDebug
brianfry713 wrote:Your code got TLE because of the line:
while(i + size < strlen(message)) {

If message is large, then computing strlen() every loop will take a lot of time. If the length of the string isn't changing then just compute the strlen() once.

You should also declare large arrays as global so they go on the heap (where more memory is available) instead of the stack.
Thank you. You're right. I implemented both these changes and my code's now AC. So, I stand corrected. I've updated my first post now to reflect this. Thanks again.