파워 쉘, 134 바이트
param($m,$c)$c-csplit"([$m])"|%{$i+=$o=$_-ceq$m[+$i]
if($o-or$_-cne"`0$h"[-1]){$h+=($_-replace'(.)(?=\1)')*($o+1)}}
$h*!($i-$m.Length)
스크립트는 empty string
캐리어에 메시지 문자가 올바른 순서로 포함되어 있지 않은 경우를 .
덜 골프 테스트 스크립트 :
$f = {
param($message,$carrier)
$carrier-csplit"([$message])"|%{ # split by chars of the message, chars itself included ([])
$offset=$_-ceq$message[+$i] # 0 or 1 if current substring is a current message char (case-sensitive equality)
$i+=$offset # move to next message char if need it
if($offset-or$_-cne"`0$h"[-1]){ # condition to remove redundant doubles after message char: arrrived -> arrived, ooo -> oo, etc
# `0 to avoid exception error if $h is empty
$h+=($_-replace'(.)(?=\1)')*($offset+1) # accumulate a double message char or a single substring without inner doubles: arried -> arived, anna -> ana, etc
}
}
$h*!($i-$message.Length) # repeat 0 or 1 times to return '' if the carrier does not contain the message characters in the right order
}
@(
,('hi' ,'has it arrived?' ,'hhas iit arived?', 'hhas it ariived?')
,('hi?' ,'has it arrived?' ,'hhas iit arived??', 'hhas it ariived??')
,('sir' ,'has it arrived?' ,'hass iit arrived?')
,('foo' ,'has it arrived?' ,'')
,('Car' ,'Cats are cool.' ,'CCaats arre col.')
,('car' ,'Cats are cool.' ,'')
,('Couch' ,'Couch' ,'CCoouucchh')
,('oo' ,'oooooooooo' ,'oooo')
,('o o' ,'oooo oooa' ,'oo ooa')
,('er' ,'error' ,'eerorr', 'eerror')
,('a+b' ,'anna+bob' ,'aana++bbob')
) | % {
$message,$carrier,$expected = $_
$result = &$f $message $carrier
"$($result-in$expected): $result"
}
산출:
True: hhas iit arived?
True: hhas iit arived??
True: hass iit arrived?
True:
True: CCaats arre col.
True:
True: CCoouucchh
True: oooo
True: oo ooa
True: eerror
True: aana++bbob