結果

問題 No.430 文字列検索
ユーザー face4face4
提出日時 2019-12-27 10:55:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,159 bytes
コンパイル時間 777 ms
コンパイル使用メモリ 79,236 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-04-16 17:41:00
合計ジャッジ時間 1,950 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 52 ms
7,680 KB
testcase_02 AC 24 ms
7,552 KB
testcase_03 AC 25 ms
7,640 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 49 ms
7,548 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 43 ms
7,676 KB
testcase_12 AC 44 ms
7,548 KB
testcase_13 AC 44 ms
7,552 KB
testcase_14 AC 40 ms
7,548 KB
testcase_15 AC 38 ms
7,676 KB
testcase_16 AC 25 ms
7,548 KB
testcase_17 AC 24 ms
7,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef long long ll;

int main(){
    ll times[10] = {};
    times[0] = 1;
    for(int i = 1; i < 10; i++){
        times[i] = times[i-1]*26;
    }

    string s;
    cin >> s;

    int n = s.length();
    vector<vector<ll>> hash(11);
    
    for(int i = 1; i <= min(n, 10); i++){
        ll roll = 0;
        for(int j = 0; j < i; j++)  roll = roll*26 + s[j]-'A';
        hash[i].push_back(roll);
        for(int j = i; j < n; j++){
            roll %= times[i-1];
            roll *= 26;
            roll += s[j]-'A';
            hash[i].push_back(roll);
        }
    }

    for(int i = 1; i < 11; i++) sort(hash[i].begin(),hash[i].end());

    int m;
    cin >> m;

    string c;
    int ans = 0;
    for(int i = 0; i < m; i++){
        cin >> c;
        int clen = c.length();
        ll target = 0;
        for(int j = 0; j < clen; j++){
            target = target*26 + (c[j]-'A');
        }
        ans += upper_bound(hash[clen].begin(),hash[clen].end(),target)-lower_bound(hash[clen].begin(),hash[clen].end(),target);
    }

    cout << ans << endl;
    
    return 0;
}
0