結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2019-03-03 04:10:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,424 bytes |
コンパイル時間 | 581 ms |
コンパイル使用メモリ | 69,884 KB |
最終ジャッジ日時 | 2025-01-06 21:54:20 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 6 WA * 8 |
ソースコード
#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 + i}; } } 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++; } else { if (next_pos == -1) { break; } } } 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 << std::endl; }