首页 > 代码库 > 小白书训练-Excuses, Excuses!m

小白书训练-Excuses, Excuses!m

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=350

题意:没读懂题让人WA到死啊啊啊!大体的意思很简单,就是给你n个单词,m个句子,问包含最多单词的句子是什么,要注意的是,找的是单词,不是字符串,说白了,就是被空格和标点符号明确分开的字符片段,不能直接上find><!!!需要对整个句子划分,然后对比查找。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

struct ANS
{
    int num;
    int no;
} ans[100];

int cmp(ANS a,ANS b)
{
    if(a.num == b.num)
        return a.no < b.no;
    return a.num > b.num;
}

int main()
{
    int n,m;

    string word[100];
    string se[100],xse[100];
    int NO = 1;
    while(cin >> n >> m)
    {
        cin.get();
        memset(ans,0,sizeof(ans));
        for(int i = 0; i < n; i++)
        {
            cin >> word[i];
            cin.get();
            for(string::iterator p = word[i].begin(); p < word[i].end(); p++)
                *p = tolower(*p);
        }

        for(int i = 0; i < m; i++)
        {
            getline(cin,se[i]);
            xse[i] = se[i];
            for(string::iterator p = xse[i].begin(); p < xse[i].end(); p++)
            {
                if(isalpha(*p))
                    *p = tolower(*p);
                else
                    *p = ' ';
            }
        }

        for(int i = 0; i < m; i++)
        {
            ans[i].no = i;
            int s = 0,e = 0;
            for(string::iterator p = xse[i].begin(); p < xse[i].end(); p++,e++)
            {
                string tmp;
                while(*p != ' ' && p < xse[i].end())
                {
                    tmp += *p;
                    p++;
                }
                for(int j = 0; j < n; j++)
                {
                    //cout << word[j] << '-' << tmp << endl;
                    if(word[j] == tmp)
                        ans[i].num++;
                }

            }

        }

        sort(ans,ans + m,cmp);
        int imax = ans[0].num;

        cout << "Excuse Set #" << NO++ << endl;
        for(int i = 0; i < m; i++)
            if(ans[i].num == imax)
                cout << se[ans[i].no] << endl;

        cout << endl;
    }
    return 0;
}
梦续代码:http://www.hypo.xyz

小白书训练-Excuses, Excuses!m