UVa409_Excuses, Excuses!(小白书字符串专题)[通俗易懂]

UVa409_Excuses, Excuses!(小白书字符串专题)

大家好,又见面了,我是全栈君。

解题报告

题意:

找包括单词最多的串。有多个按顺序输出

思路:

字典树爆。

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
int k,e,num[100],cnt;
struct node
{
    int v;
    node *next[26];
};
node *newnode()
{
    node *p=new node;
    p->v=0;
    int i;
    for(i=0;i<26;i++)
        p->next[i]=NULL;
    return p;
}
void addnode(node *root,char *str)
{
    int i,l=strlen(str);
    node *p=root;
    for(i=0;i<l;i++)
    {
        int t=str[i]-'a';
        if(p->next[t]==NULL)
        p->next[t]=newnode();
        p=p->next[t];
    }
    p->v=1;
}
int findd(node *root,char *str)
{
    node *p=root;
    int i,l=strlen(str);
    for(i=0;i<l;i++)
    {
        int t=str[i]-'a';
        if(p->next[t]==NULL)
        return 0;
        p=p->next[t];
    }
    return p->v;
}
int main()
{
    char ch[100],str[100][100];
    int i,j,m,kk=1;
    while(~scanf("%d%d",&k,&e))
    {
        node *root=newnode();
        for(i=0;i<k;i++)
        {
            scanf("%s",ch);
            addnode(root,ch);
        }
        getchar();
        int maxx=0;
        for(i=0;i<e;i++)
        {
            fgets(str[i],100,stdin);
            int l=strlen(str[i]);
            m=0;
            cnt=0;
            for(j=0;j<l;j++)
            {
                if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z'))
                {
                    if(str[i][j]>='A'&&str[i][j]<='Z')
                    ch[m++]=str[i][j]+32;
                    else ch[m++]=str[i][j];
                }
                else
                {
                    ch[m]=0;
                    m=0;
                    if(findd(root,ch))
                    cnt++;
                }
            }
            if(maxx<cnt)
            maxx=cnt;
            num[i]=cnt;
        }
        printf("Excuse Set #%d\n",kk++);
        for(i=0;i<e;i++)
        {
            if(num[i]==maxx)
            printf("%s",str[i]);
        }
        printf("\n");
    }
    return 0;
}

 Excuses, Excuses! 

Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.

Input

Input to your program will consist of multiple sets of data.

  • Line 1 of each set will contain exactly two integers. The first number ( tex2html_wrap_inline30 ) defines the number of keywords to be used in the search. The second number ( tex2html_wrap_inline32 ) defines the number of excuses in the set to be searched.
  • Lines 2 through K+1 each contain exactly one keyword.
  • Lines K+2 through K+1+E each contain exactly one excuse.
  • All keywords in the keyword list will contain only contiguous lower case alphabetic characters of length L ( tex2html_wrap_inline42 ) and will occupy columns 1 through L in the input line.
  • All excuses can contain any upper or lower case alphanumeric character, a space, or any of the following punctuation marks [SPMamp“.,!?&] not including the square brackets and will not exceed 70 characters in length.
  • Excuses will contain at least 1 non-space character.

Output

For each input set, you are to print the worst excuse(s) from the list.

  • The worst excuse(s) is/are defined as the excuse(s) which contains the largest number of incidences of keywords.
  • If a keyword occurs more than once in an excuse, each occurrance is considered a separate incidence.
  • A keyword “occurs” in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non-alphabetic character or a space.

For each set of input, you are to print a single line with the number of the set immediately after the string “Excuse Set #“. (See the Sample Output). The following line(s) is/are to contain the worst excuse(s) one per line exactly as read in. If there is more than one worst excuse, you may print them in any order.

After each set of output, you should print a blank line.

Sample Input

5 3dogatehomeworkcanarydiedMy dog ate my homework.Can you believe my dog died after eating my canary... AND MY HOMEWORK?This excuse is so good that it contain 0 keywords.6 5superhighwaycrazythermonuclearbedroomwarbuildingI am having a superhighway built in my bedroom.I am actually crazy.1234567890.....,,,,,0987654321?????!!!!!!There was a thermonuclear war!I ate my dog, my canary, and my homework ... note outdated keywords?

Sample Output

Excuse Set #1Can you believe my dog died after eating my canary... AND MY HOMEWORK?

Excuse Set #2I am having a superhighway built in my bedroom.There was a thermonuclear war!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115320.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 记录ubuntu16.04版本安装过程中遇到的问题

    1.ubuntu源替换https://blog.csdn.net/m0_37924639/article/details/787796492.windows与vmwareubuntu文件夹共

    2021年12月28日
    49
  • 齐次坐标讲解_齐次方程为什么叫齐次

    齐次坐标讲解_齐次方程为什么叫齐次齐次坐标的表示根据《向量几何在游戏编程中的使用6》中关于基的概念。对于一个向量v以及基oabc,可以找到一组坐标(v1,v2,v3),使得v=v1a+v2b+v3c(1)而对于一个点p,则可以找到一组坐标(p1,p2,p3),使得p–o=p1a+p2b+p3c(2)从上面对向量和点的表达,我们可以看出为了在坐标系中表示一个点(如p),我们把点的位置看作是对这个基的原点o所进行的一个位移,即一个向量——p–o(有的书中把这样的向.

    2025年6月21日
    4
  • 程序员日记

    程序员日记混迹程序圈yi

    2022年5月25日
    41
  • mysql集群之MYSQL CLUSTER

    mysql集群之MYSQL CLUSTERMYSQLCLUSTER

    2022年7月1日
    24
  • 使用Zookeeper实现选举

    使用Zookeeper实现选举

    2021年6月7日
    216
  • JavaScript、js文件、Node.js、静态文件

    JavaScript、js文件、Node.js、静态文件1、JavaScript认知JavaScript(简称“JS”)是一种解释型的脚本语言。广泛用于Web应用开发,对页面事件做出响应。通常JavaScript脚本是通过嵌入在HTML页面中来实现自身的功能的,也可以写成单独的js文件。JavaScript与Java名称上的近似,但是两种完全不同的语言。2、JavaScript特点2.1、动态性。JavaScript是一种采用事件驱动的脚本语言,它不需要经过Web服务器就可以对用户的输入做出响应。在访问一个网页时,鼠标在网页中进行鼠标点击或上下移、窗

    2025年6月24日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号