• COGS 1176. [郑州101中学] 月考


    ★   输入文件:mtest.in   输出文件:mtest.out   简单对比
    时间限制:1 s   内存限制:128 MB

    【题目描述】

    在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时
     间学校就此事做了全校通报. 现已知在当天校会时间有总共N个同学听到了有关Bugall的处分决定.
     
     Bugall同学在铁一有M个朋友,这M个人中有的可能听到了当天的处分决定,有的可能没
     有听到,现在Bugall同学想知道他有几个朋友听到了当天的处分通报.

    【输入格式】

    第一行为一个整数N,从第2行到N+1行,每行用一个长度不超过200的字符串表示
     一个人的名字.
      第N+2行为一个整数M,从第N+3行到N+M+2行,每行用一个长度不超过200的字符
     串表示Bugall同学一个朋友的名字.

    【输出格式】

    输出有几个Bugall同学的铁一朋友在当天的校会时间听到了Bugall处分通报.保证不重名。

    【样例输入】

    3
    Dazui
    Erge
    Dapigu
    2
    Varpro
    Erge
    

    【样例输出】

    1

    本来想用这题练习trie树,结果码了半天没码出来。。十分尴尬。。
    然后用map过了 233
    求路过的大佬帮忙改下trie树代码。。
    屠龙宝刀点击就送
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <cstdio> 
    #include <map>
    
    using namespace std;
    
    map<string,int>q;
    int n,ans;
    string name;
    int main()
    {
        freopen("mtest.in","r",stdin);
        freopen("mtest.out","w",stdout);
        scanf("%d",&n);
        for(;n--;)
        {
            cin>>name;
            q[name]=1;
        }
        scanf("%d",&n);
        for(;n--;)
        {
            cin>>name;
            if(q[name]) ans++;
        }
        printf("%d",ans);
        return 0;
    }
    
    
    #include <cstring>
    #include <cstdio>
    
    struct node
    {
        int Count;
        node *next[100];
        
    }*root;
    int ans,n;
    node *create_cn()
    {
        node *tp=new node;
        memset(tp->next,0,sizeof(tp->next));
        tp->Count=0;
        return tp;
    }
    void insert(char *word)
    {
        node *p=root;
        char *q=word;
        while(*q)
        {
            int id=*q-'A';
            if(p->next[id]==NULL)
                p->next[id]=create_cn();
            p=p->next[id];
            ++q;
            p->Count++;
        }
    }
    bool search(char *word)
    {
        node *p=root;
        char *q=word;
        while(*q)
        {
            int id=*q-'A';
            p=p->next[id];
            q++;
            if(p==NULL) return 0; 
        }
        return p->Count;
    }
    int main()
    {
        freopen("mtest.in","r",stdin);
        freopen("mtest.out","w",stdout);
        root=create_cn();
        scanf("%d",&n);
        char name[301];
        for(;n--;)
        {
            scanf("%s",name);
            insert(name);
        }
        scanf("%d",&n);
        for(;n--;)
        {
            scanf("%s",name);
            if(search(name)) ans++;
        }
        printf("%d",ans);
        return 0;
    }
    失败的trie树
     
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    Nodejs下载和第一个Nodejs示例
    永久关闭Win10工具栏的TaskbarSearch控件
    对称加密,非对称加密,散列算法,签名算法
    【转】TTL和RS232之间的详细对比
    zlg核心板linux系统中查看系统内存等使用信息
    Power BI后台自动刷新数据报错 The operation was throttled by Power BI Premium because there were too many datasets being processed concurrently.
    剪切板和上传文件内容获取
    CSS, LESS, SCSS, SASS总结
    文字程序
    electron 打包“ERR_ELECTRON_BUILDER_CANNOT_EXECUTE”
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6764164.html
Copyright © 2020-2023  润新知