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
题目大意
输入一个数字n表示语言总数,后面n行字符表示一种语言,每一行的第一个单词为这一种语言的名字,后面的都为这一种语言的关键词(除空格外无其他符号)
输入若干行字符串,包含逗号、句点、感叹号、分号、问号和括号和空格,如果其中有前面语言的关键词的话就说明属于该种语言,输出该语言的名字(配对不区分大小写)
解题思路
我是想将每一个关键词和其对应的语言联系起来,且便于访问,就想到了map容器,将每一种语言的关键词作为键值,语言名字为值储存(转化为小写/大写),最后配对语言输入的时候将特殊符号转为空格(同时转化为小写/大写)…
AC代码
#include<algorithm>
#include<iostream>
#include<sstream>
#include<map>
#include<string>
using namespace std;
int main()
{
int n;
cin>>n;
string line,language,key;
map<string,string> ww;
while(n--)
{
cin>>language;
getline(cin,line);
stringstream word(line);
while(word>>key)
{
for(int i=0;i<key.size();++i)
{
key[i]=tolower(key[i]);
}
ww[key]=language;
}
}
while(getline(cin,line))
{
for(int i=0;i<line.size();++i)
{
if(line[i]==','||line[i]=='.'||line[i]==';'||line[i]=='!'||line[i]=='?'||line[i]=='('||line[i]==')')
line[i]=' ';
else if(line[i]>='A'&&line[i]<='Z')
line[i]=tolower(line[i]);
}
stringstream word(line);
while(word>>key)
{
if(ww.count(key))
{
cout<<ww[key]<<endl;
break;
}
}
}
}