UVA (POJ 1598)Excuses, Excuses!

Excuses, Excuses!

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

Description

 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 (  ) defines the number of keywords to be used in the
    search. The second number (  ) 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 (  ) 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 3
dog
ate
homework
canary
died
My 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 5
superhighway
crazy
thermonuclear
bedroom
war
building
I 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 #1
Can you believe my dog died after eating my canary... AND MY HOMEWORK?

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

题意:前面给出n个特殊单词,后面给出m句话,问这m句话中特殊单词的个数最多的是哪句话?如果有两句话的特殊单词数相同按输入的顺序同时输出(因为不知道要按输入时的顺序输出,改了一天半,把所有可能犯的错全改了,就是交不上,真是醉了)

POJ的数据略水,还是说UVA的数据太硬...在UVA上WA的代码在POJ上直接Accepted。

UVA    AC代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<map>

using namespace std;

int n,m;

struct node
{
    int x,y;
} q[100010];

int main()
{
    int e = 0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        char a[10001];
        map<string,int>p;
        map<string,int>::iterator it;
        for(int i=0; i<n; i++)
        {
            scanf("%s",a);
            for(int j=0; a[j]!=‘\0‘; j++)
            {
                if(a[j]>=‘A‘ && a[j]<=‘Z‘)
                {
                    a[j] = a[j] + 32;
                }
            }
            p[a] = 1;
        }
        char b[3001][101];
        char str[3001][110];
        memset(q,0,sizeof(q));
        getchar();
        for(int i=0; i<m; i++)
        {
            gets(b[i]);
            int t = 0;
            int k = 0;
            for(int j=0; b[i][j]!=‘\0‘; j++)
            {
                if((b[i][j] >=‘a‘ && b[i][j]<=‘z‘) || (b[i][j]>=‘A‘ && b[i][j]<=‘Z‘))
                {
                    str[t][k++] = b[i][j];
                }
                else
                {
                    str[t][k] = ‘\0‘;
                    t++;
                    k = 0;
                }
            }
            str[t][k] = ‘\0‘;
            q[i].x = i;
            for(int j=0; j<=t; j++)
            {
                if((str[j][0] >=‘A‘ && str[j][0]<=‘Z‘) || (str[j][0]>=‘a‘ && str[j][0]<=‘z‘))
                {
                    for(int v=0; str[j][v]!=‘\0‘; v++)
                    {
                        if(str[j][v]>=‘A‘ && str[j][v]<=‘Z‘)
                        {
                            str[j][v] = str[j][v] + 32;
                        }

                    }
                    if((str[j][0] >=‘a‘ && str[j][0]<=‘z‘) && p[str[j]] == 1)
                    {

                        q[i].y++;
                    }
                }

            }
        }
        int max = 0;
        for(int i=0;i<m;i++)
        {
            if(max<q[i].y)
            {
                max = q[i].y;
            }
        }
        printf("Excuse Set #%d\n",++e);
        for(int i=0;i<m;i++)
        {
            if(max == q[i].y)
            {
                printf("%s\n",b[q[i].x]);
            }
        }
        printf("\n");
    }
    return 0;
}

UVA提交WA,POJ提交AC代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<map>

using namespace std;

int n,m;

struct node
{
    int x,y;
} q[100010];

int cmp(const void *a,const void *b)
{
    return (*(struct node*)a).y - (*(struct node*)b).y;
}

int main()
{
    int e = 0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        char a[10001];
        map<string,int>p;
        map<string,int>::iterator it;
        for(int i=0; i<n; i++)
        {
            scanf("%s",a);
            for(int j=0; a[j]!=‘\0‘; j++)
            {
                if(a[j]>=‘A‘ && a[j]<=‘Z‘)
                {
                    a[j] = a[j] + 32;
                }
            }
            p[a] = 1;
        }
        char b[3001][101];
        char str[3001][110];
        memset(q,0,sizeof(q));
        getchar();
        for(int i=0; i<m; i++)
        {
            gets(b[i]);
            int t = 0;
            int k = 0;
            for(int j=0; b[i][j]!=‘\0‘; j++)
            {
                if((b[i][j] >=‘a‘ && b[i][j]<=‘z‘) || (b[i][j]>=‘A‘ && b[i][j]<=‘Z‘))
                {
                    str[t][k++] = b[i][j];
                }
                else
                {
                    str[t][k] = ‘\0‘;
                    t++;
                    k = 0;
                }
            }
            str[t][k] = ‘\0‘;
            q[i].x = i;
            for(int j=0; j<=t; j++)
            {
                if((str[j][0] >=‘A‘ && str[j][0]<=‘Z‘) || (str[j][0]>=‘a‘ && str[j][0]<=‘z‘))
                {
                    for(int v=0; str[j][v]!=‘\0‘; v++)
                    {
                        if(str[j][v]>=‘A‘ && str[j][v]<=‘Z‘)
                        {
                            str[j][v] = str[j][v] + 32;
                        }

                    }
                    if((str[j][0] >=‘a‘ && str[j][0]<=‘z‘) && p[str[j]] == 1)
                    {
                        //printf("%s\n",str[j]);
                        q[i].y++;
                    }
                }

            }
        }
        qsort(q,m,sizeof(q[0]),cmp);
        printf("Excuse Set #%d\n",++e);
        printf("%s\n",b[q[m-1].x]);
        for(int i=m-2; i>=0; i--)
        {
            if(q[i].y == q[i+1].y)
            {
                printf("%s\n",b[q[i].x]);
            }
            else
            {
                break;
            }
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-08-25 15:52:54

UVA (POJ 1598)Excuses, Excuses!的相关文章

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1

HDU 1535 Invitation Cards (POJ 1511)

两次SPFA.求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换,但是这个有1000000个点,矩阵开不了. d1[]为 1~N 的最短路. 将所有边的 邻点 交换. d2[] 为 1~N 的最短路. 所有相加为 所要答案. 忧伤的是用SPFA  "HDU 1535"  AC了,但是POJ 一样的题 "POJ 1511" 就WA了. 然后强迫症犯了,不停的去测试. 题意中找到一句关键话 :Prices are positive integ

每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化

Largest Rectangle in a Histogram 题目大意: 有数个宽为1,长不定的连续方格,求构成的矩形中最大面积 /************************************************************************/ /* 思路1. 当前为n的面积如何与n-1相联系,dp[i][j]=max(dp[i-1][k]) , 0<k<=j 描述:i为方块个数,j为高度 但是此题目的数据对于高度太变态,h,1000000000 ,n,1

Power string(poj 2406)

题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len])==0 ,答案就是 len /(len-next[len]),否则答案是1:证明如下: 如果s能表示成 a^k的形式且k>1,k尽可能大,即s可以表示成aaaaaa(k个a):那么next[len]就等于k-1个a的长度:aaaaaaa  aaaaaaa那么 (len-next[len])a的长

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your

Feel Good (poj 2796)

题目大意就是在给出的串中找出一段连续数字,使得 这一段的和 乘上 这一段最小的数 的结果最大. 可以用rmq做.每次区间找当中最小的数,算出值并记录位置.然后再递推它的左右区间. 不过- -,一开始用深搜递推RE了.栈空间不够了,然后慢慢优化,最后还是ac了. 貌似这一题是用单调栈做的,还可以用查并集做...我也是醉了 具体看代码吧 1 /************************************************************************* 2 > F

昂贵的聘礼(poj 1062)

Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000金币就行了."探险家就跑到大祭司那里,向他要求皮袄或水晶球,大祭司要他用金币来换,或者替他弄来其他的东西,他可以降低价格.探险家于是又跑到其他地方,其他人也提出了类似的要求

Collecting Bugs(POJ 2096)

Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3064   Accepted: 1505 Case Time Limit: 2000MS   Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material s

Games:取石子游戏(POJ 1067)

取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者.现在给出初始的两堆石子的数目,如果轮到你先取,假设双方都采取最好的策略,问最后你是胜者还是败者