結果

問題 No.430 文字列検索
ユーザー minatominato
提出日時 2020-05-15 22:11:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 3,624 bytes
コンパイル時間 2,564 ms
コンパイル使用メモリ 222,004 KB
実行使用メモリ 6,532 KB
最終ジャッジ日時 2023-10-19 14:39:55
合計ジャッジ時間 3,836 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 21 ms
6,532 KB
testcase_02 AC 9 ms
4,348 KB
testcase_03 AC 7 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 21 ms
5,228 KB
testcase_12 AC 22 ms
5,480 KB
testcase_13 AC 22 ms
5,480 KB
testcase_14 AC 14 ms
4,980 KB
testcase_15 AC 11 ms
4,956 KB
testcase_16 AC 11 ms
4,956 KB
testcase_17 AC 10 ms
4,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }

/////////////////////////////////////////////////////////////////////////////////////////////

template<typename C>
struct Trie {
    struct Node {
        int exist;
        vector<int> accept;
        map<C, int> ch;
        Node() : exist(0) {}  
    };

    vector<Node> node;

    Trie() {node.emplace_back();}

    template<typename S>
    void add (const S &A, int id) {
        int cur = 0;
        for (int i = 0; i < (int)A.size(); i++) {
            if (!node[cur].ch.count(A[i])) {
                int nex = node.size();
                node.emplace_back();
                node[cur].ch.emplace(A[i],nex);
            }
            node[cur].exist++;
            cur = node[cur].ch[A[i]];
        }
        node[cur].exist++;
        node[cur].accept.emplace_back(id);
    }

    template<typename S>
    int find(const S &A) {
        int cur = 0;
        for (int i = 0; i < (int)A.size(); i++) {
            if (!node[cur].ch.count(A[i])) return -1;
            cur = node[cur].ch[A[i]];
        }
        return cur;
    }

    int count() const {return node[0].eixst;}

    int size() const {return(int)node.size();}
};

////////////////////////////////////////////////////////////////////////////

void tenka1_2016_final_c() {
    string S; cin >> S;
    int N = S.size();
    int M; cin >> M;
    Trie<char> T;
    for (int i = 0; i < M; i++) {
        string P; cin >> P;
        T.add<string>(P,i);
    }
    vector<int> W(M);
    for (int i = 0; i < M; i++) cin >> W[i];

    vector<int> dp(N+1);
    int ans = 0;
    for (int i = 0; i < N; i++) {
        int cur = 0;
        chmax(ans,dp[i]);
        for (int j = i; j < N; j++) {
            if (T.node[cur].ch.count(S[j])) {
                cur = T.node[cur].ch[S[j]];
                for (auto k : T.node[cur].accept) {
                    chmax(dp[j+1],ans+W[k]);
                }
            } else break;
        }
    }
    chmax(ans,dp[N]);

    cout << ans << endl;
}

void joisc2010_dna() {
    int M; cin >> M;
    string S; cin >> S;
    int N = S.size();
    Trie<char> T;
    for (int i = 0; i < M; i++) {
        string P; cin >> P;
        T.add<string>(P,i);
    }

    vector<int> dp(N,1e9);
    dp[0] = 0;
    for (int i = 0; i < N; i++) {
        int cur = 0;
        for (int j = i; j < N; j++) {
            if (T.node[cur].ch.count(S[j])) {
                cur = T.node[cur].ch[S[j]];
                if (j != N-1 or !T.node[cur].accept.empty()) chmin(dp[j],dp[i]+1);
            } else break;
        }
    }

    cout << dp[N-1] << endl;
}

void yuki430() {
    string S; cin >> S;
    int N = S.size();
    int M; cin >> M;
    Trie<char> T;
    for (int i = 0; i < M; i++) {
        string C; cin >> C;
        T.add<string>(C,i);
    }
    vector<long long> cnt(M);

    for (int i = 0; i < N; i++) {
        int cur = 0;
        for (int j = i; j < N; j++) {
            if (T.node[cur].ch.count(S[j])) {
                cur = T.node[cur].ch[S[j]];
                for (auto k : T.node[cur].accept) cnt[k]++;
            } else break;
        }
    }

    cout << accumulate(cnt.begin(),cnt.end(),0LL) << endl;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //tenka1_2016_final_c();
    //joisc2010_dna();
    yuki430();
}

/*
  verified on 2020/05/15
  https://atcoder.jp/contests/tenka1-2016-final-open/tasks/tenka1_2016_final_c
*/
0