CSU 1826:Languages(stringstream的应用)

来源:互联网 发布:关于网络暴力的演讲稿 编辑:程序博客网 时间:2024/06/10 05:03

Languages

Time limit 5000 ms Memory limit 131072 kB


Problem 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


题意:

现在有很多门语言,输入每行第一个单词是语言的名字,接下来一整行都是语言中的单词,每个单词只会出现在一种语言中(单词忽略大小写,并且逗号、感叹号、分号、时期、问号和括号不包含在单词内)。输入完以后,输入句子要我们判断是那种语言。

解题思路:

暴力。由于大小写,并且逗号、感叹号、分号、时期、问号和括号不包含在单词,我们直接把这些符号全部变成空格,大写全部转为小写,然后用stringstream来遍历单词,出现相同的单词就意味着找到这门语言(因为一个单词不会出现在两种语言)。


Code:

#include <iostream>#include <string>#include <sstream>using namespace std;string lgg[105];string name[105];int main(){    int n;    cin>>n;    for(int i=0; i<n; i++)    {        cin>>name[i];        getline(cin,lgg[i]);        int len=lgg[i].length();        for(int j=0; j<len; j++)        {            if(lgg[i][j]==','||lgg[i][j]=='.'||lgg[i][j]=='!'||lgg[i][j]==':'||lgg[i][j]=='?'||lgg[i][j]==')'||lgg[i][j]=='(')                lgg[i][j]=' ';            else if(lgg[i][j]>='A'&&lgg[i][j]<='Z')                lgg[i][j]+='a'-'A';        }    }    string s;    while(getline(cin,s))    {        int len=s.length();        for(int i=0;i<len;i++)        {            if(s[i]==','||s[i]=='.'||s[i]=='!'||s[i]==':'||s[i]=='?'||s[i]==')'||s[i]=='(')                s[i]=' ';            else if(s[i]>='A'&&s[i]<='Z')                s[i]+='a'-'A';        }        //for(string rr;ss1>>rr;cout<<rr<<endl);        int flag=0;        stringstream ss1(s);        for(int i=0;i<n;i++)        {            string j;            for(stringstream ss1(lgg[i]);ss1>>j;)            {                //cout<<j<<endl;                string k;                for(stringstream ss2(s);ss2>>k;)                {                    //cout<<j<<' '<<k<<endl;                    if(j==k)                    {                        cout<<name[i]<<endl;                        flag=1;                        break;                    }                }                if(flag==1)                    break;            }            if(flag==1)                break;        }    }    return 0;}