Skip to content

Instantly share code, notes, and snippets.

@sng2c
Last active March 6, 2022 10:33
Show Gist options
  • Save sng2c/6077247 to your computer and use it in GitHub Desktop.
Save sng2c/6077247 to your computer and use it in GitHub Desktop.
detect_repetition.md

리스트에 기호들이 들어 있을 때, 연속된 기호가 일정한 거리를 두고 다시 나타나는 것을 반복이라고 하기로 하구요. 그 연속되는 기호의 단위 묶음을 반복셋이라고 하겠습니다. 같은 위치에서 시작하는 반복셋은 여러개가 있을 수 있습니다.

A B C A B C 의 경우에는

A _ _ A _ _  : offset 2
A B _ A B _  : offset 1
A B C A B C  : offset 0

위와 같이 해서 [$offset,@symbols] 의 형태로 나타내면,

[2,A], [1,A,B], [0,A,B,C] 가 첫번째 A 부터 반복할 수 있는 반복셋들이 되겠습니다.

A A A A 의 경우에는

A _ _ _ : offset 0
A A _ _ : offset 0

[0, A], [0, A, A] 이렇게 두 개가 됩니다. offset은 0보다 작을 수 없습니다.

A A _ _ 후에는 _ _ A A 가 매칭 해야하고, _ A A _ 가 매칭될 수는 없습니다. 이 경우에 offset이 -1이 됩니다.

"0번째 기호"로 시작하는 모든 반복셋을 얻고 싶습니다.

입력

A
A A
A A A A
A B A B A B A B
A B C A B C A C
A A B A A C

출력 - ( [$offset, @symbols], [...], ... ) 의 형식

( )
( [ 0, A ] )
( [ 0, A ], [ 1, A ], [ 2, A ], [ 0, A,A ] )
( [ 1, A ], [ 3, A ], [ 5, A ], [ 0, A,B ], [ 2, A,B ], [ 4, A,B ], [ 1, A,B,A ], [ 0, A,B,A,B ] )
( [ 2, A ], [ 5, A ], [ 1, A,B ], [ 0, A,B,C ] )
( [ 0, A ], [ 2, A ], [ 3, A ], [ 1, A,A ] )

도와주세요 ㅠㅠ

@sng2c
Copy link
Author

sng2c commented Jul 25, 2013

(updated) aero 님의 정답소스 : http://codepad.org/orOvN8Gi
LuzLuna 님의 정답 소스 : http://codepad.org/7zmmRoTl
감사합니다!!

@aero
Copy link

aero commented Jul 25, 2013

다시 보니 제 코드의 내부루프에서
foreach my $search ($set_length .. $len) { 은
foreach my $search ($set_length .. $len-$set_length) { 로 바꿔야 뒤에 보나 마나 한 부분을 skip 해서 약간이나마 빠를듯 하네요

@asonhador
Copy link

제출합니다~.. http://codepad.org/5mLgmSF4

@sng2c
Copy link
Author

sng2c commented Sep 8, 2015

@asonhador
@aero
@luzluna

https://github.com/sng2c/clj-template-reverse/blob/master/test/template_reverse/core_test.clj#L31
구현완료요~ 도움주셔서 감사합니다. 2년 걸렸네요 ㅋㅋ

@thyeem
Copy link

thyeem commented Mar 6, 2022

(헉, 수정하다가 댓글을 지워버렸네요;)

저도 동참해봅니다. 7년전의 글에 답글을!

https://gist.github.com/thyeem/06daf6bbc080d9c970e6b0870479b588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment