結果

問題 No.430 文字列検索
ユーザー neterukunneterukun
提出日時 2020-05-05 02:25:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 1,705 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 86,484 KB
最終ジャッジ日時 2023-09-07 21:09:44
合計ジャッジ時間 5,985 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,288 KB
testcase_01 AC 299 ms
86,484 KB
testcase_02 AC 362 ms
79,072 KB
testcase_03 AC 250 ms
78,852 KB
testcase_04 AC 63 ms
71,320 KB
testcase_05 AC 66 ms
71,140 KB
testcase_06 AC 67 ms
71,532 KB
testcase_07 AC 67 ms
71,300 KB
testcase_08 AC 160 ms
77,620 KB
testcase_09 AC 74 ms
76,096 KB
testcase_10 AC 96 ms
77,148 KB
testcase_11 AC 425 ms
82,984 KB
testcase_12 AC 446 ms
83,904 KB
testcase_13 AC 400 ms
83,692 KB
testcase_14 AC 309 ms
82,040 KB
testcase_15 AC 285 ms
80,044 KB
testcase_16 AC 372 ms
80,520 KB
testcase_17 AC 378 ms
80,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class TrieNode:
    """Trie用のノード用クラスであり、
    子供のポインタ(辞書で管理)とノードが有効かどうかを持つ"""
    def __init__(self, s):
        self.child = {}
        self.valid = False

    def set_child(self, s):
        self.child[s] = TrieNode(s)

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


class Trie:
    """Trie木: 文字列の検索、追加、削除をO(|検索する文字列長さ|)で行う"""
    def __init__(self):
        self.root = TrieNode(None)

    def search(self, string: str) -> bool:
        """集合に文字列が存在するかどうかを返す"""
        ptr = self.root
        for s in string:
            if ptr.get_child(s) is None:
                return False
            ptr = ptr.get_child(s)
        return ptr.valid

    def insert(self, string: str):
        """集合に文字列を追加する"""
        ptr = self.root
        for s in string:
            if ptr.get_child(s) is None:
                ptr.set_child(s)
            ptr = ptr.get_child(s)
        ptr.valid = True

    def delete(self, string: str):
        """集合から文字列を削除する"""
        ptr = self.root
        for s in string:
            if ptr.get_child(s) is None:
                return
            ptr = ptr.get_child(s)
        ptr.valid = False


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

tr = Trie()
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