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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 实时车辆行人多目标检测与跟踪系统-上篇(UI界面清新版,Python代码)

    实时车辆行人多目标检测与跟踪系统-上篇(UI界面清新版,Python代码)摘要:本文详细介绍如何利用深度学习中的YOLO及SORT算法实现车辆、行人等多目标的实时检测和跟踪,并利用PyQt5设计了清新简约的系统UI界面,在界面中既可选择自己的视频、图片文件进行检测跟踪,也可以通过电脑自带的摄像头进行实时处理,可选择训练好的YOLOv3/v4等模型参数。该系统界面优美、检测精度高,功能强大,设计有多目标实时检测、跟踪、计数功能,可自由选择感兴趣的跟踪目标。博文提供了完整的Python程序代码和使用教程,适合新入门的朋友参考,完整代码资源文件请转至文末的下载链接。

    2022年10月13日
    0
  • PHP如何解决网站大流量与高并发的问题(二)「建议收藏」

    PHP如何解决网站大流量与高并发的问题(二)

    2022年2月9日
    159
  • matlab 计算变异系数,变异系数法求权重matlab代码

    matlab 计算变异系数,变异系数法求权重matlab代码《变异系数法求权重matlab代码》由会员分享,可在线阅读,更多相关《变异系数法求权重matlab代码(1页珍藏版)》请在读根文库上搜索。1、变异系数法求权重matlab代码clear;clc;data1,header1=xlsread(statistic1.xlsx,ECO);%必须将statistic.xlsx至于默认文件下,或者给出完整路径data2,header2=xl…

    2022年4月28日
    114
  • 证书签名验签以及加密解密

    证书签名验签以及加密解密签名,验签的理解(转) 一、公钥加密假设一下,我找了两个数字,一个是1,一个是2。我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥。我有一个文件,不能让别人看,我就用1加密了。别人找到了这个文件,但是他不知道2就是解密的私钥啊,所以他解不开,只有我可以用数字2,就是我的私钥,来解密。这样我就可以保护数据了。我的好朋友x用我的公钥1加密了字…

    2022年5月2日
    217
  • Android传感器_传感器网络的基本功能

    Android传感器_传感器网络的基本功能前言Android系统提供了对传感器的支持,如果手机的硬件提供了这些传感器的话,那么我们就可以通过代码获取手机外部的状态。比如说手机的摆放状态、外界的磁场、温度和压力等等。对于我们开发者来说,开发传感器十分简单。只需要注册监听器,接收回调的数据就行了,下面来详细介绍下各传感器的开发。使用第一步//获取传感器管理对象SensorManagermSensorManager=…

    2022年9月29日
    1
  • jsoup 1.5.2 发布,超棒的 HTML 解析器

    jsoup 1.5.2 发布,超棒的 HTML 解析器

    2021年8月10日
    59

发表回复

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

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