Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cartalk1, why using i=i+1-2*count? #78

Open
Naolador opened this issue Nov 30, 2020 · 1 comment
Open

cartalk1, why using i=i+1-2*count? #78

Naolador opened this issue Nov 30, 2020 · 1 comment

Comments

@Naolador
Copy link

I just don't understand why we use "i = i + 1 - 2*count" to reset the pointer in line 31 if two nearby characters doesn't match. I replaced the line with "i = i + 1" and got the same result.

Repository owner deleted a comment from DanilGeryng Apr 18, 2021
@jerryu
Copy link

jerryu commented Jul 2, 2021

I am also curious, so I added some debugging code to output

  • the partial string yet to check at the beginning of the iteration.
  • the values of i and count, at beginning and the end of each iteration

The i=i+1 proposed by Naolador seems to have the desirable/expected flow.

Allowing overlapped counting might require walking the pointer back, depending on the flow.
To illustrate, I made up a word, 'booookmaker'.
Both solutions don't allow overlapping, whereas the second 'o' forms one double with 'o' before and another double with 'o' after.

 reset	booookmaker	0	0	1	0		 reset	booookmaker	0	0	1	0
 double	 ooookmaker	1	0	3	1		 double	 ooookmaker	1	0	3	1
 double	   ookmaker	3	1	5	2		 double	   ookmaker	3	1	5	2
 reset	     kmaker	5	2	2	0	      |	 reset	     kmaker	5	2	6	0
 double	  oookmaker	2	0	4	1	      <
 reset	    okmaker	4	1	3	0	      <
 double	   ookmaker	3	0	5	1	      <
 reset	     kmaker	5	1	4	0	      <
 reset	    okmaker	4	0	5	0	      <
 reset	     kmaker	5	0	6	0	      <
 reset	      maker	6	0	7	0		 reset	      maker	6	0	7	0
 reset	       aker	7	0	8	0		 reset	       aker	7	0	8	0
 reset	        ker	8	0	9	0		 reset	        ker	8	0	9	0
 reset	         er	9	0	10	0		 reset	         er	9	0	10	0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants