您的位置:首页 > 其它

2017湖南多校第一场-L(1011): Languages

2017-03-22 16:31 239 查看

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 di erent languages, the Enterprise team is trying to determine what type of beings

inhabited the planet.

Input

The rst line of input will be N (1  N  100), the number of di erent 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 speci c language.

Keywords should be matched in a case-insensitive manner.

Output

For each line of sample text that follows the blank line separating the de ned languages, print a

single line that identi es the language with which the sample text is associated.

Sample Input Sample Output

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!

Vulcan

Romulan

解题思路:用map将单词映射为语言,使用stringstream,提高读入效率

使用字典树查询也可以~

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<sstream>
#include<map>
using namespace std;

int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
int T;
cin>>T;
string text,lan,word;
map<string,string> m;
getchar();
for(int i=0;i<T;i++)
{
getline(cin,text);
stringstream ss1(text);
ss1>>lan;
while(ss1>>word)
{
for(int i=0;i<word.size();i++)
{
if(word[i]>='A'&&word[i]<='Z')
word[i]+=32;
}
m[word]=lan;
}
}
while(getline(cin,text))
{
string x;
for(int i=0; i<text.size(); i++)
{
if (text[i]==','||text[i]=='.'||text[i]=='!'||text[i]==';'||text[i]=='?'||text[i]=='('||text[i]==')')
text[i]=' ';
}
stringstream ss2(text);
while(ss2>>x)
{
for(int i=0; i<x.size(); i++)
{
if(x[i]>='A'&&x[i]<='Z')
x[i]+=32;
}
if(m.count(x))
{
cout << m[x] << endl;
break;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: