結果

問題 No.430 文字列検索
ユーザー sinsincoscossinsincoscos
提出日時 2019-08-15 23:13:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 83,852 KB
実行使用メモリ 27,884 KB
最終ジャッジ日時 2023-10-19 19:39:08
合計ジャッジ時間 4,473 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 391 ms
27,884 KB
testcase_02 AC 16 ms
5,180 KB
testcase_03 AC 16 ms
5,180 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 385 ms
27,620 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 16 ms
6,132 KB
testcase_11 AC 89 ms
8,876 KB
testcase_12 AC 89 ms
8,876 KB
testcase_13 AC 89 ms
8,876 KB
testcase_14 AC 66 ms
7,292 KB
testcase_15 AC 51 ms
6,500 KB
testcase_16 AC 19 ms
5,444 KB
testcase_17 AC 18 ms
5,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;

const int MAX = 500000, MS = 2;
const ll mod[] = {999999937LL, 1000000007LL}, base = 9973;
struct rolling_hash {
    //全て開区間であることに注意
    int n;
    vector<ll> hs[MS], pw[MS];
    rolling_hash(){}
    rolling_hash(const string &s) {
        n = s.size();
        for (int i = 0; i < MS; i++) {
            hs[i].assign(n+1,0);
            pw[i].assign(n+1,0);
            hs[i][0] = 0;
            pw[i][0] = 1;
            for (int j = 0; j < n; j++) {
                pw[i][j+1] = pw[i][j]*base%mod[i];
                hs[i][j+1] = (hs[i][j]*base+s[j])%mod[i];
            }
        }
    }
    ll hash(int l, int r, int i) { return ((hs[i][r]-hs[i][l]*pw[i][r-l])%mod[i]+mod[i])%mod[i]; }
    bool match(int l1, int r1, int l2, int r2) {
        bool ret = 1;
        for (int i = 0; i < MS; i++) ret &= hash(l1,r1,i)==hash(l2,r2,i);
        return ret;
    }
    bool match(int l, int r, ll h[]) {
        bool ret = 1;
        for (int i = 0; i < MS; i++) ret &= hash(l,r,i)==h[i];
        return ret;
    }
};

int main(){
    string S;
    cin >> S;
    int N = S.size();
    rolling_hash rh(S);
    map<pair<ll,ll>,ll> m[11];
    for(int i=0;i<N;i++){
        for(int j=0;j<10;j++){
            if(i+j>=N) break;
            m[j+1][{rh.hash(i,i+j+1,0),rh.hash(i,i+j+1,1)}]++;
        }
    }
    int M;
    cin >> M;
    ll ans = 0;
    for(int i=0;i<M;i++){
        string C;
        cin >> C;
        rolling_hash now(C);
        ans += m[C.size()][{now.hash(0,C.size(),0),now.hash(0,C.size(),1)}];
    }
    cout << ans << endl;
}
0