結果

問題 No.430 文字列検索
ユーザー mkawa2mkawa2
提出日時 2020-04-23 01:21:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 657 ms / 2,000 ms
コード長 883 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 81,088 KB
最終ジャッジ日時 2024-04-20 16:17:55
合計ジャッジ時間 7,798 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,352 KB
testcase_01 AC 657 ms
80,768 KB
testcase_02 AC 630 ms
80,640 KB
testcase_03 AC 638 ms
80,812 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 38 ms
52,736 KB
testcase_06 AC 38 ms
52,352 KB
testcase_07 AC 38 ms
52,224 KB
testcase_08 AC 53 ms
65,536 KB
testcase_09 AC 45 ms
59,264 KB
testcase_10 AC 48 ms
60,672 KB
testcase_11 AC 639 ms
80,512 KB
testcase_12 AC 637 ms
80,896 KB
testcase_13 AC 636 ms
81,088 KB
testcase_14 AC 635 ms
80,768 KB
testcase_15 AC 632 ms
80,768 KB
testcase_16 AC 630 ms
80,896 KB
testcase_17 AC 644 ms
81,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline()[:-1]

def main():
    def hash(aa):
        res = 0
        base=1
        for a in aa[::-1]:
            res += a*base
            base*=26
        return res


    s=SI()
    s=[ord(c)-65 for c in s]
    sn=len(s)
    memo=[[-1]*sn for _ in range(11)]
    ans=0
    for _ in range(II()):
        t=SI()
        t = [ord(c) - 65 for c in t]
        tn=len(t)
        ht=hash(t)
        memotn = memo[tn]
        if memotn[0]!=-1:
            memotn=memo[tn]
            for i in range(sn - tn + 1):
                if memotn[i]==ht:ans+=1
        else:
            hs=hash(s[:tn])
            mxd=pow(26,tn-1)
            for i in range(sn - tn + 1):
                if i:hs=(hs-mxd*s[i-1])*26+s[i+tn-1]
                if hs==ht:ans+=1
                memotn[i]=hs

    print(ans)

main()
0