zoukankan      html  css  js  c++  java
  • [Trie] Luogu P2580 于是他错误的点名开始了

    题目描述

    这之后校长任命你为特派探员,每天记录他的点名。校长会提供化学竞赛学生的人数和名单,而你需要告诉校长他有没有点错名。(为什么不直接不让他玩炉石。)

    输入输出格式

    输入格式:

    第一行一个整数 n,表示班上人数。接下来 n 行,每行一个字符串表示其名字(互不相同,且只含小写字母,长度不超过 50)。第 n+2 行一个整数 m,表示教练报的名字。接下来 m 行,每行一个字符串表示教练报的名字(只含小写字母,且长度不超过 50)。

    输出格式:

    对于每个教练报的名字,输出一行。如果该名字正确且是第一次出现,输出“OK”,如果该名字错误,输出“WRONG”,如果该名字正确但不是第一次出现,输出“REPEAT”。(均不加引号)

    输入输出样例

    输入样例#1:
    5  
    a
    b
    c
    ad
    acd
    3
    a
    a
    e
    
    输出样例#1:
    OK
    REPEAT
    WRONG
    

    说明

    对于 40%的数据,n≤1000,m≤2000;

    对于 70%的数据,n≤10000,m≤20000;

    对于 100%的数据, n≤10000,m≤100000。

    题解

    • 这题应该是比较裸的一棵trie
    • 插入、搜索,再加多一个cnt统计有没有找过

    代码

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 using namespace std;
     5 int n,m,v[800000],end[800000],tot,trie[800000][40];
     6 char ch[60];
     7 void insert(char *str)
     8 {
     9     int len=strlen(str),p=0;
    10     for (int i=0;i<len;i++)
    11     {
    12         int ch=str[i]-'a';
    13         if (trie[p][ch]==0) trie[p][ch]=++tot;
    14         p=trie[p][ch];
    15     }
    16     end[p]=true;
    17 }
    18 void search(char *str)
    19 {
    20     int len=strlen(str),p=0;
    21     for (int i=0;i<len;i++)
    22     {
    23         p=trie[p][str[i]-'a'];
    24         if (p==0) 
    25         {
    26             cout<<"WRONG"<<endl;
    27             return;
    28         }
    29     }
    30     if (end[p]==false) 
    31     {
    32         cout<<"WRONG"<<endl;
    33         return;
    34     }
    35     if (v[p]!=0) 
    36     {
    37         cout<<"REPEAT"<<endl;
    38         return;
    39     }
    40     if (v[p]==0) 
    41     {
    42         cout<<"OK"<<endl;
    43         v[p]=1;
    44     }
    45     
    46 }
    47 int main()
    48 {
    49     scanf("%d",&n);
    50     for (int i=1;i<=n;i++) 
    51     {
    52         scanf("%s",ch);
    53         insert(ch);
    54     }
    55     scanf("%d",&m);
    56     for (int i=1;i<=m;i++)
    57     {
    58         scanf("%s",ch);
    59         search(ch);
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    SOUI开发者论坛
    第二十五篇:在SOUI中做事件分发处理
    第二十四篇:导出SOUI对象到LUA脚本
    第二十三篇:在SOUI中使用LUA脚本开发界面
    第四章:为妹子镶上璀璨的珠宝
    第三章:为妹子重塑婀娜身段
    第二章:美丽的幻想灰飞烟灭
    第一章:描绘妹子的靓影
    拥抱ARM妹子 序章!ARM妹子~~ 哥我来啦!
    一个比较方便的关闭进程函数
  • 原文地址:https://www.cnblogs.com/Comfortable/p/8796424.html
Copyright © 2011-2022 走看看