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树
不知道是不是第一次写情书的缘故,这。。。

5c0bae02d15eb.png

你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;
}

转载于:https://www.cnblogs.com/olinr/p/10088838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值