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
Sample Output
Hint
题目大意:先输入n行字符串,每一个字符串的第一个值为语言名字,随后便是该语言中包含有的单词
然后,输入多条句子,处理到文件结束,判断每一条句子属于那种言。
其实思路很简单,关键就是处理给出的句子当中,怎么分割每一个单词,根据题目意思,是可以用逗号,句号,感叹号,括号,分号,问号,空格来分隔每一个单词,所以我们可以将这些分隔符统一定为空格,这样方便处理一些
然后,关于用空格分割字符串,我们可以使用strtock,strstr函数等等,参照了一下网上大佬的代码,这里用的是stringstream流来进行处理
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 ; }
相关文章推荐
- Java集合Map<K,V>,HashMap<K,V>的使用,for循环正解
- Surprising Strings<map集合使用>
- 使用HashSet<String>将map集合去重
- 使用Enumerable.OfType<T>扩展方法实现非泛型集合的Linq查询
- 寻找倍数<set集合使用>
- 再谈使用Emit把Datatable转换为对象集合(List<T>)
- Struts2.3,<s:iterator>,<c:forEach>遍历map中的list集合
- C#:泛型集合—List<T>使用技巧
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值 .
- 将List集合中的map对象转为List<对象>形式实例代码
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- Java集合Map<K,V>
- Intent传递List<Map<String,String>>的使用
- 使用Emit把Datatable转换为对象集合(List<T>)
- 使用 <map> 库创建关联容器
- C#泛型集合—List<T>使用技巧
- python3下使用map()函数返回<map object at 0x00000000038327F0>导致不能直接使用的解决办法
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- java集合汇总-Map-Set-HashMap等的区别与联系<未完待续>