• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with implementing the right data structure for this problem

 
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:
Evening Folks,

I'm trying to solve the Keystrokes Problem on Open Kattis. To explain briefly, the dude in this example types the password in lower characters but also occasionally uses the left arrow key ("L"), right arrow key ("R") and the backspace ("B") to confuse any keylogger on the machine. You have to write a program which given a specific input of his keystrokes (example: iLnLnLeLb), prints the correct password (in this case benni).

I've tried various approaches in Java using Stack and Deque structures but the evaluator fails them with a timeout. Below is the Deque version which I've tried to optimize as much as possible. It works fine on my local machine but when the evaluator runs with larger string inputs, it just fails with a timeout. Where am I going wrong with this?

 
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Please start by explaining what the keystrokes problem is. It is better you explain it rather than simply posting a link because the solution may become obvious as you go.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Why are you using a for loop rather than for‑each? What is supposed to happen when you encounter a B?
 
Prahlad Yeri
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:Please start by explaining what the keystrokes problem is. It is better you explain it rather than simply posting a link because the solution may become obvious as you go.



I've updated the post and added a brief description of the problem statement.
 
Prahlad Yeri
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:Why are you using a for loop rather than for‑each? What is supposed to happen when you encounter a B?



Is the speed difference between the two that big? I'm trying to optimize for performance here. "B" is for backspace, it means the character before the cursor gets deleted. To keep track of that, I'm removing the last character from the front of the "left" array (I'm maintaining two arrays called "left" and "right" which correspond to left and right of the cursor).
 
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Your Deque approach  is clever but not efficient. Handling 'L' and 'R' should only involve decrementing or incrementing a cursor and not the moving of characters. I think you'd find manipulating a single ArrayList would be better but I haven't tested it on the website.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Prahlad Yeri wrote:. . . I've updated the post and added a brief description of the problem statement.

Kindly don't. It makes it difficult to know what was asked originally
 
Saloon Keeper
Posts: 15608
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Agree with Carey.

It's MUCH simpler to solve this issue using a ListIterator.

Deque or Stack are not the right data structures for this problem.
 
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
To be fair, I think the two ArrayDeques are more efficient than Carey's suggestion of using an ArrayList (with or without ListIterator), if you're adding characters anywhere besides the end of the string.  But a LinkedList traversed with a ListIterator will be more efficient, and more importantly, simpler to code correctly.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

Prahlad Yeri wrote:

Campbell Ritchie wrote:Why are you using a for loop rather than for‑each?



Is the speed difference between the two that big? I'm trying to optimize for performance here.


The speed difference is negligible.  The for-each is just simpler to use and read, and harder to accidentally insert an error into -- thus it's usually preferable unless you have a specific need for something else.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:Agree with Carey.

It's MUCH simpler to solve this issue using a ListIterator.

Deque or Stack are not the right data structures for this problem.


Ah, forgot totally about ListIterator. Works beautifully.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Congratulations on solving it! This is what I came up with using ListIterator, just FYI.
 
Prahlad Yeri
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:

Stephan van Hulst wrote:Agree with Carey.

It's MUCH simpler to solve this issue using a ListIterator.

Deque or Stack are not the right data structures for this problem.


Ah, forgot totally about ListIterator. Works beautifully.



Thanks to everyone on this thread. Turns out the issue wasn't with the algorithm at all but rather the way console input was taken and output was printed.

1. Scanner was apparently inefficient for large inputs, so I replaced with BufferedReader.
2. Instead of printing each character one by one for output, I utilized a StringBuilder object to make it efficient.

With these changes, my solution was accepted! Here is the final code:

 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Congratulations on solving it! I had overlooked the IO performance issue with your code. Good catch. Two posts up is what I came up with using ListIterator.
 
Stephan van Hulst
Saloon Keeper
Posts: 15608
366
  • Number of slices to send:
    Optional 'thank-you' note:

Prahlad Yeri wrote:Scanner was apparently inefficient for large inputs, so I replaced with BufferedReader.


Wait, who told you that? Please forget it right away. Scanner uses an internal buffer and in theory there shouldn't be a large difference in performance when comparing it to BufferedReader.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Sorry for delay in replying.
A Scanner is usually slower than a Buffered Reader, but that probably has nothing to do with buffers. It is because the Scanner does more with the input. A Buffered Reader returns each line of the input unchanged. It may then be necessary to do more processing to convert the input to an int or some other form of data. A Scanner does that automatically, saving you lots of trouble parsing the input, but that takes time. You will probably find that parsing the lines read by a Buffered Reader will slow your application to the same speed as a Scanner. And there is a risk on introducing errors by writing your own parsing code.
In this case, where you want the individual letters, you are using the whole line, and a Scanner doesn't have a method for that particular manipulation. In which case a Buffered Reader will give you slightly better performance, but not because of the buffer size.
Since both a Buffered Reader and a Scanner often read not more than one line at a time, there is unlikely to be any problem with buffering, as Stephan said yesterday.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
It all depends on which methods you use, and what the data is like.  A method like Scanner.nextLine() (used in the solution above) is usually pretty innocuous - but it reads everything up to the next line separator, or EOF.  What if the input doesn't have any newlines, just a really long stream of characters?  Then you're forcing the Scanner to load the whole thing into memory at once.  Whereas with BufferedReader you can break the input up into reasonably-sized chunks.  It may be that the instructor / tester / whatever is using a test framework that intentionally creates a really long input.  They could also run the JVM with a smaller-than-usual heap size, in order to force memory efficiency.  

I think the use of StringBuilder here is still problematic, because just like with ArrayList, there is a  performance issue if you are inserting characters in the front or middle of the StringBuilder.  Inserting at the end is fast, but anywhere else, you're also shifting the contents of everything after your insert.  That's why I advocate using LinkedList.

Still, it seems this issue wasn't big enough to prevent you from a successful submission, so congratulations!
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:I think the use of StringBuilder here is still problematic...


StringBuilder is only using append()s so not a problem.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
You're right, I didn't look at the actual usage - I was just reacting to the mention of StringBuilder, and how I imagined using it.    Good catch.
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |