您的位置:首页 > 理论基础 > 数据结构算法

Languages<stringstream的使用+map集合的使用>

2017-07-24 16:17 405 查看
                                                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 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

题目大意:先输入n行字符串,每一个字符串的第一个值为语言名字,随后便是该语言中包含有的单词
然后,输入多条句子,处理到文件结束,判断每一条句子属于那种言。

其实思路很简单,关键就是处理给出的句子当中,怎么分割每一个单词,根据题目意思,是可以用逗号,句号,感叹号,括号,分号,问号,空格来分隔每一个单词,所以我们可以将这些分隔符统一定为空格,这样方便处理一些
然后,关于用空格分割字符串,我们可以使用strtock,strstr函数等等,参照了一下网上大佬的代码,这里用的是stringstream流来进行处理

#include<cstdio>
#include<cctype>
#include<iostream>
#include<stack>
#include<map>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
using namespace std ;
int main()  {
int n;
cin>>n;
string a,b,c;
map<string,string> q;
getchar();//这个getchar()不能省,如果省略了,相当于少输入了一行数据,因为getline遇到回车直接跳下一步了
for(int i=0;i<n;i++){
getline(cin,a);
stringstream txt(a);
txt>>b;
while(txt>>c){//这里也是之前没考虑到的问题,一定要讲所有的单词同一大小写,注意,是单词统一大小写,语言的名字不管他
for(int i=0;i<c.size();i++)
{
if(c[i]>='A'&&c[i]<='Z')
c[i]+=32;
}

q[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 txt1(a);
string d;
while(txt1>>d){
for(int i=0;i<d.size();i++)//每一句话里面的单词也都要同一大小写.
{
if(d[i]>='A'&&d[i]<='Z')
d[i]+=32;
}
//cout<<d<<endl;
if(q.count(d)){//如果找到有一个单词属于上面输入的语言之一,就直接输出。
cout<<q[d]<<endl;
break;
}
}

}
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM OJ 数据结构