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


相关推荐

  • ATA考试该注意什么[通俗易懂]

    ATA考试该注意什么[通俗易懂]一、考试前将所有计算机除掉还原卡及还原软件。二、officeXp安装要用完全安装。三、服务器端尽量不要刷新所有客户端否则引起考试管理系统死机。四、拍照功能无法使用,可重新启动考试管理系统。五、服务器端无法扫描到客户端,除了服务器与客户端必须在同一网段内,可看一下客户端是否启动llistening…

    2022年7月13日
    16
  • 在Repeater中嵌套使用Repeater

    在Repeater中嵌套使用Repeater在一般的网站中浏览类别的用户控件通常都位于大多数ASP.NET页的左边,它使用户能够按类别快速的查找产品。最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加

    2022年7月1日
    21
  • 富文本编辑器汇总_移动端富文本编辑器

    富文本编辑器汇总_移动端富文本编辑器富文本编辑器:(RichTextEditor,RTE)是一种可内嵌于浏览器,所见即所得的文本编辑器。它提供类似于OfficeWord的编辑功能,方便那些不太懂HTML用户使用,富文本编辑器的应用非常广泛,它的历史与图文网页诞生的历史几乎一样长。1.TinyMCTinyMCE是一个开源的所见即所得的HTML编辑器,界面相当清新,界面模拟本地软件的风格,顶部有菜单栏。支持图片在线处理,插件多,功能非常强大,易于集成,并且拥有可定制的主题。支持目前流行的各种浏览器,它可以达到微软…

    2025年7月3日
    3
  • RecyclerView 实现横向滚动效果

    RecyclerView 实现横向滚动效果我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系统为我们提供了另一个控件RecyclerView。RecyclerView是listview之后的又一利器,它可以实现高度的定制。今天就利用RecyclerView实现我们需要的相册效果。先上一个图:主要实现就是一个RecyclerView

    2022年9月18日
    2
  • pycharm修改环境_pycharm更改环境

    pycharm修改环境_pycharm更改环境因为有时需要在Terminal中调试代码,而且调试前需要配置环境变量,虽然操作不麻烦,但用起来总不习惯.特别是项目比较多时,需要频繁进到设置里面去改.以前设置环境变量的操作如下图:Settings-Tools-Terminal-ProjectSettings-Enviromentvariables添加环境变量比如我们有一个文件env.list里面的内容如下:IN_HOST=’192.168.0.6’#数据库主机IPIN_PORT=8888#数据库端口IN_US

    2022年8月27日
    4
  • pymssql 中文乱码_pycharm输出中文乱码

    pymssql 中文乱码_pycharm输出中文乱码开门见山,先放解决问题的代码SELECTCONVERT(NVARCHAR,test_field)AStest_fieldFROMtest_tableWHEREtest_condition=’测试中文’–直接将中文字段test_field(VARCHAR)转化为NVARCHAR,其他类型同理,转换成N开头的类型接下来才是其他可能可行的解决方案:使用其他库,如pyodbc(详细信息请阅读相关文档),可参考:https://blog.csdn.net/zhaogeno1/art

    2025年6月30日
    5

发表回复

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

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