結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-27 01:50:31 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 109 ms / 2,000 ms |
| コード長 | 999 bytes |
| コンパイル時間 | 1,052 ms |
| コンパイル使用メモリ | 92,220 KB |
| 実行使用メモリ | 18,944 KB |
| 最終ジャッジ日時 | 2024-11-10 00:41:10 |
| 合計ジャッジ時間 | 2,032 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <map>
using namespace std;
const int LEN = 5;
int main() {
string s;
cin >> s;
int m;
cin >> m;
vector<string> c(m);
for (int i = 0; i < m; i++) {
cin >> c[i];
}
vector<map<string, vector<int>>> subs(6);
for (int i = 0; i < s.size(); i++) {
for (int j = 1; j <= 5; j++) {
auto t = s.substr(i, j);
if (t.size() == j) {
subs[j][t].emplace_back(i);
}
}
}
long long ans = 0;
for (auto t: c) {
int t_len = t.size();
if (t_len <= 5) {
ans += subs[t_len][t].size();
continue;
}
// t = u + v
string v = t.substr(LEN);
int v_len = v.size();
if (subs[v_len][v].empty()) {
continue;
}
string u = t.substr(0, LEN);
auto u_pos = subs[LEN][u];
auto v_pos = subs[v_len][v];
for (auto x: u_pos) {
int y = x + LEN;
if (binary_search(v_pos.begin(), v_pos.end(), y)) {
ans++;
}
}
}
cout << ans << endl;
}