洛谷 P2580 于是他错误的点名开始了 题解

每日一题 day10 打卡

Analysis

trie树模板题,只需用到简单的插入和查询就好了

如果想要学trie树,见信息学奥赛一本通·提高篇P82

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,m,cnt=0;
 7 string a[10001];
 8 int ch[500001][105];
 9 bool bo[500001],book[500001];
10 inline int read() 
11 {
12     int x=0;
13     bool f=1;
14     char c=getchar();
15     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
16     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
17     if(f) return x;
18     return 0-x;
19 }
20 void insert(string x)
21 {
22     int len=x.size();
23     int u=1;
24     for(int i=0;i<len;i++)
25     {
26         int c=x[i]-'a';
27         if(ch[u][c]==0)ch[u][c]=++cnt;
28         u=ch[u][c];
29     }
30     bo[u]=true;
31 }
32 int check(string y)
33 {
34     int len=y.size();
35     int u=1;
36     for(int i=0;i<len;i++)
37     {
38         int c=y[i]-'a';
39         if(ch[u][c]==0)return 2;
40         u=ch[u][c];
41     }
42     if(bo[u]!=true)return 2;
43     else if(bo[u]==true&&book[u]==0)
44     {
45         book[u]=1;
46         return 1;
47     }
48     else if(bo[u]==true&&book[u]==1)
49     {
50         return 3;
51     }
52 }
53 int main()
54 {
55     n=read();
56     for(int i=1;i<=n;i++)
57     {
58         cin>>a[i];
59         insert(a[i]);
60     }
61     m=read();
62     string x;
63     for(int i=1;i<=m;i++)
64     {
65         cin>>x;
66         int se=check(x);
67         if(se==1)cout<<"OK"<<endl;
68         if(se==2)cout<<"WRONG"<<endl;
69         if(se==3)cout<<"REPEAT"<<endl;
70     }
71     return 0;
72 }

请各位大佬斧正(反正我不认识斧正是什么意思)

原文地址:https://www.cnblogs.com/handsome-zyc/p/11517573.html