902 - Password Search
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 902 - Password Search
Remove line 37:
entrada.close();
entrada.close();
Check input and AC output for thousands of problems on uDebug!
Re: 902 - Password Search
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.
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.

-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 902 - Password Search
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!
Re: 902 - Password Search
Thank you brianfry713 again!, but using Scanner instead of BufferedReader I got AC with entrada.close(); 

Re: 902 - Password Search
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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 902 - Password Search
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:
or
And you should print
Code: Select all
3 baababacb 3 baababacb
Code: Select all
3
baababacb
3
baababacb
Code: Select all
aba
aba
Check input and AC output for thousands of problems on uDebug!
Re: 902 - Password Search
For gathering the input, I did the following
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
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.
Code: Select all
while(scanf("%d%s\n", &size, message) != EOF) {
// Do stuff
}
Also, the following lines to gather input work
Code: Select all
while(cin >> size >> message) {
// Do stuff
}
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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 902 - Password Search
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.
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!
Re: 902 - Password Search
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.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.