結果

問題 No.430 文字列検索
ユーザー Hachimori
提出日時 2016-10-03 00:58:44
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 68,312 KB
実行使用メモリ 26,496 KB
最終ジャッジ日時 2024-11-10 00:07:33
合計ジャッジ時間 2,232 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

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


int ch2v(char ch) {
    return ch - 'A' + 1;
}

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<long long, int> hash2cnt;
    for (int len = 1; len <= min(10, (int) txt.size()); ++len) {
        long long currHash = 0;
        for (int i = 0; i < len; ++i) {
            currHash = currHash * 26 + ch2v(txt[i]);
        }
    
        long long p = 1;
        for (int i = 0; i < len; ++i) {
            p *= 26;
        }

        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 = currHash * 26 + ch2v(txt[i + len]) - p * ch2v(txt[i]);
        }
    }
    
    int sum = 0;
    for (int i = 0; i < nWord; ++i) {
        long long targetHash = 0;
        for (int j = 0; j < word[i].size(); ++j) {
            targetHash = targetHash * 26 + ch2v(word[i][j]);
        }
        sum += hash2cnt[targetHash];
    }
    cout << sum << endl;
}


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