結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
goodbaton
|
| 提出日時 | 2019-02-20 23:15:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,973 ms / 2,000 ms |
| コード長 | 1,872 bytes |
| コンパイル時間 | 980 ms |
| コンパイル使用メモリ | 97,160 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 00:22:21 |
| 合計ジャッジ時間 | 16,714 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << '{';
for (const T &item : v) out << item << ", ";
out << "\b\b}";
return out;
}
#endif
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010
/* KMP */
// A[i] := 文字列S[0,i-1]の接頭辞と接尾辞が最大何文字一致しているか (i-1未満)
// len(A) == S.size() + 1
void KMP(const char *S, int *A){
A[0] = -1;
int j = -1, n = strlen(S);
for (int i = 0; i < n; i++) {
while (j >= 0 && S[i] != S[j]) j = A[j];
j++;
//KMP
if (S[i+1] == S[j]) A[i+1] = A[j];
else A[i+1] = j;
//MP
//A[i+1] = j;
}
}
char s[SIZE];
int main(){
int n, m, ans = 0;
scanf("%s%d", s, &m);
n = strlen(s);
for(int i=0; i<m; i++) {
char p[12] = {};
int k[14];
scanf("%s", p);
p[strlen(p)] = '#';
KMP(p, k);
int t = 0;
for(int j=0; j<n; j++) {
if (p[t] == s[j]) {
t++;
} else {
ans += p[t] == '#';
j -= k[t] >= 0;
t = max(0, k[t]);
}
}
ans += p[t] == '#';
}
printf("%d\n", ans);
return 0;
}
goodbaton