結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
shung11260
|
| 提出日時 | 2019-09-02 17:49:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 842 ms / 2,000 ms |
| コード長 | 1,506 bytes |
| コンパイル時間 | 474 ms |
| コンパイル使用メモリ | 61,400 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 00:30:16 |
| 合計ジャッジ時間 | 9,245 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const long long int BASE = 31;
const long long int MOD = 100000007;
struct RollingHash {
vector<long long int> hash;
vector<long long int> power;
RollingHash(string str) : hash(str.size()+1), power(str.size()+1) {
long long int ch;
hash[0] = 0;
power[0] = 1;
for(int i=0; i<str.size(); i++) {
ch = str[i];
ch %= BASE;
power[i+1] = (power[i]*BASE)%MOD;
hash[i+1] = (hash[i]*BASE + ch)%MOD;
}
}
long long int get_hash(int left, int right) {
return ((hash[right]-hash[left]*power[right-left])%MOD+MOD)%MOD;
}
};
int main() {
string str;
cin >> str;
int str_size=str.size();
RollingHash rh_str(str);
int M;
cin >> M;
int ans=0;
for(int i=0; i<M; i++) {
string cmp;
cin >> cmp;
int cmp_size=cmp.size();
RollingHash rh_cmp(cmp);
for(int j=0; j+cmp_size<str_size+1; j++) {
// cout << j << ", " << cmp_size << endl;
// cout << "str[" << j << "] - str[" << j+cmp_size-1 << "] rh = "\
<< rh_str.get_hash(j, j+cmp_size) << endl;
// cout << "cmp rh = " << rh_cmp.get_hash(0, cmp_size) << endl;
if(rh_str.get_hash(j, j+cmp_size)==rh_cmp.get_hash(0, cmp_size)) ans++;
}
}
cout << ans << endl;
return 0;
}
shung11260