結果

問題 No.430 文字列検索
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-04-24 11:01:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 673 ms
コンパイル使用メモリ 79,872 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-25 02:29:12
合計ジャッジ時間 1,436 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,704 KB
testcase_01 AC 7 ms
8,832 KB
testcase_02 AC 6 ms
8,832 KB
testcase_03 AC 5 ms
8,832 KB
testcase_04 AC 5 ms
8,704 KB
testcase_05 AC 5 ms
8,832 KB
testcase_06 AC 5 ms
8,576 KB
testcase_07 AC 4 ms
8,832 KB
testcase_08 AC 6 ms
8,832 KB
testcase_09 AC 5 ms
8,832 KB
testcase_10 AC 5 ms
8,832 KB
testcase_11 AC 8 ms
8,832 KB
testcase_12 AC 7 ms
8,832 KB
testcase_13 AC 7 ms
8,960 KB
testcase_14 AC 7 ms
8,832 KB
testcase_15 AC 6 ms
8,832 KB
testcase_16 AC 6 ms
8,704 KB
testcase_17 AC 6 ms
8,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
using namespace std;

vector<int> x;
vector<vector<int>> childrenId;
int ptr;

void init() {
  constexpr int N = 50'005;
  x.assign(N, 0);
  childrenId.assign(26, vector<int>(N, -1));
  ptr = 0;
}

void insert(int id, const string &s) {
  for(char c : s) {
    int pos = c - 'A';
    int &childId = childrenId[pos][id];
    if(childId == -1) { childId = ++ptr; }
    id = childId;
  }
  ++x[id];
}

int count(int id, const string &s) {
  int res = 0;
  for(char c : s) {
    int pos = c - 'A';
    int &childId = childrenId[pos][id];
    if(childId == -1) { break; }
    res += x[childId];
    id = childId;
  }
  return res;
}

int main(void) {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string s; cin >> s;
  int M; cin >> M;
  init();
  for(int i=0; i<M; ++i) {
    string ci; cin >> ci;
    insert(0, ci);
  }
  int res = 0;
  for(size_t i=0, n=s.size(); i<n; ++i) {
    string t = s.substr(i, 10);
    res += count(0, t);
  }
  cout << res << '\n';
  return 0;
}
0