結果
問題 | No.2210 equence Squence Seuence |
ユーザー |
👑 |
提出日時 | 2023-02-19 00:08:44 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 735 ms / 2,000 ms |
コード長 | 8,768 bytes |
コンパイル時間 | 119 ms |
コンパイル使用メモリ | 6,816 KB |
実行使用メモリ | 74,368 KB |
最終ジャッジ日時 | 2024-07-20 06:22:02 |
合計ジャッジ時間 | 8,285 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
local mfl, mce = math.floor, math.ceillocal mmi, mma = math.min, math.maxlocal bls, brs = bit.lshift, bit.rshiftlocal SparseTable = {}SparseTable.create = function(self, n, ary, func)self.n = nself.func = funclocal spt = {{}}for i = 1, n dospt[1][i] = ary[i]enddolocal len = 1for i = 2, 25 doif n < len then break endspt[i] = {}for j = 1, n doif j + len <= n thenspt[i][j] = func(spt[i - 1][j], spt[i - 1][j + len])elsespt[i][j] = spt[i - 1][j]endendlen = len * 2endendlocal sptpos = {}local sptlen = {}dolocal tmp = 1local pos = 1for len = 1, n doif tmp * 2 < len thentmp = tmp * 2pos = pos + 1endsptpos[len] = possptlen[len] = tmpendendself.spt = sptself.sptpos = sptposself.sptlen = sptlenendSparseTable.query = function(self, l, r)local len = r - l + 1local tbl = self.spt[self.sptpos[len]]return self.func(tbl[l], tbl[ r + 1 - self.sptlen[len] ])endSparseTable.new = function(n, ary, func)local obj = {}setmetatable(obj, {__index = SparseTable})obj:create(n, ary, func)return objendlocal function SA_IS(data, data_size, word_size)local suffix_array = {}local type_tag = {} -- -2: L, -1: S, 1~: i-th LMSfor i = 1, data_size dosuffix_array[i] = -1type_tag[i] = -2endlocal word_count = {}local word_count_sum = {}local word_pushback_count = {}local word_pushfront_count = {}for i = 1, word_size doword_count[i] = 0word_count_sum[i] = 0word_pushback_count[i] = 0word_pushfront_count[i] = 0endlocal lms_count = 0local lms_data_indexes = {}local sorted_lms_data_indexes = {}local lms_compressed_data = {}for i = 1, data_size doword_count[data[i]] = word_count[data[i]] + 1endword_count_sum[1] = word_count[1]for i = 2, word_size doword_count_sum[i] = word_count_sum[i - 1] + word_count[i]endlocal prev_type = -2for i = data_size - 1, 1, -1 doif data[i] == data[i + 1] thentype_tag[i] = prev_typeelsetype_tag[i] = data[i] < data[i + 1] and -1 or -2prev_type = type_tag[i]endendfor i = 2, data_size doif type_tag[i - 1] == -2 and type_tag[i] == -1 thenlms_count = lms_count + 1type_tag[i] = lms_countlms_data_indexes[lms_count] = isorted_lms_data_indexes[lms_count] = -1lms_compressed_data[lms_count] = -1endend-- put LMSfor i = 1, lms_count dolocal idx = lms_data_indexes[i]local w = data[idx]suffix_array[word_count_sum[w] - word_pushback_count[w]] = idxword_pushback_count[w] = word_pushback_count[w] + 1end-- put Ldolocal w = data[data_size]local offset = w == 1 and 0 or word_count_sum[w - 1]suffix_array[offset + 1] = data_sizeword_pushfront_count[w] = word_pushfront_count[w] + 1endfor sufa_idx = 1, data_size - 1 dolocal data_idx = suffix_array[sufa_idx]if 1 < data_idx thendata_idx = data_idx - 1if type_tag[data_idx] == -2 thenlocal w = data[data_idx]local offset = w == 1 and 0 or word_count_sum[w - 1]word_pushfront_count[w] = word_pushfront_count[w] + 1suffix_array[offset + word_pushfront_count[w]] = data_idxendendend-- remove LMSfor w = 1, word_size doword_pushback_count[w] = 0end-- put Sfor sufa_idx = data_size, 1, -1 dolocal data_idx = suffix_array[sufa_idx]if 1 < data_idx thendata_idx = data_idx - 1if -1 <= type_tag[data_idx] thenlocal w = data[data_idx]suffix_array[word_count_sum[w] - word_pushback_count[w]] = data_idxword_pushback_count[w] = word_pushback_count[w] + 1endendendlocal lms_data_value = 0local prev_lms_index = 0local prev_lms_data_index = 0for sufa_idx = 1, data_size dolocal data_idx = suffix_array[sufa_idx]local lms_idx = type_tag[data_idx]if 1 <= lms_idx thenlocal is_same = falseif 0 < prev_lms_data_index and prev_lms_index ~= lms_count and lms_idx ~= lms_count thenlocal len1 = lms_data_indexes[lms_idx + 1] - data_idxlocal len2 = lms_data_indexes[prev_lms_index + 1] - prev_lms_data_indexif len1 == len2 thenis_same = truefor i = 0, len1 doif data[i + data_idx] ~= data[i + prev_lms_data_index] thenis_same = falsebreakendendendendif not is_same thenlms_data_value = lms_data_value + 1endlms_compressed_data[lms_idx] = lms_data_valueprev_lms_index = lms_idxprev_lms_data_index = data_idxendendif lms_data_value == lms_count thenfor i = 1, lms_count dosorted_lms_data_indexes[lms_compressed_data[i]] = lms_data_indexes[i]endelselocal lms_sa = SA_IS(lms_compressed_data, lms_count, lms_data_value)for i = 1, lms_count dosorted_lms_data_indexes[i] = lms_data_indexes[lms_sa[i]]endendfor i = 1, lms_count dolocal inv = lms_count + 1 - iif inv <= i then break endsorted_lms_data_indexes[i], sorted_lms_data_indexes[inv]= sorted_lms_data_indexes[inv], sorted_lms_data_indexes[i]end-- Second Induced Sortfor i = 1, data_size dosuffix_array[i] = -1endfor i = 1, word_size doword_pushback_count[i] = 0word_pushfront_count[i] = 0end-- put LMSfor i = 1, lms_count dolocal idx = sorted_lms_data_indexes[i]local w = data[idx]suffix_array[word_count_sum[w] - word_pushback_count[w]] = idxword_pushback_count[w] = word_pushback_count[w] + 1end-- put Ldolocal w = data[data_size]local offset = w == 1 and 0 or word_count_sum[w - 1]suffix_array[offset + 1] = data_sizeword_pushfront_count[w] = word_pushfront_count[w] + 1endfor sufa_idx = 1, data_size - 1 dolocal data_idx = suffix_array[sufa_idx]if 1 < data_idx thendata_idx = data_idx - 1if type_tag[data_idx] == -2 thenlocal w = data[data_idx]local offset = w == 1 and 0 or word_count_sum[w - 1]word_pushfront_count[w] = word_pushfront_count[w] + 1suffix_array[offset + word_pushfront_count[w]] = data_idxendendend-- remove LMSfor w = 1, word_size doword_pushback_count[w] = 0end-- put Sfor sufa_idx = data_size, 1, -1 dolocal data_idx = suffix_array[sufa_idx]if 1 < data_idx thendata_idx = data_idx - 1if -1 <= type_tag[data_idx] thenlocal w = data[data_idx]suffix_array[word_count_sum[w] - word_pushback_count[w]] = data_idxword_pushback_count[w] = word_pushback_count[w] + 1endendendreturn suffix_arrayendlocal SAIS = {}SAIS.create = function(self, data, data_size, word_size)local sufa = SA_IS(data, data_size, word_size)local sufa_inv = {}for i = 1, data_size do sufa_inv[i] = 0 endfor i = 1, data_size dosufa_inv[sufa[i]] = iendself.sufa = sufaself.sufa_inv = sufa_inv-- LCPAlocal n = data_sizelocal lcpa = {}for i = 1, n - 1 do lcpa[i] = 0 endlocal spos = 0for i = 1, n dolocal lcppos = sufa_inv[i]if lcppos < n thenlocal len = sposlocal p1, p2 = sufa[lcppos], sufa[lcppos + 1]p1, p2 = p1 + spos, p2 + sposwhile p1 <= n and p2 <= n doif data[p1] == data[p2] thenlen = len + 1p1, p2 = p1 + 1, p2 + 1else breakendendlcpa[lcppos] = lenspos = 1 < len and len - 1 or 0endendself.lcpa = lcpaself.data = dataself.n = nendlocal n, k = io.read("*n", "*n")local a = {}for i = 1, n doa[i] = io.read("*n")endSAIS:create(a, n, n)local sufa = SAIS.sufalocal sufa_inv = SAIS.sufa_invlocal lcpa = SAIS.lcpalocal spt = SparseTable.new(n - 1, lcpa, mmi)local function getlen(x, y)if y < x then x, y = y, x endreturn spt:query(x, y - 1)endlocal t = {}for i = 1, n dot[i] = iend-- 123456789-- 23456789-- 12 456789-- 1234 6789-- 12345678local function compare(x, y)if x < y thenlocal p1 = sufa_inv[x + 1]local p2 = sufa_inv[x]local len = getlen(p1, p2)if y - x <= len thenreturn falseelsereturn p1 < p2endelselocal p1 = sufa_inv[y + 1]local p2 = sufa_inv[y]local len = getlen(p1, p2)if x - y <= len thenreturn falseelsereturn p2 < p1endendendtable.sort(t, compare)local b = {}for i = 1, n doif i ~= t[k] then table.insert(b, a[i]) endendprint(table.concat(b, " "))