結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
veqcc
|
| 提出日時 | 2019-07-21 12:58:58 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,784 bytes |
| コンパイル時間 | 834 ms |
| コンパイル使用メモリ | 84,652 KB |
| 実行使用メモリ | 27,904 KB |
| 最終ジャッジ日時 | 2024-11-10 00:26:43 |
| 合計ジャッジ時間 | 2,669 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 13 WA * 1 |
ソースコード
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
class RollingHash {
const ll base = 9973;
const vector <ll> mod = {999999937LL, 1000000007LL};
string S;
vector <ll> hash[2], pow[2];
public:
RollingHash(const string &s) {
S = s;
int n = S.size();
for (int i = 0; i < 2; i++) {
hash[i].assign(n + 1, 0);
pow[i].assign(n + 1, 1);
for (int j = 0; j < n; j++) {
hash[i][j + 1] = (hash[i][j] * base + S[j]) % mod[i];
pow[i][j + 1] = pow[i][j] * base % mod[i];
}
}
}
// get hash of S[l:r]
ll get(int l, int r, int id = 0) {
ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % mod[id];
if (res < 0) res += mod[id];
return res;
}
};
// verified
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B&lang=jp
void AOJ_ALDS1_14_B() {
string T, P;
cin >> T >> P;
RollingHash rh1(T), rh2(P);
for (int i = 0; i + P.size() <= T.size(); i++) {
if (rh1.get(i, i + P.size()) == rh2.get(0, P.size())) {
cout << i << "\n";
}
}
}
// verified
// https://yukicoder.me/problems/no/430
void yuki430() {
string S, C;
int M;
cin >> S >> M;
RollingHash rh(S);
map <ll, int> mp;
for (int l = 0; l < S.size(); l++) {
for (int r = 1; r <= 10; r++) {
if (l + r > S.size()) continue;
mp[rh.get(l, l + r)]++;
}
}
int ans = 0;
for (int m = 0; m < M; m++) {
cin >> C;
RollingHash rh1(C);
ans += mp[rh1.get(0, C.size())];
}
cout << ans << "\n";
}
int main() {
// AOJ_ALDS1_14_B();
yuki430();
return 0;
}
veqcc