結果

問題 No.430 文字列検索
ユーザー ttkkggwwttkkggww
提出日時 2024-05-31 17:01:18
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,829 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 3,828 ms
コンパイル使用メモリ 267,748 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-10 01:11:38
合計ジャッジ時間 22,768 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
string S;
using mint = modint998244353;
int M;
vector<string> T;

void solve(){
	ll base = 314;
	vector<mint> coef(S.size() );
	coef[0] = 1;
	for(int i = 1;i<S.size();i++) coef[i] = coef[i-1] * base;


	int ans = 0;
	for(int i = 0;i<M;i++){
		if(S.size() < T[i].size()) continue;
		mint hashT = 0;
		mint hashS = 0;
		for(int j = 0;j<T[i].size();j++){
			hashT = hashT*base + T[i][j];
			hashS = hashS*base + S[j];
		}
		if(hashT == hashS) ans++;
		for(int j = 0;j<S.size()-T[i].size();j++){
			hashS = hashS * base + S[j+T[i].size()];
			hashS -= coef[T[i].size()] * S[j];
			if(hashT == hashS) ans++;
		}
	}
	cout<<ans<<endl;

	
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> S >> M;
	T.resize(M);
	for(int i = 0; i < M; ++i) cin >> T[i];
	solve();
}

0