import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) # verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/NTL_1_E import strutils import sequtils import algorithm ImportExpand "cplib/collections/palindromic_tree.nim" <=== "when not declared CPLIB_COLLECTIONS_PALINDROMIC_TREE:\n const CPLIB_COLLECTIONS_PALINDROMIC_TREE* = 1\n import sequtils\n import algorithm\n type PalindromicTreeNode* = object\n link: seq[ref PalindromicTreeNode]\n suffix_link: ref PalindromicTreeNode\n len, count, id: int\n\n type PalindromicTree* = object\n amax: int\n nodes: seq[ref PalindromicTreeNode]\n\n proc len*(node: PalindromicTreeNode): int = node.len\n proc count*(node: PalindromicTreeNode): int = node.count\n proc id*(node: PalindromicTreeNode): int = node.id\n proc suffix_link*(node: PalindromicTreeNode): ref PalindromicTreeNode = node.suffix_link\n proc link*(node: PalindromicTreeNode): seq[ref PalindromicTreeNode] = node.link\n proc nodes*(pt: PalindromicTree): seq[ref PalindromicTreeNode] = pt.nodes\n\n proc newPalindromicTreeNode(pt: var PalindromicTree, amax, len: int): ref PalindromicTreeNode =\n result = new PalindromicTreeNode\n result[].link = newSeq[ref PalindromicTreeNode](amax)\n result[].suffix_link = nil\n result[].len = len\n result[].count = 0\n result[].id = pt.nodes.len\n pt.nodes.add(result)\n\n proc init(amax: int): PalindromicTree =\n discard result.newPalindromicTreeNode(amax, -1)\n discard result.newPalindromicTreeNode(amax, 0)\n result.amax = amax\n result.nodes[1][].suffix_link = result.nodes[0]\n\n\n proc initPalindromicTree*(a: seq[int], amax: int = -1): PalindromicTree =\n var amax = amax\n if amax < 0: amax = a.max\n result = init(amax)\n proc find_longest(pos: int, node: ref PalindromicTreeNode): ref PalindromicTreeNode =\n var ln = pos - node[].len - 1\n if ln >= 0 and a[ln] == a[pos]:\n return node\n return find_longest(pos, node[].suffix_link)\n var current_node = result.nodes[0]\n for i in 0..