結果

問題 No.430 文字列検索
ユーザー masamasa
提出日時 2020-02-27 01:50:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 91,728 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-04-21 16:43:59
合計ジャッジ時間 2,189 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 115 ms
19,072 KB
testcase_02 AC 14 ms
5,376 KB
testcase_03 AC 12 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 102 ms
18,432 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 9 ms
5,504 KB
testcase_11 AC 70 ms
5,376 KB
testcase_12 AC 52 ms
5,376 KB
testcase_13 AC 53 ms
5,376 KB
testcase_14 AC 59 ms
5,376 KB
testcase_15 AC 75 ms
5,376 KB
testcase_16 AC 30 ms
5,376 KB
testcase_17 AC 18 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0