結果

問題 No.430 文字列検索
ユーザー gyouzasushigyouzasushi
提出日時 2019-12-01 01:27:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,004 bytes
コンパイル時間 1,395 ms
コンパイル使用メモリ 173,836 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 03:34:17
合計ジャッジ時間 25,339 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n-1);i>=0;i--)
#define FOR(i,n,m) for(int i=n;i<=(int)(m);i++)
#define RFOR(i,n,m) for(int i=(int)(n);i>=m;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(std::unique(all(x)), x.end());
typedef long long ll;
const int INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;
const double PI=acos(-1);
using namespace std;
vector<int> dx={1,0,-1,0};
vector<int> dy={0,1,0,-1};
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

struct RollingHash {
    int n;
    vector<pair<ll,ll>> vec,pw;
    ll mod1,mod2,base1,base2;
    
    RollingHash(string s) {
        //srand(time(NULL));
        n=sz(s);
        vec.resize(n+1);
        pw.resize(n+1);
        pw[0]={1,1};
        
        mod1=1e9+93;
        mod2=1e9+97;
        //base1=(ll)rand()%((ll)1e7+9);
        //base2=(ll)rand()%((ll)1e7+9);
        base1=10007;
        base2=10007;
        rep(i,n) {
            pw[i+1].first=pw[i].first*base1%mod1;
            pw[i+1].second=pw[i].second*base2%mod2;
            vec[i+1].first=(vec[i].first*base1+(ll)s[i])%mod1;
            //vec[i+1].second=(vec[i].second*base2+(ll)s[i])%mod2;
        }
    }
    
    //[l,r)
    pair<ll,ll> get(int l,int r) {
        ll fi,se=0;
        fi=(vec[r].first-vec[l].first*pw[r-l].first+mod1*mod1)%mod1;
        //se=(vec[r].second-vec[l].second*pw[r-l].second+mod1*mod2)%mod2;
        return make_pair(fi,se);
    }
};

int main() {
    string s;
    cin>>s;
    RollingHash rlh(s);
    
    int m;
    cin>>m;
    int ans=0;
    while(m--) {
        string t;
        cin>>t;
        RollingHash rlh2(t);
        rep(i,sz(s)-sz(t)+1) {
            if(rlh.get(i,i+sz(t))==rlh2.get(0,sz(t))) ans++;
        }
    }
    cout<<ans<<endl;
}
0