LuoguP2580字典树

link

链接

code

tag[num] == 1表示从根节点到编号为num的节点所表示的字符串曾经出现过。为了判断是否重复,可以对已经重复的tag值修改为2.

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 500010;
int m, n, tr[N][26], tag[N];
int cnt = 1;

int main(){
    
    
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin >> n;
    string s;
    for (int i = 0; i < n; ++i) {
    
    
        cin >> s;
        int u = 1;
        for (int j = 0; j < s.length(); ++j) {
    
    
            int num = s[j] - 'a';
            if (!tr[u][num]) tr[u][num] = ++cnt;
            u = tr[u][num];
        }
        tag[u] = 1;
    }

    cin >> m;
    for (int i = 0; i < m; ++i) {
    
    
        cin >> s;
        int u = 1;
        for (int j = 0; j < s.length(); ++j) {
    
    
            int num = s[j] - 'a';
            u = tr[u][num];
            if (u == 0) break;
        }
        if (tag[u] == 1) {
    
    
            tag[u] = 2;
            cout << "OK" << endl;
        } else if (tag[u] == 2)
            cout << "REPEAT" << endl;
        else
            cout << "WRONG" << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_50070650/article/details/114269722