結果

問題 No.430 文字列検索
ユーザー ysystem7ysystem7
提出日時 2020-04-03 14:13:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 1,619 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 210,632 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 04:22:45
合計ジャッジ時間 8,768 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 505 ms
4,376 KB
testcase_02 AC 502 ms
4,380 KB
testcase_03 AC 504 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 504 ms
4,376 KB
testcase_12 AC 501 ms
4,380 KB
testcase_13 AC 505 ms
4,376 KB
testcase_14 AC 503 ms
4,376 KB
testcase_15 AC 503 ms
4,380 KB
testcase_16 AC 505 ms
4,380 KB
testcase_17 AC 501 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const ll INF=1e18;

const ull B=1000000007;//ハッシュの基数

//aはbに含まれているか?
int contain(string a,string b){
    int res=0;
    int al=a.length(),bl=b.length();
    if(al>bl) return res;

    //Bのal乗を計算
    ull t=1;
    for(int i=0;i<al;i++) t*=B;

    //aとbの最初のal文字に関するハッシュ値を計算
    ull ah=0,bh=0;
    for(int i=0;i<al;i++) ah=ah*B+a[i];
    for(int i=0;i<al;i++) bh=bh*B+b[i];

    //bの場所を1つずつ進めながらハッシュ値をチェック
    for(int i=0;i+al<=bl;i++){
        if(ah==bh) res++;//bのi文字目からのal文字が一致
        if(i+al<bl) bh=bh*B+b[i+al]-b[i]*t;
    }
    return res;
}


int main(){
    string s;
    cin>>s;
    int m;
    cin>>m;
    ll ans=0;
    while(m--){
        string c;
        cin>>c;
        ans=ans+contain(c,s);
    }
    cout<<ans<<endl;
}
0