結果

問題 No.430 文字列検索
ユーザー kenta255kenta255
提出日時 2019-08-29 10:22:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 934 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 2,411 ms
コンパイル使用メモリ 215,840 KB
実行使用メモリ 51,072 KB
最終ジャッジ日時 2024-04-28 21:00:31
合計ジャッジ時間 5,995 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 934 ms
51,072 KB
testcase_02 AC 73 ms
5,632 KB
testcase_03 AC 90 ms
5,632 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 866 ms
50,432 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 35 ms
8,320 KB
testcase_11 AC 193 ms
12,800 KB
testcase_12 AC 189 ms
12,800 KB
testcase_13 AC 186 ms
12,800 KB
testcase_14 AC 143 ms
9,856 KB
testcase_15 AC 111 ms
8,064 KB
testcase_16 AC 56 ms
5,760 KB
testcase_17 AC 54 ms
5,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

struct RollingHash{
	ll M;
	vector<ll> H,B;
	RollingHash(string s,ll b=1009LL,ll m=1000000007LL){//999999937LL
		M = m;
		ll n = s.size();
		B.resize(n+1);
		H.resize(n+1,0);
		B[0] = 1;
		for(ll i=0;i<n;i++){
			B[i+1] = (B[i]*b)%M;
			H[i+1] = (H[i]*b%M+s[i])%M;
		}
	}
	ll GetHash(ll l,ll r){ // [l,r)
		return (H[r]+(M-B[r-l]*H[l]%M))%M;
	}
};


int main(){
	string S,C;
	ll M,ans=0,Sl;
	cin >> S >> M;
	Sl = S.size();
	map<ll,ll> ma1,ma2;
	RollingHash Shash1(S);
	RollingHash Shash2(S,9973,999999937);
	for(int i=0;i<M;i++){
		cin >> C;
		RollingHash Chash1(C);
		RollingHash Chash2(C,9973,999999937);
		ll cl = C.size();
		ll ch1 = Chash1.GetHash(0,cl);
		ll ch2 = Chash2.GetHash(0,cl);
		ma1[ch1]++;
		ma2[ch2]++;
	}
	for(int i=1;i<11;i++){
		for(int j=0;j<Sl-i+1;j++){
			ans += min(ma1[Shash1.GetHash(j,j+i)],ma2[Shash2.GetHash(j,j+i)]);
		}
	}
	cout << ans << endl;
}
0