結果

問題 No.430 文字列検索
ユーザー HachimoriHachimori
提出日時 2016-10-02 23:15:01
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,386 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 58,388 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-05-01 10:05:13
合計ジャッジ時間 3,742 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
using namespace std;
const int MOD = 1000000007;
const int WORD = 5005;


int add(int a, int b) {
    return (1LL * a + b) % MOD;
}

int sub(int a, int b) {
    a %= MOD;
    b %= MOD;
    return (a + MOD - b) % MOD;
}

int mul(int a, int b) {
    return (1LL * a * b) % MOD;
}


string txt;
int nWord;
string word[WORD];
    
void read() {
    cin >> txt;
    cin >> nWord;
    for (int i = 0; i < nWord; ++i) {
        cin >> word[i];
    }
}


int calc(string &wd) {
    if (txt.size() < wd.size()) return 0;
    
    int targetHash = 0;
    for (int i = 0; i < wd.size(); ++i) {
        targetHash = add(mul(targetHash, 256),  wd[i]);
    }

    int currHash = 0;
    for (int i = 0; i < wd.size(); ++i) {
        currHash = add(mul(currHash, 256),  txt[i]);
    }
    
    int p = 1;
    for (int i = 0; i < wd.size() - 1; ++i) {
        p = mul(p, 256);
    }

    int cnt = 0;
    for (int i = 0; i + wd.size() <= txt.size(); ++i) {
        if (currHash == targetHash) {
            ++cnt;
        }
        currHash = sub(currHash, mul(p, txt[i]));
        currHash = mul(currHash, 256);
        currHash = add(currHash, txt[i + wd.size()]);
    }
    
    return cnt;
}

void work() {
    int sum = 0;
    for (int i = 0; i < nWord; ++i) {
        sum += calc(word[i]);
    }
    cout << sum << endl;
}


int main() {
    read();
    work();
    return 0;
}
0