結果

問題 No.430 文字列検索
ユーザー shung11260shung11260
提出日時 2019-09-02 17:49:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 980 ms / 2,000 ms
コード長 1,506 bytes
コンパイル時間 595 ms
コンパイル使用メモリ 61,864 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 09:38:25
合計ジャッジ時間 11,962 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 968 ms
4,376 KB
testcase_02 AC 971 ms
4,376 KB
testcase_03 AC 969 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 971 ms
4,380 KB
testcase_12 AC 980 ms
4,380 KB
testcase_13 AC 968 ms
4,380 KB
testcase_14 AC 978 ms
4,376 KB
testcase_15 AC 970 ms
4,376 KB
testcase_16 AC 970 ms
4,384 KB
testcase_17 AC 970 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;

const long long int BASE = 31;
const long long int MOD = 100000007;

struct RollingHash {
    vector<long long int> hash;
    vector<long long int> power;
    
    RollingHash(string str) : hash(str.size()+1), power(str.size()+1) {
        long long int ch;
        hash[0] = 0;
        power[0] = 1;
        
        for(int i=0; i<str.size(); i++) {
            ch = str[i];
            ch %= BASE;
            
            power[i+1] = (power[i]*BASE)%MOD;
            hash[i+1] = (hash[i]*BASE + ch)%MOD;
        }
    }
    
    long long int get_hash(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();
    
    RollingHash rh_str(str);
    
    int M;
    cin >> M;
    int ans=0;
    for(int i=0; i<M; i++) {
        string cmp;
        cin >> cmp;
        int cmp_size=cmp.size();
        
        RollingHash rh_cmp(cmp);

        for(int j=0; j+cmp_size<str_size+1; j++) {
            // cout << j << ", " << cmp_size << endl;
            // cout << "str[" << j << "] - str[" << j+cmp_size-1 << "] rh = "\
            << rh_str.get_hash(j, j+cmp_size) << endl;
            // cout << "cmp rh = " << rh_cmp.get_hash(0, cmp_size) << endl;
            if(rh_str.get_hash(j, j+cmp_size)==rh_cmp.get_hash(0, cmp_size)) ans++;
        }
    }
    
    cout << ans << endl;

    return 0;
    
}
0