結果

問題 No.430 文字列検索
ユーザー neterukunneterukun
提出日時 2021-05-24 01:36:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 1,896 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 80,668 KB
最終ジャッジ日時 2024-04-20 10:49:43
合計ジャッジ時間 4,291 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,816 KB
testcase_01 AC 292 ms
79,112 KB
testcase_02 AC 225 ms
78,904 KB
testcase_03 AC 186 ms
78,760 KB
testcase_04 AC 41 ms
52,656 KB
testcase_05 AC 39 ms
52,464 KB
testcase_06 AC 38 ms
53,452 KB
testcase_07 AC 37 ms
53,492 KB
testcase_08 AC 161 ms
76,972 KB
testcase_09 AC 42 ms
53,772 KB
testcase_10 AC 79 ms
76,192 KB
testcase_11 AC 394 ms
80,556 KB
testcase_12 AC 358 ms
80,668 KB
testcase_13 AC 353 ms
80,532 KB
testcase_14 AC 271 ms
80,524 KB
testcase_15 AC 246 ms
80,080 KB
testcase_16 AC 301 ms
80,076 KB
testcase_17 AC 310 ms
80,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class PatriciaNode:
    def __init__(self, string=None):
        self.data = string
        self.child = {}
        self.valid = False

    def set_child(self, string):
        self.child[string[0]] = PatriciaNode(string)
        return self.child[string[0]]

    def get_child(self, s):
        if s not in self.child:
            return None
        return self.child[s]


class Patricia:
    def __init__(self):
        self.root = PatriciaNode()

    def _longest_match(self, string):
        ptr = self.root
        i = 0
        while i < len(string):
            ptrch = ptr.get_child(string[i])
            if ptrch  is None:
                break
            j = 1
            while j < len(ptrch .data):
                if i + j == len(string) or string[i + j] != ptrch .data[j]:
                    return ptrch , i + j, j
                j += 1
            i += j
            ptr = ptrch
        return ptr, i, 0

    def search(self, string):
        ptr, match, sub_match = self._longest_match(string)
        if len(string) == match and sub_match == 0:
            return ptr.valid
        return False

    def insert(self, string):
        ptr, match, sub_match = self._longest_match(string)
        if sub_match > 0:
            newptr = PatriciaNode(ptr.data[sub_match:])
            newptr.child = ptr.child
            newptr.valid = ptr.valid
            ptr.data = ptr.data[:sub_match]
            ptr.child = {newptr.data[0]: newptr}
            ptr.valid = False
        if match < len(string):
            ptr = ptr.set_child(string[match:])
        ptr.valid = True

s = input()
m = int(input())
c = [input() for i in range(m)]

tr = Patricia()
for string in c:
    tr.insert(string)

ans = 0
for i in range(len(s)):
    for length in range(1, 11):
        if i + length > len(s):
            break
        if tr.search(s[i:i + length]):
            ans += 1
print(ans)
0