Friday 15 July 2011

Ruby regex to remove all consecutive letters from string -


Here is the problem (codeforces)

Polycarp often thinks about the meaning of life. Continuation, even while typing in the editor. Every time he starts browging, he can not concentrate fully and can press the keys repeatedly, which must be pressed only once. For example, instead of the phrase "how are you" you can type "yes ayyyou".

Polycarp decided to automate the process of fixing such errors. He decided to write a plug-in in the text editor which will remove the same letter joints (if any in the text) . Of course, this does not need absolutely Polycarp, but he has got to start with something!

Write Polycarp help and main plug-in modules. Your program should be removed from the string of all pairs of identical characters, which are consistent. If new pairs are seen after deletion, then the program should remove them as much as possible. Technically, its work should be equivalent to: While the string consistently has a pair of identical characters, the pair must be removed. Note that continuously removing the same letter can be done in any order, because any order results in the same result.

This is my solution ... due to some reason, this case of a huge test fails. I think that can get rid of more letters than this? Is this regular expression wrong?

  str = gets.chomp while str = ~ / (.) \ 1 / str.gsub! (/ (.) \ 1 + /, '') End ART   

Edit - This solution does not work because it can relieve all of the characters continuously from the group. It only gets rid of duplicates if I do it in a way that I think is right, it takes time on very large stars:

  str = gets.chomp while str = ~ / (.) \ 1 / str Gsub!    

in the end (/ (.) \ 1 /, '') Why should it be a regular expression?

  'foobar'.squeeze = & gt; "Fobora" "ho aayere yuyouu". Squeeze = & gt; "How are you"   

are useful tools or specific ones to compress the runes of all characters. Here are some examples:

 "yellow moon". Squid # = & gt; "Yellow Mon" is "Now". Squeeze ("") # = & gt; "Now" are shoot balls of "." Squijes ("M-Z") # = & gt; "Putts shoots balls"  

If "Ab" gets b, then you have questions The example given in the "Hoover" is changed into "how". By your statement it will be "w", and "yoyuyu" will be "." I think you read too much in it And do not understand the problem based on its sample input and sample output.

No comments:

Post a Comment