Submission #6999628


Source Code Expand

Copy
when NimMajor == 0 and NimMinor <= 18: import future
else: import sugar

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import map, mapIt, toSeq
from strutils import split, parseInt, parseFloat, parseBool, parseEnum
when NimMajor == 0 and NimMinor >= 14: from strutils import parseUint

import macros
# headaux macro -- https://stackoverflow.com/questions/31948131/unpack-multiple-variables-from-sequence
# unpack (toTuple) macro  -- https://chy72.hatenablog.com/entry/2017/12/16/214708
macro unpack*(input: array|seq|tuple; count: static[int]): untyped =
  var t = genSym()
  result = quote do: (let `t` = `input`; ())
  when NimMajor == 0 and NimMinor <= 17:
    for i in 0..<count: result[0][1].add quote do: `t`[`i`]
  else:
    for i in 0..<count: result[1].add quote do: `t`[`i`]

template input*(typ: static[typedesc]): seq =
  when typ is char:
    toSeq(stdin.readLine.items)
  elif typ is string:
    stdin.readLine.split
  else:
    stdin.readLine.split.map(`parse typ`)

# 2引数の input() は通常こちらが呼ばれる
template input*(typ: static[typedesc]; count: static[int]): untyped =
  input(typ).unpack(count)

# input(typ, [some integer].Positive) と指定しないとこちらは呼ばれない
template input*(typ: static[typedesc]; lineCount: Positive): untyped =
  (1..lineCount).mapIt(input(typ))

template input*(typ: static[typedesc]; count: static[int] = 1; lineCount: Positive): untyped =
  (1..lineCount).mapIt(input(typ, count))

macro inputs*(body: untyped): untyped =
  result = newNimNode(nnkStmtList)

  var letSection = newNimNode(nnkLetSection)

  var vars = newSeq[NimNode](body.len)
  for i, _ in vars:
    if body[i][0].len > 1: # (var1, var2, ...): int | float | string
      vars[i] = newNimNode(nnkVarTuple)
      for ident in body[i][0]:
        vars[i].add(ident)
      vars[i].add(newEmptyNode())
      vars[i].add(newNimNode(nnkCall)
                    .add(ident("input"))
                    .add(body[i][1][0])
                    .add(newLit(body[i][0].len))
                 )
    elif body[i][1][0].len == 0: # variable: int | float | string
      vars[i] = newIdentDefs(
        body[i][0],
        newEmptyNode(),
        newNimNode(nnkCall)
          .add(ident("input"))
          .add(body[i][1][0])
          .add(newLit(1))
      )
    elif body[i][1][0][0].len > 0: # variable: tuple[field1, field2, ...: T]
      var tupleIdents = newNimNode(nnkIdentDefs)
      for ident in body[i][1][0][0]:
        tupleIdents.add(ident)
      let tuplety = newNimNode(nnkTupleTy).add(tupleIdents)

      vars[i] = newIdentDefs(
        body[i][0],
        tuplety,
        newNimNode(nnkCall)
          .add(ident("input"))
          .add(body[i][1][0][0][^2])
          .add(newLit(body[i][1][0][0].len - 2))
      )
    elif body[i][1][0][0].len == 0: # seq
      if body[i][1][0].len == 2: # variable: seq[int | float | string | char]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][1])
        )
      elif body[i][1][0].len == 3 and body[i][1][0][2].len == 0: # variable: seq[N, int | float | string | char]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2])
            .add(newLit(1))
            .add(body[i][1][0][1])
        )
      elif body[i][1][0][2].len == 2: # variable: seq[lineCount, seq[int | float | string | char]]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2][1])
            .add(newNimNode(nnkDotExpr)
              .add(body[i][1][0][1])
              .add(ident("Positive"))
            )
        )
      else: # variable: seq[lineCount, tuple[field1, field2, ...: T]]
        var tupleIdents = newNimNode(nnkIdentDefs)
        for ident in body[i][1][0][2][0]:
          tupleIdents.add(ident)
        let tuplety = newNimNode(nnkTupleTy).add(tupleIdents)

        vars[i] = newIdentDefs(
          body[i][0],
          newNimNode(nnkBracketExpr).add(body[i][1][0][0]).add(tuplety),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2][0][^2])
            .add(newLit(body[i][1][0][2][0].len - 2))
            .add(body[i][1][0][1])
        )
    else:
      discard

  for `var` in vars:
    letSection.add(`var`)

  result.add(letSection)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc `//=`*(x: var SomeInteger; y: SomeInteger) = x = x div y
proc `%=`*(x: var SomeInteger; y: SomeInteger) = x = x mod y
proc `<?=`*[T](x: var T; y: T) = x = min(x, y)
proc `>?=`*[T](x: var T; y: T) = x = max(x, y)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import concat

template flatten*[T](s: seq[T]): untyped =
  when T is seq: flatten(s.concat)
  else: s
proc flatten*[T](s: seq[T]; recLevel: static[Natural]): auto =
  when recLevel == 0: s
  elif T is seq: flatten(s.concat, recLevel - 1)
  else: s

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc indexed*[T](s: openArray[T]): seq[tuple[i: int, v: T]] =
  (0..s.high).mapIt((it, s[it]))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template countIt*[T](a: openArray[T]; pred: untyped): int =
  var result = 0
  for it {.inject.} in items(a):
    if pred: result.inc
  result

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template stderrEcho*(x: varargs[string, `$`]) =
  for v in x:
    stderr.write(v)
  stderr.writeLine ""

template stderrEcho*[T](x: seq[seq[T]]) =
  for v in x: stderrEcho v

template stderrEcho*(x: seq[string]) =
  for v in x: stderrEcho v

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc lowerBound*[T, K](a: openArray[T]; key: K; cmp: proc (x: T; k: K): int): int =
  if cmp(a[0], key) >= 0: return 0
  if cmp(a[^1], key) < 0: return a.len
  result = a.low
  var step = a.len
  while step > 0:
    step = step div 2
    if cmp(a[min(result + step, a.high)], key) < 0:
      result += step + 1

proc upperBound*[T, K](a: openArray[T]; key: K; cmp: proc (x: T; k: K): int): int =
  a.lowerBound(key, proc (x:T; k: K): int = return if cmp(x, k) <= 0: -1 else: 1)

proc equalRange*[T, K](a: openArray[T]; key: K; cmp: proc (x: T; k: K): int): int =
  a.upperBound(key, cmp) - a.lowerBound(key, cmp)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import sets
from sequtils import applyIt

inputs:
  s: string
  t: string

if (t.toSet - s.toSet).len > 0:
  echo -1
  quit()

var
  T = newSeq[int](t.len)
  positions: array['a'..'z', seq[int]]

for c in 'a'..'z':
  positions[c] = newSeq[int]()

for i, c in s:
  positions[c].add(i)

for i, c in t:
  if i == 0:
    T[i] = positions[c][0]
  else:
    var j = positions[c].upperBound(T[i - 1], system.cmp)
    if j == positions[c].len:
      j = 0
    T[i] = positions[c][j]

T.applyIt(it + 1)
var
  result = T[0]
  prev = T[0]
for i in 1..<T.len:
  if prev >= T[i]:
    result += s.len - prev + T[i]
  else:
    result += T[i] - prev
  prev = T[i]

echo result

Submission Info

Submission Time
Task E - Strings of Impurity
User nimon
Language Nim (0.13.0)
Score 500
Code Size 7570 Byte
Status AC
Exec Time 23 ms
Memory 12924 KB

Compile Error

Hint: system [Processing]
Hint: Main [Processing]
Hint: future [Processing]
Hint: macros [Processing]
Hint: sequtils [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: sets [Processing]
Hint: os [Processing]
Hint: times [Processing]
Hint: posix [Processing]
Hint: hashes [Processing]
Hint: etcpriv [Processing]
Hint: math [Processing]
Hint:  [Link]
Hint: operation successful (23003 lines compiled; 2.357 sec total; 21.220MB; Release Build) [SuccessX]

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 45
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 256 KB
b08 AC 1 ms 256 KB
b09 AC 1 ms 256 KB
b10 AC 23 ms 12924 KB
b11 AC 14 ms 12924 KB
b12 AC 14 ms 12924 KB
b13 AC 14 ms 12924 KB
b14 AC 23 ms 12924 KB
b15 AC 23 ms 12924 KB
b16 AC 7 ms 9468 KB
b17 AC 22 ms 10748 KB
b18 AC 21 ms 10876 KB
b19 AC 7 ms 9468 KB
b20 AC 14 ms 10748 KB
b21 AC 14 ms 10876 KB
b22 AC 14 ms 10876 KB
b23 AC 14 ms 10876 KB
b24 AC 14 ms 10876 KB
b25 AC 20 ms 11260 KB
b26 AC 19 ms 11260 KB
b27 AC 18 ms 11388 KB
b28 AC 17 ms 11260 KB
b29 AC 16 ms 11260 KB
b30 AC 15 ms 11004 KB
b31 AC 6 ms 9468 KB
b32 AC 9 ms 5628 KB
b33 AC 4 ms 4860 KB
b34 AC 6 ms 7676 KB
b35 AC 4 ms 4860 KB
b36 AC 14 ms 12924 KB
b37 AC 14 ms 12924 KB
b38 AC 14 ms 12924 KB
b39 AC 14 ms 12924 KB
b40 AC 14 ms 12924 KB
b41 AC 14 ms 12924 KB
b42 AC 6 ms 9468 KB
b43 AC 6 ms 9468 KB
b44 AC 14 ms 12924 KB
b45 AC 14 ms 12924 KB