結果

問題 No.430 文字列検索
ユーザー TatamoTatamo
提出日時 2016-10-18 18:09:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 1,335 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 59,288 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 17:51:38
合計ジャッジ時間 6,269 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<utility>
#include<vector>
#include<string>

using namespace std;
#define cerr cerr << "[DBG] "
#define DBG(x) cerr << #x << ": " << x << endl
// http://genkisugimoto.com/jp/blog/procon/2015/04/15/print-debug-technique-in-cpp.html
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}

template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) { for (int i = 0; i < v.size(); ++i) { s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; }

typedef long long ll;
typedef unsigned long long ull;

const ull B=100000007;

// 蟻本p332写経
int count(string s, string c){
	int result = 0;
	int sl = s.length();
	int cl = c.length();
	if(cl > sl) return 0;

	// t=B^cl
	ull t = 1;
	for(int i=0; i<cl; i++) t*=B;

	// ハッシュ計算
	ull ch=0, sh=0;
	for(int i=0; i<cl; i++) ch = ch*B+c[i];
	for(int i=0; i<cl; i++) sh = sh*B+s[i];

	// 数える
	for(int i=0; i+cl<=sl; i++){
		if(ch == sh) result++;
		if(i+cl < sl) sh = sh*B + s[i+cl] - s[i]*t; // sのハッシュを1文字文ずらす
	}
	return result;
}

int main(){
	string s;
	cin >> s;
	int m;
	cin >> m;
	ll result = 0;
	for(int i=0; i<m; i++){
		string c;
		cin >> c;
		result += count(s, c);
	}

	cout << result << endl;

	return 0;
}
0