๋์! ๋ด ๋ฐฐ์ด ์ค ์ผ๋ถ์ ์ฑ๊ฐ์ ์์ฝ๊ฐ์๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋ฉฐ ๊ทธ๊ฒ์ ์์ ๊ณ ์ถ์ต๋๋ค. ์ด ๊ฒฝ์ฐ ์๋ ๋ฐฐ์ด์ด ๊ฐ์ด๋ฐ ์ด๋๊ฐ์ ๋ฐ๋ณต๋์ด ๊ฐ์ด ์๋ก ์ถ๊ฐ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด, ๋ฐฐ์ด [ 422, 375, 527, 375, 859, 451, 754, 451 ]
์๋ ๋ค์๊ณผ ๊ฐ์ ์์ฒด ์์ฝ๊ฐ ํฌํจ๋ฉ๋๋ค.
[ 422, 375, 527, 375, 859, 451, 754, 451 ] <-- array with echo (input)
[ 422, 375, 105, 0, 754, 451 ] <-- original array (output)
[ 422, 375, 105, 0, 754, 451 ] <-- echo of original array
์ 2 :
[ 321, 526, 1072, 899, 6563, 798, 7038, 3302, 3032, 3478, 1806, 601 ] <-- input
[ 321, 526, 751, 373, 5812, 425, 1226, 2877, 1806, 601 ] <-- output
[ 321, 526, 751, 373, 5812, 425, 1226, 2877, 1806, 601 ]
๋ฐฐ์ด์ ์์ฝ๊ฐ ์์ ์๋ ์์ผ๋ฉฐ,์ด ๊ฒฝ์ฐ ์๋ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค.
์ 3 :
[ 623, 533, 494, 382 ] <-- input
[ 623, 533, 494, 382 ] <-- output
๋์ :
๋ฐํฅ์ ํฌํจ ํ ์์๋ ๋ฐฐ์ด์ด ์์ผ๋ฉด์ด๋ฅผ ์ ๊ฑฐํ๊ณ ๋ฐํฅ์์ด ๋ฐฐ์ด์ ๋ฆฌํดํ์ญ์์ค.
์ ๋ ฅ:
- ๋ฐฐ์ด์์ ๊ตฌ๋ถ ๋ ๋ฌธ์์ด, ํ์น ์นด๋ ๋๋ ํ๋ซํผ์ ์ ํฉํ ๋ฒ์์์ ์ธ ๊ฐ ์ด์์ ์ ์๋ฅผ ํฌํจํ๋ ๋๋ฑํ,
0โคn<10000 ์ ์ด๋ ํ๋ ๊ฐ์ ์์์ >0 . - ์์ฝ๋ ์ฒซ ๋ฒ์งธ ์์ ๋ ๋ง์ง๋ง ์์ ๋ค์์ ์์ํ ์ ์์ต๋๋ค.
- ์์ฝ๋ ์ ๋ ฅ ๋ด์์ ํ ๋ฒ๋ง ๋ฐ์ํ๊ฑฐ๋ ์ ํ ๋ฐ์ํ์ง ์์ต๋๋ค.
์ฐ์ถ:
- ์์ฝ๊ฐ ์ ๊ฑฐ ๋ ์ ์
0โคn<10000 ์ ๋ฐฐ์ด, ๋ชฉ๋ก ๋ฑ - ์์ฝ๊ฐ ์์ผ๋ฉด ์๋ ๋ฐฐ์ด์ ๋ฐํํ์ญ์์ค.
๊ท์น๊ณผ ๋์ :
- ์ด๊ฒ์ code-golf ์ด๋ฏ๋ก ๊ฐ ์ธ์ด์ ๋ํ ๋ฐ์ดํธ ๋จ์์ ์ต๋จ ๋ต๋ณ์ด ์น๋ฆฌํฉ๋๋ค.
- ํ์ค ๊ท์น ๋ฐ ๊ธฐ๋ณธ I / O ๊ท์น์ด ์ ์ฉ๋ฉ๋๋ค.
- ํ์ ์ ๋ฌผ๋ก ๊ธ์ง๋์ด ์์ต๋๋ค.
- ์ฝ๋ ํ ์คํธ ( TIO.run ๋ฑ) ์ ํจ๊ป ๋งํฌ๋ฅผ ์ ๊ณตํ์ญ์์ค .
- ๋ต์ ๋ช ํํ๊ฒ ์ค๋ช ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
ํ ์คํธ ์ฌ๋ก :
์์ฝ :
[ 422, 375, 527, 375, 859, 451, 754, 451 ]
[ 422, 375, 105, 0, 754, 451 ]
[ 321, 526, 1072, 899, 6563, 798, 7038, 3302, 3032, 3478, 1806, 601 ]
[ 321, 526, 751, 373, 5812, 425, 1226, 2877, 1806, 601 ]
[ 4330, 3748, 363, 135, 2758, 3299, 1674, 1336, 4834, 2486, 4087, 1099, 4098, 4942, 2159, 460, 4400, 4106, 1216, 3257, 1638, 2848, 3616, 3554, 1605, 490, 1308, 2773, 3322, 3284, 4037, 7109, 4171, 5349, 2675, 3056, 4702, 4229, 1726, 5423, 6039, 8076, 6047, 7088, 9437, 4894, 1946, 7501, 5331, 3625, 5810, 6289, 2858, 6610, 4063, 5565, 2200, 3493, 4573, 4906, 3585, 4147, 3748, 3488, 5625, 6173, 3842, 5671, 2555, 390, 589, 3553, 3989, 4948, 2990, 4495, 2735, 1486, 3101, 1225, 2409, 2553, 4651, 10, 2994, 509, 3960, 1710, 2185, 1800, 1584, 301, 110, 969, 3065, 639, 3633, 3544, 4268 ]
[ 4330, 3748, 363, 135, 2758, 3299, 1674, 1336, 4834, 2486, 4087, 1099, 4098, 4942, 2159, 460, 4400, 4106, 1216, 3257, 1638, 2848, 3616, 3554, 1605, 490, 1308, 2773, 3322, 3284, 4037, 2779, 423, 4986, 2540, 298, 1403, 2555, 390, 589, 3553, 3989, 4948, 2990, 4495, 2735, 1486, 3101, 1225, 2409, 2553, 4651, 10, 2994, 509, 3960, 1710, 2185, 1800, 1584, 301, 110, 969, 3065, 639, 3633, 3544, 4268 ]
[ 24, 12, 52, 125, 154, 3, 567, 198, 49, 382, 53, 911, 166, 18, 635, 213, 113, 718, 56, 811, 67, 94, 80, 241, 343, 548, 68, 481, 96, 79, 12, 226, 255, 200, 13, 456, 41 ]
[ 24, 12, 52, 125, 154, 3, 567, 198, 25, 370, 1, 786, 12, 15, 68, 15, 88, 348, 55, 25, 55, 79, 12, 226, 255, 200, 13, 456, 41 ]
[ 1, 3, 2 ]
[ 1, 2 ]
[ 0, 1, 3, 2, 0 ]
[ 0, 1, 2, 0 ]
์์ฝ์์ด :
[ 623, 533, 494, 382 ]
[ 623, 533, 494, 382 ]
[ 1141, 1198, 3106, 538, 3442, 4597, 4380, 3653, 1370, 3987, 1964, 4615, 1844, 5035, 2463, 6345, 4964, 4111, 5192, 8555, 5331, 3331, 4875, 6586, 5728, 4532, 5972, 2305, 3491, 6317, 2256, 2415, 5788, 4873, 6480, 2080, 5319, 4551, 6527, 5267, 4315, 2178, 2615, 5735, 5950, 6220, 7114, 6259, 5000, 4183, 6822, 6927, 7150, 8003, 5603, 3154, 8231, 5005, 5743, 6779, 4530, 4029, 5336, 6105, 4777, 6183, 6838, 5725, 6819, 8584, 3142, 3840, 3291, 4284, 2933, 4859, 2906, 5176, 2853, 2110, 2048, 4389, 4501, 2267, 2704, 431, 1495, 2712, 3008, 187, 3487, 630 ]
[ 1141, 1198, 3106, 538, 3442, 4597, 4380, 3653, 1370, 3987, 1964, 4615, 1844, 5035, 2463, 6345, 4964, 4111, 5192, 8555, 5331, 3331, 4875, 6586, 5728, 4532, 5972, 2305, 3491, 6317, 2256, 2415, 5788, 4873, 6480, 2080, 5319, 4551, 6527, 5267, 4315, 2178, 2615, 5735, 5950, 6220, 7114, 6259, 5000, 4183, 6822, 6927, 7150, 8003, 5603, 3154, 8231, 5005, 5743, 6779, 4530, 4029, 5336, 6105, 4777, 6183, 6838, 5725, 6819, 8584, 3142, 3840, 3291, 4284, 2933, 4859, 2906, 5176, 2853, 2110, 2048, 4389, 4501, 2267, 2704, 431, 1495, 2712, 3008, 187, 3487, 630 ]
[ 4791, 1647, 480, 3994, 1507, 99, 61, 3245, 2932, 8358, 6618, 1083, 5391, 3498, 4865, 1441, 3729, 5322, 5371, 6271, 2392, 1649, 5553, 9126, 3945, 2179, 3672, 2201, 4433, 5473, 4924, 6585, 6407, 3862, 6505, 1530, 5293, 4792, 6419, 6739, 3258, 3839, 3891, 7599, 2576, 5969, 5659, 6077, 5189, 1325, 4490, 5694, 6567, 6367, 5724, 5756, 6450, 5863, 4360, 2697, 3100, 3779, 4040, 4653, 1755, 3109, 2741, 3269 ]
[ 4791, 1647, 480, 3994, 1507, 99, 61, 3245, 2932, 8358, 6618, 1083, 5391, 3498, 4865, 1441, 3729, 5322, 5371, 6271, 2392, 1649, 5553, 9126, 3945, 2179, 3672, 2201, 4433, 5473, 4924, 6585, 6407, 3862, 6505, 1530, 5293, 4792, 6419, 6739, 3258, 3839, 3891, 7599, 2576, 5969, 5659, 6077, 5189, 1325, 4490, 5694, 6567, 6367, 5724, 5756, 6450, 5863, 4360, 2697, 3100, 3779, 4040, 4653, 1755, 3109, 2741, 3269 ]
[ 235, 121, 52, 1249, 154, 26, 5672, 1975, 482, 3817, 532, 9104, 1661, 171, 6347, 2124, 1122, 7175, 558, 8101, 667, 934, 798, 2404, 3424, 5479, 672, 4808, 956, 789, 123, 2255, 2549, 200, 126, 4562, 41 ]
[ 235, 121, 52, 1249, 154, 26, 5672, 1975, 482, 3817, 532, 9104, 1661, 171, 6347, 2124, 1122, 7175, 558, 8101, 667, 934, 798, 2404, 3424, 5479, 672, 4808, 956, 789, 123, 2255, 2549, 200, 126, 4562, 41 ]
[ 1, 1, 1, 1, 1 ]
[ 1, 1, 1, 1, 1 ]
๋ต๋ณ
MATL , 16 ๋ฐ์ดํธ
t"GX@WQB&Y-~?w]x
์จ๋ผ์ธ์ผ๋ก ์ฌ์ฉํด๋ณด์ญ์์ค! ๋๋ ๋ชจ๋ ํ ์คํธ ์ฌ๋ก๋ฅผ ํ์ธํ์ญ์์ค .
์ค๋ช
์น๋ฆฌ๋ฅผ์ํ ๋คํญ์ ๋ถํ !
t % Implicit input. Duplicate
" % For each (do the following as many times as input length)
G % Push input again. This will be the output if no solution exists
X@ % Push current iteration index, k
WQB % 2 raised to that, add 1, convert to binary. Gives [1 0 ... 0 1] (k-1 zeros)
&Y- % Two-output polynomial division (deconvolution). Gives quotient and remainder
~ % Logical negate: transforms zeros into 1, nonzeros into 0
? % If all values are nonzero (i.e. if remainder was all zeros): solution found
w % Swap. This moves the copy of the input to top (to be deleted)
] % End
x % Delete. This deletes the quotient if it was not a solution, or else deletes
% the copy of the input
% End (implicit). Since it is guaranteed that at most one solution exists, at this
% point the stack contains either the solution or the input
% Implicit display
๋ต๋ณ
ํ์ค์ผ , 167 ๋ฐ์ดํธ
๋จผ์ ๋ฐํฅ์ด ์์ผ๋ฉด ์
๋ ฅ ๋ฐฐ์ด์ด ํ์์ ์ผ๋ถ ๋ฐฐ์ด๊ณผ ๋ค๋ฅธ ๋ฐฐ์ด์ ์ปจ๋ณผ ๋ฃจ์
์ด๋ผ๋ ์ ์ ์ ์ํด์ผํฉ๋๋ค [1,1],[1,0,1],[1,0,0,1],...
.
์ฆ,์ด ๋ชจ๋ ๋ฐฐ์ด์ ๋ํด ์ด๊ฒ์ ํ์ธํ๋ฉด๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด์ฐ ์ปจ๋ณผ ๋ฃจ์ / ๋์ปจ ๋ณผ ๋ฃจ์ ์ ๋คํญ์ ๊ณฑ์ / ๊ธด ๋๋์ ๊ณผ ๋์ผํ๋ฏ๋ก ๊ฐ๋ฅํ ๊ฒฝ์ฐ ํญ์ ๋ชซ์ ๋ฐํํ๋ ๋คํญ์์ ์ฌ์ฉํ ๊ตฌํ์ ๋๋ค.
[1]
๋ค๋ฅธ ๋ฐฐ์ด์ด ์๋ํ์ง ์์ผ๋ฉด deconvolution [1]
์ด ์๋ํ๊ณ ์๋์ ๋คํญ์์ ๋ฐํ ํ๊ธฐ ๋๋ฌธ์ ์์ ๋ฐฐ์ด ์ธ์๋ ์ ์ฒด ๋ฐฐ์ด์ ์ฝ๊ฐ ๋จ์ถ ํ ํธ๋ฆญ ์ ๊ธฐ๋ณธ ์ฌ๋ก๋ก ํ์ธ ํ๋ ๊ฒ์
๋๋ค.
import Math.Polynomial
import Data.Ratio
p=poly LE
c z=last[polyCoeffs LE q|k<-zipWith const[p(take k(1:repeat 0)++[1])|k<-[0..]]z,(q,r)<-[quotRemPoly(p z)k],r==zero]
๋ต๋ณ
์๋ฐ ์คํฌ๋ฆฝํธ , 211 171 145 ๋ฐ์ดํธ
s=>{for(n=x=0,y=s.length;++x<y/2&!n;)for(n=s.slice(i=0,x);i+x<y-x&&n;)n=(n[i+x]=s[i+x]-n[i++])<0?0:n;return n&&n.slice(1-x)+''==s.slice(1-x)?n:s}
์จ๋ผ์ธ์ผ๋ก ์ฌ์ฉํด๋ณด์ญ์์ค
Kevin Cruijssen ์์ 40 ๋ฐ์ดํธ
Arnauld ์์ ๋ ๋ค๋ฅธ 26 ๋ฐ์ดํธ
์ฒซ ๋ฒ์งธ ์ฝ๋ ๊ณจํ ์๋ต์ ์ ์ฌ์ ์คํ์ ์ ๋ฌดํจํํ๊ณ ์ฐพ์ ๋ด์ฉ์ ๋ฐ๋ผ ์๋ ๋ฐฐ์ด ๋๋ ์ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. ๋๊ตฐ๊ฐ๊ฐ ๊ทธ๊ฒ์ ์งง๊ฒ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์๊ณ ์๋ค๋ฉด ์๋ ค์ฃผ์ธ์. ์ฌ๋ฏธ์๋ ๊ฒ์์ฒ๋ผ ๋ณด์ ๋๋ค.
๋ต๋ณ
ํ์ค์ผ, 112 (111) 110 ๋ฐ์ดํธ
l=length
(!)=splitAt
f a=last$a:[x|i<-[1..l a],let (h,t)=i!a;o=h++zipWith(-)t o;(x,y)=l t!o,all(>=0)o,sum y<1]
์จ๋ผ์ธ์ผ๋ก ์ฌ์ฉํด๋ณด์ญ์์ค!
f a=
i<-[1..l a] -- for all indices 'i' of the input array 'a'
(h,t)=i!a -- split 'a' at 'i' into 'h' and 't'
-- e.g. a: [1,2,3,4], i: 1 -> h: [1], t:[2,3,4]
o= -- calculate the original array by
h++ -- prepended 'h' to
zipWith(-)t o -- the (element-wise) difference of
-- 't' and itself
(x,y)=l t!o -- split 'o' at position <length of t>
--
-- example:
-- a: [0,1,3,2,0]
-- h: [0]
-- t: [1,3,2,0]
-- now
-- o: [0,1,2,0,0]
-- x: [0,1,2,0]
-- y: [0]
--
, -- 'o' is valid, if
all(>=0)o -- all elements of 'o' are not negative
,sum y<1 -- and 'y' is all zeros
[x| ] -- keep 'x' (a valid echo array) if 'o' is valid
last $ a :[ ] -- if there's no echo, the list of 'x's is empty
-- and 'a' is picked, else the last of the 'x's
๋ต๋ณ
๋ณผํ๋ ์ธ์ด (ํฐ์นด) , 131 (129) 120 (119) 102 98 97 96 95 ๋ฐ์ดํธ
(w=#;Do[(w=v/.#)&/@Thread[#==PadLeft[v=Array[x,L-d],L]+v~PadRight~L]~Solve~v,{d,L=Tr[1^#]}];w)&
์จ๋ผ์ธ์ผ๋ก ์ฌ์ฉํด๋ณด์ญ์์ค!
attinat ๋๋ถ์ โ1 ๋ฐ์ดํธ : ์ธ์๊ฐ ์ซ์ ๋ชฉ๋ก ์ผ ๋ L=Tr[1^#]
๋์ ์ธ ์ ์์ต๋๋ค L=Length@#
.
์ฝ๋ ์ค๋ช
: ์์ถ์ ๋ฐ๋ณตํฉ๋๋ค d
(์
๋ ฅ ๊ธธ์ด์ ์ถ๋ ฅ ๊ธธ์ด์ ์ฐจ์ด). ๊ฐ ์ถ๋ ฅ ๋ชฉ๋ก ๊ธธ์ด์ ๋ํด ์ ์์๋ ๋ชฉ๋ก์ ๊ตฌ์ฑํ๊ณ v={x[1],x[2],...,x[L-d]}
์ผ์ชฝ์ ์ฑ์์ง ๊ธธ์ด์ ์ค๋ฅธ์ชฝ์ ์ฑ์์ง ๊ธธ์ด๋ฅผ ๊ธธ์ด L
( PadLeft[v,L]+PadRight[v,L]
)๋ก ์ถ๊ฐ ํ ๋ค์์ด ํฉ๊ณ๋ฅผ ์
๋ ฅ ๋ชฉ๋ก๊ณผ ๋์ผํ๊ฒ ์ค์ ํ๊ณ ์ ์์๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ญ์์ค x[1]...x[L-d]
. ๊ฐ์ฅ ์งง์ ์๋ฃจ์
์ ์ ํํ์ญ์์ค. ๊ฐ์ฅ ์ต๊ทผ์ ์์ฑ ๋ w
์๋ฃจ์
์ ์๋ฃจ์
์ ์ฐพ์ ๋๋ง๋ค ๋ณ์๋ฅผ ๋ฎ์ด ์ฐ๋ ๊ฒ์
๋๋ค.
๊ณจํ ์ฉ ๋ฒ์ :
F = Function[A, (* A is the input list *)
Module[{L = Length[A], (* length of A *)
v, (* list of unknowns *)
x, (* unknowns in v *)
w = A}, (* variable for solution, defaults to A *)
Do[ (* loop over shrinkage: d = Length[A]-Length[output] *)
v = Array[x, L - d]; (* list of unknowns to be determined *)
(w = v /. #) & /@ (* overwrite w with every... *)
Solve[ (* ...solution of... *)
Thread[PadLeft[v,L]+PadRight[v,L]==A], (* ...v added to itself, left-padded and right-padded, equals A *)
v], (* solve for elements of v *)
{d, L}]; (* loop for shrinkage from 1 to L (the last case d=L is trivial) *)
w]]; (* return the last solution found *)
๋ต๋ณ
์ ค๋ฆฌ , 25 24 ๋ฐ์ดํธ
รฐsแบก\FแธฃL_ยฅ,+ยฅลปโนยก$ยตโฑฎLแนชโผยฅฦศฏ
์จ๋ผ์ธ์ผ๋ก ์ฌ์ฉํด๋ณด์ญ์์ค!
์ ์ ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๊ณ ๋ฆฌํดํ๋ ๋ชจ๋๋ ๋งํฌ์ ๋๋ค. ๊ธฐ์ ์ ์ผ๋ก ์ฑ๊ณต์ ์ธ ๊ฒฐ๊ณผ๋ ๋ ๊ฐ์ ์ถ๊ฐ ๋ชฉ๋ก์ ์ค์ฒฉ๋์ง๋ง ์ ์ฒด ํ๋ก๊ทธ๋จ์ผ๋ก ์คํํ๋ฉด stdout์ ๋ํ ์์ ์ ์ถ๋ ฅ์ด ์ค๋ณต ๋ชฉ๋ก์ ๋ฌด์ํฉ๋๋ค.
๋ต๋ณ
ํ์ด์ฌ (2) , 113 (123) 128 127 123 122 ๋ฐ์ดํธ
def f(a,i=1):
e=a[:i]
for v in a[i:-i]:e+=v-e[-i],
return i<=len(a)/2and(min(e)>=0<e[-i:]==a[-i:]and e or f(a,i+1))or a
์จ๋ผ์ธ์ผ๋ก ์ฌ์ฉํด๋ณด์ญ์์ค!
1 ๋ฐ์ดํธ thx ~ TFeld ; ๊ทธ๋ฆฌ๊ณ Sebastian Kreft์ 1 ๋ฐ์ดํธ thx .
๋ฅผ ํธ์ถ ํ ๋๋ง๋ค f
๊ธธ์ด์ ์ ์ฌ์ ์์ฝ๋ฅผ ๊ตฌ์ฑํฉ๋๋ค len(a)-i
. ์ฒซ ๋ฒ์งธ ๋ถ๋ถ์ i
a ์ ์ฒซ ๋ฒ์งธ ๋ฐ์ดํธ์
๋๋ค. ๋๋จธ์ง๋ โ์์ฝ ํฉ๊ณโ๊ฐ ์์ฝ ํฉ๊ณ์ โ๊ฒน์นโ์น์
์ ๋ํด ์ ํํ๋๋ก ๊ณ์ฐ๋ฉ๋๋ค (์ฆ, ์์ฝ ํฉ๊ณ๊ฐ ์ต๋ ๊ฐ๊น์ง ์ ํํจ a[:-i]
).
๊ทธ๋ฐ ๋ค์ ๊ณจํ๋ฅผ ํ์ง ์์ ๋ฐ๋ก ๊ฐ๊ธฐ ๋น๊ต๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
if i>=len(a)/2+1:
return a # because it can't be that short, so there is no echo
else:
if min(e)>=0 # all elements are non-negative
and e[-i:]==a[-i:]: # and the tails are the same
return e # it's a match!
else:
return f(a,i+1) # recurse