結果

問題 No.430 文字列検索
ユーザー HachimoriHachimori
提出日時 2016-10-03 00:58:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 68,320 KB
実行使用メモリ 26,752 KB
最終ジャッジ日時 2024-05-03 04:28:37
合計ジャッジ時間 2,322 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 225 ms
26,752 KB
testcase_02 AC 11 ms
5,376 KB
testcase_03 AC 12 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 212 ms
26,368 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 16 ms
5,888 KB
testcase_11 AC 95 ms
7,296 KB
testcase_12 AC 94 ms
7,680 KB
testcase_13 AC 93 ms
7,680 KB
testcase_14 AC 76 ms
6,144 KB
testcase_15 AC 59 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 15 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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