結果

問題 No.515 典型LCP
ユーザー sue_charosue_charo
提出日時 2017-06-02 15:04:16
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,693 bytes
コンパイル時間 1,206 ms
コンパイル使用メモリ 81,524 KB
実行使用メモリ 270,568 KB
最終ジャッジ日時 2023-10-21 20:47:05
合計ジャッジ時間 5,874 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N = II()
    s = [str(input()) for __ in range(N)]
    M, x, d = ILI()
    return (N, s, M, x, d)


def makeQuery(N, M, x, d):
    query = []
    for k in range(M):
        i = (x // (N - 1)) + 1
        j = (x % (N - 1)) + 1
        if i > j:
            i, j = j, i
        else:
            j += 1
        query.append((i - 1, j - 1))
        x = (x + d) % (N * (N - 1))
    return query


def solve(N, s, M, x, d):
    query = makeQuery(N, M, x, d)
    s_sorted = sorted(s)
    d_ind_sort = collections.defaultdict(int)
    for ind, str in enumerate(s_sorted):
        d_ind_sort[str] = ind
    l_lcp = []
    for i in range(N - 1):
        str_1 = s_sorted[i]
        str_2 = s_sorted[i + 1]
        count = 0
        for char_1, char_2 in zip(str_1, str_2):
            if char_1 != char_2:
                break
            count += 1
        l_lcp.append(count)

    ans = 0
    for q in query:
        str_1 = s[q[0]]
        str_2 = s[q[1]]
        sorted_ind_1 = d_ind_sort[str_1]
        sorted_ind_2 = d_ind_sort[str_2]
        ind_min = min(sorted_ind_1, sorted_ind_2)
        ind_max = max(sorted_ind_1, sorted_ind_2)
        lcp = min(l_lcp[ind_min:ind_max])
        ans += lcp

    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()
0