結果

問題 No.430 文字列検索
ユーザー cocolleaguecocolleague
提出日時 2017-02-04 20:48:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 575 ms / 2,000 ms
コード長 1,001 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 145,376 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 20:15:16
合計ジャッジ時間 8,281 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
typedef long long ll;
static const int INF = 1e8;

typedef unsigned long long ull;

string c[5010];
const ull B = 100000007;

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

	//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の場所を一つずつ進めながらハッシュ値をチェック
		for(int i = 0; i + al <= bl; i++){
			if(ah == bh) ret++;
			if(i + al < bl) bh = bh * B + b[i+al] - b[i] * t;
		}
		return ret;
}
int main(void){
	string s; cin >> s;
	int m; cin >> m;
	rep(i,m) cin >> c[i];
    ll ans = 0;
    rep(i,m){
    	ans += contain(c[i],s);
    }
    printf("%lld\n",ans);
    return 0;
}
0