結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2019-03-12 02:42:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,355 bytes |
コンパイル時間 | 625 ms |
コンパイル使用メモリ | 69,380 KB |
最終ジャッジ日時 | 2025-01-06 22:04:03 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 11 TLE * 3 |
ソースコード
#include <iostream> #include <string> #include <utility> using namespace std; int find_next_head(const std::string& str, const std::string& ptn, int begin) { for (uint i = begin; i < str.size(); ++i) { if (str[i] == ptn[0]) { return i; } } return -1; } std::pair<bool, int> find_next_substr(const std::string& str, const std::string& ptn, int begin) { int pos = find_next_head(str, ptn, begin); if (pos == -1 || str.size() < begin + ptn.size()) { return {false, -1}; } for (uint i = 0; i < ptn.size(); ++i) { if (str[pos + i] != ptn[i]) { return {false, pos + 1}; } } return {true, pos + 1}; } int count_substr(const std::string& str, const std::string& ptn) { int count = 0; std::pair<bool, int> m = find_next_substr(str, ptn, 0); bool found = m.first; int next_pos = m.second; if (found) { count++; } while (next_pos != -1) { m = find_next_substr(str, ptn, next_pos); found = m.first; next_pos = m.second; if (found) { count++; } } return found ? count + 1 : count; } int main() { string S, *C; int M; cin >> S >> M; C = new string[M]; for (int i = 0; i < M; ++i) { cin >> C[i]; } int sum = 0; for (int i = 0; i < M; ++i) { sum += count_substr(S, C[i]); } cout << sum << endl; }