結果

問題 No.430 文字列検索
ユーザー ahe100ahe100
提出日時 2019-03-14 20:15:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 792 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 535 ms
コンパイル使用メモリ 73,544 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 09:24:05
合計ジャッジ時間 9,828 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

/*

*/

ll hash26(string &c,ll mod=1e18){
	ll hs=0,mul=1;
	rep(i,c.size()){
		hs=hs+mul*(c[i]-'A');
		mul*=26;
	}
	return hs;
}

ll rolling_hash(string &s,string &c,ll mod=1e18){
	if(c.size()>s.size())return 0;
	ll hs=0,ans=0,mul=1,len=c.size(),comp=hash26(c);
	rep(i,len){
		hs=hs+mul*(s[i]-'A');
		mul*=26;
	}
	mul/=26;
	if(hs==comp)ans++;//衝突が不安ならここで検証する
	rep(i,s.size()-len){
		hs=(hs-(s[i]-'A'))/26;
		hs=hs+mul*(s[i+len]-'A');
		if(hs==comp){
			ans++;//衝突が不安ならここで検証する
		}
	}
	return ans;
}

string s,c[5010];
ll m,ans=0;

int main(void){
	cin>>s>>m;
	rep(i,m)cin>>c[i];
	rep(i,m){
		ans+=rolling_hash(s,c[i]);
	}
	cout<<ans<<endl;
	return 0;
}
0