結果

問題 No.430 文字列検索
ユーザー furafura
提出日時 2020-06-07 04:07:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 2,128 ms
コンパイル使用メモリ 210,516 KB
実行使用メモリ 27,644 KB
最終ジャッジ日時 2023-08-25 03:07:58
合計ジャッジ時間 4,183 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 334 ms
27,644 KB
testcase_02 AC 13 ms
4,900 KB
testcase_03 AC 13 ms
4,940 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 315 ms
27,396 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 16 ms
5,856 KB
testcase_11 AC 67 ms
8,652 KB
testcase_12 AC 65 ms
8,640 KB
testcase_13 AC 66 ms
8,672 KB
testcase_14 AC 52 ms
7,132 KB
testcase_15 AC 41 ms
6,472 KB
testcase_16 AC 16 ms
5,440 KB
testcase_17 AC 15 ms
5,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

class rolling_hash{
	static const long long base=10007,mod1=1e9+7,mod2=1e9+9;
	int len;
	vector<long long> pow1,pow2,h1,h2;
public:
	rolling_hash(const string& s):len(s.length()),pow1(len+1),pow2(len+1),h1(len+1),h2(len+1){
		pow1[0]=pow2[0]=1;
		rep(i,len){
			pow1[i+1]=pow1[i]*base%mod1;
			pow2[i+1]=pow2[i]*base%mod2;
			h1[i+1]=(h1[i]*base+s[i])%mod1;
			h2[i+1]=(h2[i]*base+s[i])%mod2;
		}
	}
	pair<long long,long long> get_hash(int l,int r)const{
		assert(0<=l && l<=r && r<=len);
		auto res1=(h1[r]-h1[l]*pow1[r-l])%mod1; if(res1<0) res1+=mod1;
		auto res2=(h2[r]-h2[l]*pow2[r-l])%mod2; if(res2<0) res2+=mod2;
		return {res1,res2};
	}
	pair<long long,long long> get_hash()const{ return get_hash(0,len); };
};

int main(){
	string s; cin>>s;
	int n=s.length();
	rolling_hash H(s);

	map<pair<lint,lint>,int> f;
	rep(d,min(n,10)+1) rep(i,n-d+1) ++f[H.get_hash(i,i+d)];

	int ans=0,q; cin>>q;
	rep(_,q){
		string t; cin>>t;
		ans+=f[rolling_hash(t).get_hash()];
	}
	cout<<ans<<'\n';

	return 0;
}
0