結果

問題 No.430 文字列検索
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-10-02 23:09:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 518 ms / 2,000 ms
コード長 1,016 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 65,948 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-15 17:25:08
合計ジャッジ時間 7,602 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
const int INF = 1e9;

string c[5010];

typedef unsigned long long ull;
const ull B = 100000007;
//aはbにいくつ
int contain(string a, string b){
	// cout << a << " "<<  b << endl;
	int ret = 0;
	int al = a.length(), bl = b.length();
	// printf("%d %d\n", al, bl);
	if(al > bl) return 0;

	ull t = 1;
	for (int i = 0; i < al; ++i) t *= B;

	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];

	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;
	}
	// printf("ret %d\n", ret);
	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){
		// printf("m %d\n", m);
		ans += contain(c[i], s);
	}
	printf("%lld\n", ans);
	return 0;
}
0