CSU-ACM2017暑期训练1-Debug与STL C - Languages

C - Languages

The Enterprise has encountered a planet that at one point had been inhabited. The only remnant from the prior civilization is a set of texts that was found. Using a small set of keywords found in various different languages, the Enterprise team is trying to determine what type of beings inhabited the planet.

Input

The first line of input will be N (1 ≤ N ≤ 100), the number of different known languages. The next N lines contain, in order, the name of the language, followed by one or more words in that language, separated with spaces. Following that will be a blank line. After that will be a series of lines, each in one language, for which you are to determine the appropriate language. Words consist of uninterrupted strings of upper or lowercase ASCII letters, apostrophes, or hyphens, as do the names of languages. No words will appear in more than one language. No line will be longer than 256 characters. There will be at most 1000 lines of sample text. Every sample text will contain at least one keyword from one of the languages. No sample text will contain keywords from multiple languages. The sample text may contain additional punctuation (commas, periods, exclamation points, semicolons, question marks, and parentheses) and spaces, all of which serve as delimiters separating keywords. Sample text may contain words that are not keywords for any specific language. Keywords should be matched in a case-insensitive manner.

Output

For each line of sample text that follows the blank line separating the defined languages, print a single line that identifies the language with which the sample text is associated.

Sample Input

4
Vulcan throks kilko-srashiv k'etwel
Romulan Tehca uckwazta Uhn Neemasta
Menk e'satta prah ra'sata
Russian sluchilos

Dif-tor heh, Spohkh. I'tah trai k'etwel
Uhn kan'aganna! Tehca zuhn ruga'noktan!

Sample Output

Vulcan
Romulan

将每种语言中的单词作为键,语言的名字作为值,存入map即完成了字典;
题目还要求忽略作为分隔符的标点 “,” “.” “;” “?” “!” “(” “)” ” “。为了实现这一目标,先读入Input中所述的字符串,再将上述除空格外的标点全部替换成空格,使得字符串中的单词全部都是由空格隔开的,最后用字符串流将单词逐一读取,检查是否在字典中存在匹配,若匹配,输出对应的值就行了。

#include <iostream>
#include <string>
#include <map>
#include <stdio.h>
#include <cctype>
#include <sstream>
using namespace std;
void deladdpunct(string &str){
    //函数名没意义,实际是将字符串改为小写用的

    for(int i = 0; i < str.length(); i++){
        str[i] = tolower(str[i]);
    }
}
int main(){

    map<string,string> dict;
    int T;
    string lang, examp;
    char sep;
    cin >> T;
    for(int t = 0; t < T; t++){
        cin >> lang;
        sep = getchar();
        while(sep != '\n'){//回车后读入下一种语言
            cin >> examp;
            deladdpunct(examp);//--+
            dict[examp] = lang;//  +--忽略大小写保存键值对
            sep = getchar();   //--+
        }
    }
    string text;
    string wholeLine;
    bool found = false;
    while(getline(cin, wholeLine)){
        for(int i = 0; i < wholeLine.length(); i++)
            if(wholeLine[i] == ',' || wholeLine[i] == ';' || wholeLine[i] == '.' || wholeLine[i] == '(' || wholeLine[i] == ')' || wholeLine[i] == '!' || wholeLine[i] == '?')
                wholeLine[i] = ' ';
        stringstream ss(wholeLine);//用sstream逐个读入单词
        while(ss >> text){
            deladdpunct(text);
            if(found == false){    //①
                if(dict.count(text)){
                    cout << dict[text] << endl;
                    found = true;
                }
            }
        }
        if(sep == '\n'){           //②
            found = false;         //①和②可以简化,实际上就是循环尾将found初始化为false,
        }                          //并且内层循环只在found为false时执行。不必写明
    }                              //sep=='\n'时才改为false;也不必单独在内层循环中
    return 0;                      //判断found的真假,直接在循环条件中写明就行了。
}                                  //至于②的条件总是满足,是因为读入字典时,字典以回车结束,
                                   //此后再没改变过sep的值,所以sep总是保存'\n'的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值