Uva--409 (字符串处理)

2014-05-30 17:10:28

题意:找匹配单词,简单题。本来想用C++ string和istringstream,WA了- -,果断用朴素算法。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

void Lowcase(char *p){
    int len = strlen(p);
    for(int i = 0; i < len; i++){
        if(p[i] >= 'A' && p[i] <= 'Z'){
            p[i] += ('a' - 'A');
        }
    }
}
int main(){
    int ca = 0,k,e,i,j;
    char earr[75][75],karr[25][75],estr[25][75];
    int earr_cnt[25];
    while(cin >> k >> e){
        memset(earr_cnt,0,sizeof(earr_cnt));
        for(i = 0; i < k; ++i){
            cin >> karr[i];
            Lowcase(karr[i]);
        }
        getchar();
        for(i = 0; i < e; ++i){
            gets(estr[i]);
            int flag = 0,cnt = 0,num = 0;
            for(j = 0; j < strlen(estr[i]); ++j){
                if((estr[i][j] >= 'a' && estr[i][j] <= 'z') || (estr[i][j] >= 'A' && estr[i][j] <= 'Z')){
                    flag = 1;
                    earr[num][cnt++] = estr[i][j];
                }
                else{
                    if(flag){
                        flag = 0;
                        earr[num][cnt] = '\0';
                        Lowcase(earr[num]);
                        cnt = 0;
                        for(int tj = 0; tj < k; ++tj){
                            if(strcmp(earr[num],karr[tj]) == 0){
                                ++earr_cnt[i];
                            }
                        }
                        ++num;
                    }
                }
            }
        }
        int maxn = 0;
        for(i = 0; i < e; ++i){
            maxn = max(maxn,earr_cnt[i]);
        }
        ++ca;
        cout << "Excuse Set #" << ca << endl;
        for(i = 0; i < e; ++i){
            if(earr_cnt[i] == maxn){
                cout << estr[i] << endl;
            }
        }
        cout << endl;
    }
    return 0;
}

 

posted @ 2014-05-30 17:12  Naturain  阅读(103)  评论(0编辑  收藏  举报