結果

問題 No.430 文字列検索
ユーザー HachimoriHachimori
提出日時 2016-10-03 00:56:27
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,617 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 69,288 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-05-01 11:10:01
合計ジャッジ時間 5,185 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<map>
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];
    }
}


void work() {
    map<int, int> hash2cnt;
    for (int len = 1; len <= min(10, (int) txt.size()); ++len) {
        int currHash = 0;
        for (int i = 0; i < len; ++i) {
            currHash = add(mul(currHash, 256),  txt[i]);
        }
    
        int p = 1;
        for (int i = 0; i < len; ++i) {
            p = mul(p, 256);
        }

        for (int i = 0; i + len <= txt.size(); ++i) {
            if (hash2cnt.count(currHash)) {
                int old = hash2cnt[currHash];
                hash2cnt[currHash] = old + 1;
            }
            else {
                hash2cnt[currHash] = 1;
            }
            
            currHash = sub(currHash, mul(p, txt[i]));
            currHash = mul(currHash, 256);
            currHash = add(currHash, txt[i + len]);
        }
    }
    
    int sum = 0;
    for (int i = 0; i < nWord; ++i) {
        int targetHash = 0;
        for (int j = 0; j < word[i].size(); ++j) {
            targetHash = add(mul(targetHash, 256), word[i][j]);
        }
        sum += hash2cnt[targetHash];
    }
    cout << sum << endl;
}


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