正文

缩写匹配2007-09-06 12:54:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/29161.html

分享到:

Campus Buildings
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Problem description
Most campuses have a lot of named buildings, and as the names tend to be rather long, the class schedules have abbreviations. At USC, it seems that the abbreviations have become used so widely that most people don't even remember the full name of the building. SAL, PHE, OHE. To a newcomer, this can be quite confusing. So perhaps, we ought to write a little program that finds out which building could be meant by abbreviations.

Here's how we'll do it: you will be given a list of building names, and a building abbreviation, such as SAL or FRSSC. The abbreviation matches the building name if all of its letters appear, in this order, in the building name (no letter can be matched twice). So, for instance, SAL matches "SALvatori", or "Student Aerospace Laboratory", or "univerSity of southern cALifornia". It does not match "angeles", as the letters are in the wrong order. For the comparison, we will ignore case, so 'S' and 's' are the same letter.

Input
The first line contains a number K ≥ 1, which is the number of input data sets in the file. This is followed by K data sets of the following form:

The first line of the data set contains the number n of buildings, 1 ≤ n ≤ 100. This is followed by n lines, each containing the name of a building, consisting of only uppercase and lowercase letters and white space. Finally, the building code will follow on a line by itself.

Output
For each data set, first output "Data Set x:" on a line by itself, where x is its number. Then, output all of the building names that match the building code, each on its own line, and in the order in which they appeared in the initial list.

Sample Input
2
3
studENt aeRoSpace laBoratory
salVAtori
angeles
SAL
2
SaLvatori
Science LibrarieS
SSL
Sample Output
Data Set 1:
studENt aeRoSpace laBoratory
salVAtori
Data Set 2:

/// 初学者题目

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(){
    vector<string> strVect;
    strVect.reserve(100);
    int n,num;
    cin>>n;
    for(num=1; num<=n; num++){
        int m,i,j,k;
        string str;
        cin>>m;
        cin.ignore();
        for(i=0; i<m; i++){
            getline(cin,str);
            strVect.push_back(str);
        }
        getline(cin,str);
        cout<<"Data Set "<<num<<":"<<endl;
        for(j=0; j<strVect.size(); j++){
            for(k=i=0; i<str.size() && k<strVect[j].size(); k++){
                if(tolower(str[i])==tolower(strVect[j][k]))
                    i++;
            }
            if(i>=str.size())
                cout<<strVect[j]<<endl;
        }
        strVect.resize(0);
    }
    return 0;
}

阅读(2875) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册