結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-03 22:47:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 539 ms / 2,000 ms |
| コード長 | 1,152 bytes |
| コンパイル時間 | 727 ms |
| コンパイル使用メモリ | 76,368 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 00:08:42 |
| 合計ジャッジ時間 | 6,530 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#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);
}