uva409 Excuses, Excuses!(字符串处理)

题目戳这里
题目大意:
给出一定量的关键词与一定量的句子,查找含有关键词量最多的句子,并输出,如果是多个句子,一并输出。
注意:
1.关键词在句子中出现必须是单词的形式,关键词是ab的时候在句子中出现的合法形式应该是:ab c 或ab,c等,abc则不算在内。
2.不区分大小写。
3.同一关键词在一个句子中多次出现只算一个。

思路:
注意到以上三点,直接模拟即可,可以运用string类里的find函数进行查找。

ac代码:

/*
*Author : Flint_x 
*Created Time : 2015-04-04 15:36:49 
*File name : uva409.cpp 
*/

#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
string key[25];
string ex[25];
string ex2[25];
int k,e;
int find(int m){  
    int i, j, flag = 0;  
    for (j = 1; j <= k; j++)  
        if (ex[m].find(key[j]) != string::npos) {  
            int a = ex[m].find(key[j]);  
            if (ex[m][a - 1] < 97 || ex[m][a - 1] > 122) {  
                int length = key[j].size();  
                if (ex[m][a + length] < 97 || ex[m][a + length] > 122)  
                    flag++;  
            }  
        }
//  cout << flag;  
    return flag;  
}  
int main(){
    //freopen("input.txt","r",stdin);

    int cnt = 0 ;
    while(cin >> k >> e){
        cnt ++;
        int count[25];
        int count2[25];
        memset(count,0,sizeof(count));
        memset(count2,0,sizeof(count2));
        for(int i = 1 ; i <= k ; i++){
            cin >> key[i];
        }
//      getline(cin,ex[1]);
        getchar();
        for(int i = 1 ; i <= e ; i++){
//          char x = getchar();
//          cout << x << endl;
            getline(cin,ex[i]);
            ex2[i] = ex[i];
//          cout << ex[i] << endl;
            for(int j = 0 ; j < ex[i].length() ; j++){
                if(ex[i][j] >= 'A' && ex[i][j] <= 'Z'){
                    ex[i][j] = ex[i][j] + 32;
                }
            }

        }
        for(int j = 1 ; j <= e ; j++){
                count[j] = find(j);
                count2[j] = count[j];
        }
        sort(count + 1, count + 1 + e);
        printf("Excuse Set #%d\n",cnt) ;
        for(int i = 1 ; i <= e ; i++){
            if(count[e] == count2[i]){
//              cout << count[e];
                cout << ex2[i] << endl;
            }
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值