結果

問題 No.430 文字列検索
ユーザー cielciel
提出日時 2016-10-03 22:47:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 585 ms / 2,000 ms
コード長 1,152 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 76,384 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-03 04:34:08
合計ジャッジ時間 7,317 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 584 ms
6,944 KB
testcase_02 AC 582 ms
6,940 KB
testcase_03 AC 582 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 AC 585 ms
6,944 KB
testcase_12 AC 585 ms
6,940 KB
testcase_13 AC 579 ms
6,944 KB
testcase_14 AC 584 ms
6,940 KB
testcase_15 AC 581 ms
6,940 KB
testcase_16 AC 582 ms
6,940 KB
testcase_17 AC 582 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstring>
using namespace std;

//ALDS1 14D cannot be solved.
//BWT=O(S+TQlogS) Hash=O(TS)

const int B=999999937;
const int P=1000000007;

void gen_hash(const string &s,vector<long long> &hash){
	long long c=0;
	for(int i=0;i<s.size();i++){
		c=(c*B+s[i])%P;
		hash[i]=c;
	}
}
long long pow_binary_mod(long long x,long long y,long long mod){
	long long z=1;
	for(;y;y>>=1){
		if((y&1)!=0)z=z*x%mod;
		x=x*x%mod;
	}
	return z;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	string s;
	cin>>s;
	int ls=s.size();
	vector<long long> v(ls),hsh(10001);
	gen_hash(s,v);

	int R=0;
	int T;
	for(cin>>T;T--;){
		string q;
		cin>>q;
		int lq=q.size();
		gen_hash(q,hsh);
		long long hash=hsh[lq-1];
		long long Brev=pow_binary_mod(B,lq,P);
		vector<int> r;
		int i=lq-1;
		for(;i<ls;i++){
			if(v[i]==((i>=lq?v[i-lq]:0)*Brev+hash)%P){
				r.push_back(i-lq+1);
				//break;
			}
		}
		if(!r.empty()){
			for(int i=0;i<r.size();i++)R++;//printf("%d\n",r[i]);
			//puts("1");
		}else{
			//puts("0");
		}
	}
	printf("%d\n",R);
}
0