CSU 1826 : Languages(map)

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

题目大意是输入一定量的词汇库(字符串)(一种语言名和多种词汇呈映射关系)。再输入多组句子,其中每一句有且只有一种语言的词汇。将这句话的语言打印出来(特殊字符均为分隔符,字符不区分大小写)
可以用STR库中的map映射做,用getline整段文本,再用stringstream字符串流分割映射。最后枚举用count()即可。
源代码如下:

#include<iostream>
#include<map>
#include<string>
#include<sstream>

using namespace std;

int main() {
int t;
cin>>t;
string a,b,c;
map<string,string> m;
getchar();
for(int i=0;i<t;i++)
{
getline(cin,a);
stringstream txt(a);
txt>>b;
while(txt>>c)
{
for(int j=0;j<c.size();j++)
{
if(c[j]>='A'&&c[j]<='Z')
c[j]+=32;
}
m[c]=b;
}
}
while(getline(cin,a))
{
for(int i=0;i<a.size();i++)
if(a[i]==','||a[i]=='.'||a[i]=='!'||a[i]==';'||a[i]=='?'||a[i]=='('||a[i]==')')
a[i]=' ';
stringstream txt(a);
while(txt>>c)
{
for(int i=0;i<c.size();i++)
{
if(c[i]>='A'&&c[i]<='Z')
c[i]+=32;
}
if(m.count(c))
{
cout<<m[c]<<endl;
break;
}
}
}

return 0;
}
posted @ 2017-07-26 14:14  西北会法语  阅读(284)  评论(0编辑  收藏  举报