結果

問題 No.430 文字列検索
ユーザー neterukunneterukun
提出日時 2021-05-24 01:08:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,063 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 80,512 KB
最終ジャッジ日時 2024-04-20 10:16:33
合計ジャッジ時間 5,246 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,968 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 37 ms
52,352 KB
testcase_05 AC 36 ms
51,968 KB
testcase_06 AC 37 ms
51,968 KB
testcase_07 AC 36 ms
53,120 KB
testcase_08 WA -
testcase_09 AC 41 ms
53,760 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

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
            ptr.data = ptr.data[:sub_match]
            ptr.child = {newptr.data[0]: newptr}
            if match < len(string):
                ptr.valid = False
                ptr = ptr.set_child(string[match:])
                ptr.valid = True
        else:
            if match == len(string):
                return False
            else:
                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