C - 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
The dictionary is completed by putting the words in each language as keys and the names of the languages as values into the map.
The title also requires ignoring the punctuation mark ",""..."; "?"""!"(")","as a separator. In order to achieve this goal, first read in the string described in Input, and then replace all the punctuation points except blanks with blanks, so that all the words in the string are separated by blanks. Finally, read the words one by one with the string stream, check whether there is a match in the dictionary, if matched, output the corresponding value.
#include <iostream>
#include <string>
#include <map>
#include <stdio.h>
#include <cctype>
#include <sstream>
using namespace std;
void deladdpunct(string &str){
//Function names don't make sense. They're actually lowercase strings.
for(int i = 0; i < str.length(); i++){
str[i] = tolower(str[i]);
}
}
int main(){
map<string,string> dict;
int T;
string lang, examp;
char sep;
cin >> T;
for(int t = 0; t < T; t++){
cin >> lang;
sep = getchar();
while(sep != '\n'){//Read the next language after you return
cin >> examp;
deladdpunct(examp);//--+
dict[examp] = lang;// + - Keep key-value pairs by ignoring case
sep = getchar(); //--+
}
}
string text;
string wholeLine;
bool found = false;
while(getline(cin, wholeLine)){
for(int i = 0; i < wholeLine.length(); i++)
if(wholeLine[i] == ',' || wholeLine[i] == ';' || wholeLine[i] == '.' || wholeLine[i] == '(' || wholeLine[i] == ')' || wholeLine[i] == '!' || wholeLine[i] == '?')
wholeLine[i] = ' ';
stringstream ss(wholeLine);//Read the words one by one with sstream
while(ss >> text){
deladdpunct(text);
if(found == false){ //①
if(dict.count(text)){
cout << dict[text] << endl;
found = true;
}
}
}
if(sep == '\n'){ //②
found = false; //(1) and (2) can be simplified. In fact, found s are initialized to false at the end of the loop.
} //And the inner loop is only executed when found ation is false. No need to mention it.
} //Sep==' n'is changed to false; it does not have to be in the inner loop alone.
return 0; //To judge whether found s are true or false, you can write them directly in the cyclic conditions.
} //As for the second condition, it always satisfies because when reading a dictionary, the dictionary ends with a return train.
//Since then, the value of sep has not been changed, so sep always saves' n'.