zoukankan      html  css  js  c++  java
  • P2264 情书 Trie匹配

    (color{#0066ff}{题目描述})

    为了帮助CYY,我们定义一个量化情书好坏的标准感动值。判断感动值的方法如下:

    1.在情书的一句话中若含有给定词汇列表中的特定单词,则感动值加1,但每一单词在同一句话中出现多次感动值不叠加,不同单词不受影响。保证输入的单词不重复。

    2.每句话以英文句号定界。

    3.全文不区分大小写。

    (color{#0066ff}{输入格式})

    第一行包含一个数字n,表示导致感动值提升的词汇列表中单词的数量,随后n行是给定单词,每行一个。保证单词只包含英文字母。

    最后一行为情书正文,保证只包含以下几种字符: 英文字母、数字、空格、英文逗号、英文句号。

    (color{#0066ff}{输出格式})

    一个数字g,表示情书带来的感动值。

    (color{#0066ff}{输入样例})

    3
    love
    so
    much
    I love you so much.
    

    (color{#0066ff}{输出样例})

    3
    

    (color{#0066ff}{数据范围与提示})

    对于所有的数据,保证1 ≤ n,m,k ≤ 100,每个单词不超过50字符,全文不超过1000字符。

    (color{#0066ff}{题解})

    这题很水对不对? 随便模拟STL什么的轻松水过

    我就是想练练Trie树

    不知道是不是第一次写情书的缘故,这。。。

    你Tm玩我呢!!!!

    完美入坑!!!

    1、按题目要求,必须整个单词匹配,是原文的字串不算

    2、输入有逗号,要考虑

    3、有一个点行末没有换行符,那么这个单词合法,有换行符不合法@@@@

    4、不区分大小写!!!

    5、写指针要写构造函数或初始化!!

    6、时刻注意数组越界问题

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #define _ 0
    #define LL long long
    inline LL in()
    {
        LL x=0,f=1; char ch;
        while(!isdigit(ch=getchar()))(ch=='-')&&(f=-f);
        while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
        return x*f;
    }
    struct node
    {
        node *ch[26];
        int tag;
        node()
        {
            for(int i=0;i<=25;i++) ch[i]=NULL;
            tag=0;
        }
        void *operator new (size_t)
        {
            static node *S=NULL,*T=NULL;
            return (S==T&&(T=(S=new node[1024])+1024),S++);
        }
    };
    int n;
    char s[1200];
    int len,cnt=1;
    struct Trie
    {
        node *root;
        Trie() {root=new node();}
        void ins()
        {
            char ch;
            node *o=root;
            while(!isalpha(ch=getchar()));
            while(isalpha(ch))
            {
                ch=tolower(ch);
                int p=ch-'a';
                if(o->ch[p]==NULL) o->ch[p]=new node();
                o=o->ch[p];
                ch=getchar();
            }
            o->tag=-1;
        }
        bool have(int l,int r)
        {
            node *o=root;
            for(int i=l;i<=r;i++)
            {
                int p=s[i]-'a';
                if(!isalpha(s[i])||!o->ch[p]) return false;
                o=o->ch[p];
            }
            if(o->tag==cnt||!o->tag) return false;
            return o->tag=cnt,true;
        }
        void query()
        {
            int ans=0;
    		char ch;
            while((ch=getchar())!=EOF)
    		{
    			ch=tolower(ch);
    			if(ch==' '||ch=='.'||ch==',')
    			{	
    				ans+=have(1,len);
    				if(ch=='.') cnt++;
    				len=0; 
    				continue;
    			}
    			s[++len]=ch;
    		}
    		ans+=have(1,len);
    		printf("%d",ans);
        }
    }t;
    int main()
    {
        n=in();
        for(int i=1;i<=n;i++) t.ins();
        t.query();
        return 0;
    }
    
  • 相关阅读:
    LA 6621 /ZOJ 3736 Pocket Cube 打表+暴力
    UVA 10273
    UVA 10158 并查集的经典应用
    CodeForces 382B 数学推导
    UVA 10806 最小费用最大流
    UVA 10330 最大流
    图论:匹配与覆盖+独立集 团与支配集
    sdut oj 操作系统实验--SSTF磁盘调度算法【操作系统算法】
    【转载】单调队列学习
    poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
  • 原文地址:https://www.cnblogs.com/olinr/p/10088838.html
Copyright © 2011-2022 走看看