CSU 1826 Languages [map]

Description

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

Hint

Source

2013 Pacific Northwest Region Programming Contest

 

题目大意:有N种不同的语言,每种语言都有自己独特的单词。先给出语言的名称,再给出单词,然后空一行再给你一些句子,问这句子是哪种语言。

 

大致思路:用map<string,string>把每种语言的单词映射到语言的名称中,再用stringstream读入输入的句子,把其中的标点符号改为空格,这样一个句子就被拆分成几个单词,找出其中单词对应的语言名称就ok了。详见代码。

 

#include<iostream>
#include <sstream>//用stringstream所需的头文件
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<string,string> m;
int main()
{
    int N;
    m.clear();
    string name,a,b,c;
    cin>>N;
    getchar();//这行必须要有,否则输入完N后的换行符会被getline一起输入到a中
    for(int i=0;i<N;i++)
    {
        getline(cin,a);//先把要输入的语言名称和单词用用同一组string来存,不能分成两部分输入
        stringstream ss1(a);//把a放进ss1中,stringstream主要用于分割一组字符串
        ss1>>name;//用>>符号可以把ss1中的数据分别读出,这是第一个>>,所以对应的是语言名字
        while(ss1>>b)//每进行一次>>,就会读取一个单词
        {
            for(int j=0;j<b.size();j++)
                if(b[j]>='A'&&b[j]<='Z')//把大写换成小写,便于后面的比较
                    b[j]=b[j]+32;
             m[b]=name;//单词映射到语言名称
        }
    }
    while(getline(cin,c))//输入句子
    {
        string tmp;
        for(int i=0;i<c.size();i++)
        {
            if(c[i]==','||c[i]=='.'||c[i]=='!'||c[i]==';'||c[i]=='?'||c[i]=='('||c[i]==')')//把句子中的符号换成空格
                c[i]=' ';
        }
        stringstream ss2(c);
        while(ss2>>tmp)//同上
        {
            for (int i=0;i<tmp.size();i++)
                if (tmp[i]>='A'&&tmp[i]<='Z')
                    tmp[i]+=32;//改成小写
            if(m.count(tmp))//查找tmp对应的语言
            {
                cout<<m[tmp]<<endl;
                break;//因为每种单词都是自己语言独有的,所以找到一个即可
            }
        }
    }
    return 0;
}

 

posted on 2017-07-30 19:22  FTA_Macro  阅读(367)  评论(0编辑  收藏  举报

导航