Post History
AWK, 145 bytes Takes input as space-delimited list of integers. Outputs space-delimited list of integers. function n(v){L=v;while(R++<NF&&$R==$(R-L));z[i]=R-- -L-1}{z[i=1]=NF while(i...
#2: Post edited
- # [AWK], 145 bytes
Takes input as (lines of) space-delimited list of integers.- Outputs space-delimited list of integers.
- function n(v){L=v;while(R++<NF&&$R==$(R-L));z[i]=R-- -L-1}{z[i=1]=NF
- while(i++<NF)i>R?n(R=i-1):z[k=i-L]>R-i?n(i-1):z[i]=z[k]
- for(e in z)$e=z[e]}1
- $O(n)$ algorithm taken from https://codeforces.com/blog/entry/3107, adjusted for AWK's 1-based field indexing and with duplicate code moved into a function.
- ```
- function n(v){
- L = v;
- while ( R++<NF && $R==$(R-L) )
- ;
- z[i] = R-- -L-1
- }
- {
- z[ i=1 ] = NF
- while ( i++<NF )
- i>R \
- ? n( R=i-1 ) \
- : z[ k=i-L ]>R-i \
- ? n( i-1 ) \
- : z[i]=z[k];
- for(e in z)
- $e = z[e]
- }
- 1
- ```
- [Try it online!][TIO-mnxmcfdk]
- [AWK]: https://www.gnu.org/software/gawk/manual/gawk.html
- [TIO-mnxmcfdk]: https://tio.run/##ZVDLbsIwELznK1ZRipJil9iBSpQ6vXGKevA1uFUfobVAoeIpBfHtdHHsJIhYtuKZ3ZlZfxwW56MH@P2tdbmdg3@3AZGCT4LYwJvdZziYqYf7AXmPWuRtljfAj0HIgPjg10gGAiTu2Dud57vya6tXJZThPjpmYj85/OplEcp@//l12usFUogglDSLokmVayUkpUAzyk5HvAqmxOvUq1u0aYl0Kl/KUApNWfRU5Qv8yVQqqUbUYiiDhPLmq3VYgC6hioICoUKdmJ33u1gW2wIqjJgzwklChmSkgKaQj0hcL@UhZZejhljJ8eooTrijEG66uKFYK5jgZpYyZNeLd7y4c2ylWWy1LyFbl0voui4xoRLnx2z@i661dqnGzh530tHvdrCrejdLJzRra3mTHzz7jjenbapLx@Z8vAnGm@H59ePZPmashm1I9Q8 "AWK – Try It Online"
- # [AWK], 145 bytes
- Takes input as space-delimited list of integers.
- Outputs space-delimited list of integers.
- function n(v){L=v;while(R++<NF&&$R==$(R-L));z[i]=R-- -L-1}{z[i=1]=NF
- while(i++<NF)i>R?n(R=i-1):z[k=i-L]>R-i?n(i-1):z[i]=z[k]
- for(e in z)$e=z[e]}1
- $O(n)$ algorithm taken from https://codeforces.com/blog/entry/3107, adjusted for AWK's 1-based field indexing and with duplicate code moved into a function.
- ```
- function n(v){
- L = v;
- while ( R++<NF && $R==$(R-L) )
- ;
- z[i] = R-- -L-1
- }
- {
- z[ i=1 ] = NF
- while ( i++<NF )
- i>R \
- ? n( R=i-1 ) \
- : z[ k=i-L ]>R-i \
- ? n( i-1 ) \
- : z[i]=z[k];
- for(e in z)
- $e = z[e]
- }
- 1
- ```
- [Try it online!][TIO-mnxmcfdk]
- [AWK]: https://www.gnu.org/software/gawk/manual/gawk.html
- [TIO-mnxmcfdk]: https://tio.run/##ZVDLbsIwELznK1ZRipJil9iBSpQ6vXGKevA1uFUfobVAoeIpBfHtdHHsJIhYtuKZ3ZlZfxwW56MH@P2tdbmdg3@3AZGCT4LYwJvdZziYqYf7AXmPWuRtljfAj0HIgPjg10gGAiTu2Dud57vya6tXJZThPjpmYj85/OplEcp@//l12usFUogglDSLokmVayUkpUAzyk5HvAqmxOvUq1u0aYl0Kl/KUApNWfRU5Qv8yVQqqUbUYiiDhPLmq3VYgC6hioICoUKdmJ33u1gW2wIqjJgzwklChmSkgKaQj0hcL@UhZZejhljJ8eooTrijEG66uKFYK5jgZpYyZNeLd7y4c2ylWWy1LyFbl0voui4xoRLnx2z@i661dqnGzh530tHvdrCrejdLJzRra3mTHzz7jjenbapLx@Z8vAnGm@H59ePZPmashm1I9Q8 "AWK – Try It Online"
#1: Initial revision
# [AWK], 145 bytes
Takes input as (lines of) space-delimited list of integers.
Outputs space-delimited list of integers.
function n(v){L=v;while(R++<NF&&$R==$(R-L));z[i]=R-- -L-1}{z[i=1]=NF
while(i++<NF)i>R?n(R=i-1):z[k=i-L]>R-i?n(i-1):z[i]=z[k]
for(e in z)$e=z[e]}1
$O(n)$ algorithm taken from https://codeforces.com/blog/entry/3107, adjusted for AWK's 1-based field indexing and with duplicate code moved into a function.
```
function n(v){
L = v;
while ( R++<NF && $R==$(R-L) )
;
z[i] = R-- -L-1
}
{
z[ i=1 ] = NF
while ( i++<NF )
i>R \
? n( R=i-1 ) \
: z[ k=i-L ]>R-i \
? n( i-1 ) \
: z[i]=z[k];
for(e in z)
$e = z[e]
}
1
```
[Try it online!][TIO-mnxmcfdk]
[AWK]: https://www.gnu.org/software/gawk/manual/gawk.html
[TIO-mnxmcfdk]: https://tio.run/##ZVDLbsIwELznK1ZRipJil9iBSpQ6vXGKevA1uFUfobVAoeIpBfHtdHHsJIhYtuKZ3ZlZfxwW56MH@P2tdbmdg3@3AZGCT4LYwJvdZziYqYf7AXmPWuRtljfAj0HIgPjg10gGAiTu2Dud57vya6tXJZThPjpmYj85/OplEcp@//l12usFUogglDSLokmVayUkpUAzyk5HvAqmxOvUq1u0aYl0Kl/KUApNWfRU5Qv8yVQqqUbUYiiDhPLmq3VYgC6hioICoUKdmJ33u1gW2wIqjJgzwklChmSkgKaQj0hcL@UhZZejhljJ8eooTrijEG66uKFYK5jgZpYyZNeLd7y4c2ylWWy1LyFbl0voui4xoRLnx2z@i661dqnGzh530tHvdrCrejdLJzRra3mTHzz7jjenbapLx@Z8vAnGm@H59ePZPmashm1I9Q8 "AWK – Try It Online"
