結果

問題 No.430 文字列検索
ユーザー shung11260shung11260
提出日時 2019-09-03 15:40:54
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,173 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 61,720 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-25 18:29:44
合計ジャッジ時間 10,271 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 882 ms
4,376 KB
testcase_03 AC 884 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 847 ms
4,380 KB
testcase_17 AC 850 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=10000007;

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 i=0; i<s.size(); i++) {
            long long int s2lli;
            s2lli = s[i];
            s2lli %= BASE;
            hash[i+1] = (hash[i]*BASE + s2lli) % MOD;
            power[i+1] = power[i]*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;
    RollingHush rh_str(str);
    
    int M;
    cin >> M;

    long long int ans=0;
    for(int i=0; i<M; i++) {
        string cmp;
        cin >> cmp;
        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