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

来源:互联网 发布:中山大学网络服务中心 编辑:程序博客网 时间:2024/06/11 04:02

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

4Vulcan throks kilko-srashiv k'etwelRomulan Tehca uckwazta Uhn NeemastaMenk e'satta prah ra'sataRussian sluchilosDif-tor heh, Spohkh. I'tah trai k'etwelUhn kan'aganna! Tehca zuhn ruga'noktan!

Sample Output

VulcanRomulan

将每种语言中的单词作为键,语言的名字作为值,存入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'的。
原创粉丝点击