洛谷P2580 于是他错误的点名开始了【字典树】

题目链接:P2580 于是他错误的点名开始了

程序说明:

cnt 数组存储某字符串出现的次数。每次查询完后做标记,下一次再查到的时候输出REPEAT。大概是用getline读字符串处理不好回车的输入,卡了很久(输出是正确的但是全WA。。:) 还是用scanf输入吧不会出错。。。

代码如下:

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 500010;
int trie[N][26], cnt[N], idx;

char str[55];
void insert(char *str) {
    int p = 0;
    for(int i = 0; str[i]; i++) {
        int u = str[i] - 'a';
        if(!trie[p][u]) trie[p][u] = ++idx;
        p = trie[p][u];
    }
    cnt[p]++;
}

int query(char *str) {
    int p = 0;
    for(int i = 0; str[i]; i++) {
        int u = str[i] - 'a';
        if(!trie[p][u]) {
            printf("WRONG\n");
            return 0;
        }
        p = trie[p][u];
    }
    if(cnt[p] == -1)
        printf("REPEAT\n");
    else
        printf("OK\n");
    cnt[p] = -1;
    return cnt[p];
}
int main() {
    int n, m;
    scanf("%d", &n);
    while(n--) {
        scanf("%s", str);
        insert(str);
    }   
    scanf("%d", &m);
    while(m--) {
        scanf("%s", str);
        query(str);
    }
    return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值