結果

問題 No.430 文字列検索
ユーザー h_nosonh_noson
提出日時 2017-07-21 23:43:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,487 bytes
コンパイル時間 1,854 ms
コンパイル使用メモリ 132,412 KB
実行使用メモリ 11,412 KB
最終ジャッジ日時 2024-04-17 10:45:50
合計ジャッジ時間 2,698 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'int Aho::match(std::string)',
    inlined from 'int main()' at main.cpp:88:24:
main.cpp:70:25: warning: 'matches' may be used uninitialized [-Wmaybe-uninitialized]
   70 |                 matches += matched[now].size();
      |                 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:64:13: note: 'matches' was declared here
   64 |         int matches;
      |             ^~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <cassert>
using namespace std;

#define GET_ARG(a,b,c,F,...) F
#define REP3(i,s,e) for (i = s; i <= e; i++)
#define REP2(i,n) REP3 (i,0,(int)(n)-1)
#define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__)
#define RREP3(i,s,e) for (i = s; i >= e; i--)
#define RREP2(i,n) RREP3 (i,(int)(n)-1,0)
#define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

struct Aho {
    vector<map<char,int>> states;
    vector<int> fail_path;
    vector<unordered_set<string>> matched;

    Aho(vector<string> dict) {
        states.push_back({});
        for (auto s: dict) {
            int now = 0;
            for (auto c: s) {
                if (!states[now].count(c)) {
                    states[now][c] = states.size();
                    states.push_back({});
                }
                now = states[now][c];
            }
            matched.resize(states.size());
            matched[now].insert(s);
        }
        fail_path.resize(states.size());

        queue<int> q;
        q.push(0);
        while (!q.empty()) {
            auto now = q.front(); q.pop();
            for (auto p: states[now]) {
                char c = p.first;
                int next = p.second;
                q.push(next);
                if (now == 0) continue;
                int fail = fail_path[now];
                while (fail > 0 && !states[fail].count(c)) fail = fail_path[fail];
                if (states[fail].count(c)) {
                    int x = states[fail][c];
                    fail_path[next] = x;
                    matched[next].insert(matched[x].begin(),matched[x].end());
                }
            }
        }
    }

    int match(string s) {
        int matches;
        int now = 0;
        for (auto c: s) {
            while (now > 0 && !states[now].count(c)) now = fail_path[now];
            if (states[now].count(c)) {
                now = states[now][c];
                matches += matched[now].size();
            }
        }
        return matches;
    }
};

int main(void) {
    int i, j, k, m;
    string s;
    cin >> s >> m;
    vector<string> dict;
    while (m--) {
        string t;
        cin >> t;
        dict.push_back(t);
    }
    Aho aho = Aho(dict);
    cout << aho.match(s) << endl;
    return 0;
}
0