uva 409 - Excuses, Excuses!

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=350 题意:给出k个key word,e个借口…找出包含key word最多的借口,即为最坏的借口。匹配补区分大小写&&同一个key word算多次。 思路:需要注意的是因为不区分大小写,需要都转化成大写或者小写。。但是输出的时候要输出原始的。。所以要另外存一份。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年01月22日 星期五 17时58分02秒
  4File Name :code/uva/409.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33string str[50];
 34int k,e;
 35int num[50];
 36set<string>se;
 37
 38struct node
 39{
 40    string exc;
 41    string ori;
 42    int num;
 43    bool operator<(node b)const
 44    {
 45	return num>b.num;
 46    }
 47}ex[50];
 48
 49int main()
 50{
 51	#ifndef  ONLINE_JUDGE 
 52	freopen("code/in.txt","r",stdin);
 53  #endif
 54	int cas = 0 ;
 55	while (scanf("%d %d",&k,&e)!=EOF)
 56	{
 57	    se.clear();
 58	    ms(num,0);
 59	    printf("Excuse Set #%d\n",++cas);
 60
 61	    for ( int i = 0 ; i < k ; i++)
 62	    {
 63		cin>>str[i];
 64		int len = str[i].length();
 65		for ( int j = 0 ; j < len ;j++)
 66		{
 67		    str[i][j] = tolower(str[i][j]);
 68		}
 69		se.insert(str[i]);
 70	    }
 71	    getchar();
 72//	    for ( int i = 0 ; i < k ;i++) cout<<"str[i]:"<<str[i]<<endl;
 73	    for ( int i = 0 ; i < e ; i++)
 74	    {
 75		getline(cin,ex[i].exc);
 76		ex[i].ori=ex[i].exc;
 77		int len = ex[i].exc.length();
 78		for ( int j = 0 ; j < len ; j++)
 79		{
 80		    ex[i].exc[j] = tolower(ex[i].exc[j]);
 81		    if (ex[i].exc[j]<'a'||ex[i].exc[j]>'z')
 82			ex[i].exc[j]=' ';  //把非字母部分都转化成空格.
 83		}
 84	    }
 85
 86//	    for ( int i = 0 ; i < e ; i++) cout<<"exc[i]:"<<ex[i].exc<<endl;
 87
 88	    int ans = -1;
 89
 90	    for ( int i = 0 ; i < e ;i++)
 91	    {
 92		int cnt = 0 ;
 93		string cpy = ex[i].exc;
 94		int pos = 0 ;
 95		while (pos!=-1)
 96		{
 97		    pos = cpy.find(' ');
 98		    string tmp = cpy.substr(0,pos-0);
 99		    if (se.count(tmp)==1) cnt++;
100		    cpy.erase(0,pos+1);
101		    //cout<<i<<"cpy:"<<cpy<<cpy.length()<<endl;
102		}
103
104		ex[i].num=cnt;
105	    }
106//	    for ( int i = 0 ; i < e ; i++) cout<<"num[i]:"<<ex[i].num<<endl;
107	    sort(ex,ex+e);
108	    for ( int i = 0 ; i < e ; i++)
109	    {
110		if (ex[i].num==ex[0].num)
111		{
112		    cout<<ex[i].ori<<endl;
113		}
114	    }
115	    cout<<endl;
116	}
117
118
119  #ifndef ONLINE_JUDGE  
120  fclose(stdin);
121  #endif
122    return 0;
123}