poj 1598 Excuses, Excuses!

//这题有两点需要注意的:1.有可能excuses中存在着大写字母,这就需要将其大写字母转换为小写字母再比较
//2.keysword不能是excuses中的字串,而是一个单词,具体见下面的样例! 
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

struct Info
{
       int c;
       string str;
};

bool mycmp(Info a, Info b)
{
     return a.c > b.c;
}

int main()
{
    int i, j, k, e, pos, len, tc = 0, c;
    string keysword[25];
    Info excuses[25], tmp[25];
    while (cin >> k >> e){
          tc++;
          //keysword和excuses的输入 
          for (i = 0; i < k; i++)
               cin >> keysword[i];
          cin.get();
          for (i = 0; i < e; i++){
               getline(cin, excuses[i].str);
               excuses[i].c = 0;
               tmp[i].str = excuses[i].str;
               tmp[i].c = excuses[i].c;
          }
          
          //讲excuses中的不是字母的转换为空格,而大写字母转换为小写,主要为后面的进行统一的比较 
          for (i = 0; i < e; i++){
              len = excuses[i].str.length();
              for (j = 0; j < len; j++){
                  if (isalpha(excuses[i].str[j]))
                      excuses[i].str[j] = tolower(excuses[i].str[j]);
                  else
                      excuses[i].str[j] = ' ';
              }
          }
          
          //找出keysword中excuses中的位置,如果是一个单词的情况下就符合情况,如果只是excuses中的一个字串的情况就不符合情况! 
          for (i = 0; i < e; i++){
              for (j = 0; j < k; j++){
                  pos = excuses[i].str.find(keysword[j]);
                  if (pos >= 0){
                      pos = pos + keysword[j].length();
                      if (excuses[i].str[pos] == ' ')
                          tmp[i].c++;
                  }
              }
          }
          
          sort(tmp, tmp+e, mycmp);
          
          cout << "Excuse Set #" << tc << endl;
          cout << tmp[0].str << endl;
          i = 1;
          while (tmp[i].c == tmp[0].c && i < e){
                cout << tmp[i].str << endl;
                i++;
          }
          cout << endl;
    }
    
    system("pause");
}


/*
2 2
aa
aba
aaa abaa
aba aaaa abaaad
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值