結果

問題 No.430 文字列検索
ユーザー shung11260shung11260
提出日時 2019-10-13 17:04:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 826 ms / 2,000 ms
コード長 1,225 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 61,564 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-09 12:53:42
合計ジャッジ時間 9,743 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 818 ms
5,376 KB
testcase_02 AC 820 ms
5,376 KB
testcase_03 AC 817 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 820 ms
5,376 KB
testcase_12 AC 819 ms
5,376 KB
testcase_13 AC 821 ms
5,376 KB
testcase_14 AC 820 ms
5,376 KB
testcase_15 AC 820 ms
5,376 KB
testcase_16 AC 824 ms
5,376 KB
testcase_17 AC 826 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

using namespace std;

const long long int BASE=31;
const long long int MOD=1e8+1;

struct RollingHush {
    vector<long long int> hash;
    vector<long long int> power;
    
    RollingHush(string s) : hash(s.size()+1), power(s.size()+1) {
        hash[0] = 0;
        power[0] = 1;
        for(int si=0; si<s.size(); si++) {
            long long int s2lli;
            s2lli = s[si];
            s2lli %= BASE;
            hash[si+1] = (hash[si]*BASE + s2lli) % MOD;
            power[si+1] = power[si]*BASE % MOD;
        }
    }
    
    long long int get(int left, int right) {
        return ((hash[right]-hash[left]*power[right-left])%MOD + MOD) % MOD;
    }
};

int main() {
    string str;
    cin >> str;
    int str_size=str.size();
    RollingHush rh_str(str);
    
    int M;
    cin >> M;
    long long int ans=0;
    for(int m=0; m<M; m++) {
        string cmp;
        cin >> cmp;
        int cmp_size=cmp.size();
        RollingHush rh_cmp(cmp);
        
        for(int si=0; si+cmp_size<=str_size; si++) {
            if(rh_str.get(si, si+cmp_size)==rh_cmp.get(0, cmp_size)) ans++;
        }
    }
    
    cout << ans << endl;

    return 0;
    
}
0