1. 程式人生 > >CSU 1826 Languages

CSU 1826 Languages

was text planet col round limit out lin wid

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;
}

CSU 1826 Languages