首页 > ACM题库 > HDU-杭电 > HDU 1606 Excuses, Excuses![解题报告] C++
2013
12-12

HDU 1606 Excuses, Excuses![解题报告] C++

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 to your program will consist of multiple sets of data.

Line 1 of each set will contain exactly two integers. The first number (1 <= K <= 20) defines the number of keywords to be used in the search. The second number (1 <= E <= 20) 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 (1 <= L <= 20) 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.

输出:

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 occurrence 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.

样例输入:

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?

样例输出:

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!

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;
struct Info
{
    string s;
    int n;
};
bool comp(const Info &a,const Info &b)
{
    if(a.n!=b.n) return a.n>b.n;
    else return a.n>b.n;
}
int main()
{
    vector<string>v;//保存关键词
    vector<Info>vv;//保存语句行结构体
    char ss[71];
    string s,t;
    int n,m,line=0;
    Info info;
    while(cin>>m>>n)
    {
        v.clear();
        vv.clear();
        line++;//案例号
        //读入关键词
        for(int i=0;i<m;i++)
        {
            cin>>s;
            v.push_back(s);
        }
        //忽略空行,这句至关重要
        //因为cin读入单项后,仍然有一个回车符留在本行
        //这样,直接转入cin.getline()读入一整行,第一次读入的是一个空行
        cin.getline(ss,71);
        for(int i=0;i<n;i++)
        {
            cin.getline(ss,71);
            s=ss;
            //如果是大写字母则转换为小写字母
            for(int j=0;j<s.size();j++)
                if(s[j]>='A'&&s[j]<='Z')
                    s[j]+=32;
            info.n=0;
            for(int j=0;j<v.size();j++)
                for(int k=0;k<s.size()-v[j].size();k++)
                {
                    //取出一个字串
                    t="";
                    int p;
                    for(p=k;p<k+v[j].size();p++)
                        t+=s[p];
                    //如果子串在最开头,子串后一位置都不能是字母
                    if(k==0&&t==v[j]&&(s[p]<'a'||s[p]>'z'))
                        info.n++;
                    //如果子串出现在其他位置,前后位置不能是字母
                    else if(t==v[j]&&(s[p]<'a'||s[p]>'z')&&(s[k-1]<'a'||s[k-1]>'z'))
                        info.n++;
                }
            info.s=ss;
            vv.push_back(info);
        }
        sort(vv.begin(),vv.end(),comp);
        cout<<"Excuse Set #"<<line<<endl;
        for(int i=0;i<vv.size();i++)
        {
            if(i!=0&&vv[i].n<vv[i-1].n) break;
            else cout<<vv[i].s<<endl;
        }
        cout<<endl;
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/z690933166/article/details/8654617


  1. 第一题是不是可以这样想,生了n孩子的家庭等价于n个家庭各生了一个1个孩子,这样最后男女的比例还是1:1

  2. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])

  3. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。