conniesarah
大国重器2008
2021-01-22 21:26

P2580 于是他错误的点名开始了(小白求救c++)

  • c++
#include <iostream>
using namespace std;
const int maxn = 1e+5;
struct node {
    char c;
    bool end;
    int num;
    int son[26];
}trie[maxn];
int n, m, cnt = 1;
int index(char c) {
    return c - 'a';
}
void and_trie(string& str) {
    int cur = 1;
    int len = str.length();
    for (int i = 0; i < len; i++) {
        if (trie[cur].son[index(str[i])] == 0) { // 取到每一个字符
            ++cnt;
            trie[cur].son[index(str[i])] = cnt;
        }
        cur = trie[cur].son[index(str[i])];
    }
    trie[cur].end = true;
}
void query_trie(string& str) {
    int cur = 1;
    int len = str.length();
    for (int i = 0; i < len; i++) {
        if (trie[cur].son[index(str[i])] == 0) {
            cout << "WRONG" << endl;
            return;
        }
        cur = trie[cur].son[index(str[i])];
    }
    if (trie[cur].end) {
        if (trie[cur].num == 0) {
            cout << "OK" << endl;
            trie[cur].num++;
        } else {
            cout << "REPEAT" << endl;
        }
    } else {
        cout << "WRONG" << endl;
    }
    return;
}
int main() {
    cin >> n;
    string tmp;
    for (int i = 1; i <= n; i++) {
        cin >> tmp;
        and_trie(tmp);
    }
    cin >> m;
    for (int i = 1; i <= m; i++) {
        cin >> tmp;
        query_trie(tmp);
    }
    return 0;
}
报错信息 报错信息

 

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换