902 - Password Search

All about problems in Volume 9. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 902 - Password Search

Post by brianfry713 » Wed Sep 11, 2013 10:04 pm

Remove line 37:
entrada.close();
Check input and AC output for thousands of problems on uDebug!

MPC1984
New poster
Posts: 40
Joined: Mon Jul 01, 2013 9:24 pm
Location: Valladolid, Spain

Re: 902 - Password Search

Post by MPC1984 » Thu Sep 12, 2013 1:32 pm

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. :-?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 902 - Password Search

Post by brianfry713 » Fri Sep 13, 2013 12:08 am

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.
Check input and AC output for thousands of problems on uDebug!

MPC1984
New poster
Posts: 40
Joined: Mon Jul 01, 2013 9:24 pm
Location: Valladolid, Spain

Re: 902 - Password Search

Post by MPC1984 » Fri Sep 13, 2013 7:21 pm

Thank you brianfry713 again!, but using Scanner instead of BufferedReader I got AC with entrada.close(); :wink:

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: 902 - Password Search

Post by raj » Wed Feb 12, 2014 10:29 pm

Thanks sir BrainFry for an important issue.. :)

Code: Select all

//Accepted
Last edited by raj on Fri Feb 14, 2014 8:55 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 902 - Password Search

Post by brianfry713 » Fri Feb 14, 2014 12:24 am

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
Check input and AC output for thousands of problems on uDebug!

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 902 - Password Search

Post by uDebug » Thu Mar 20, 2014 7:29 am

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.
Last edited by uDebug on Fri Mar 21, 2014 7:56 am, edited 1 time in total.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 902 - Password Search

Post by brianfry713 » Thu Mar 20, 2014 8:18 pm

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.
Check input and AC output for thousands of problems on uDebug!

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 902 - Password Search

Post by uDebug » Fri Mar 21, 2014 7:51 am

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Post Reply

Return to “Volume 9 (900-999)”