Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Pablo Moreno Olalla
- Email address: darthbrevu@yahoo.es
- */
- #include <set>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <cstring>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- //Using this array we save some memory.
- class SmallString {
- public:
- char data[7]; //C++ complains when using arrays as data types for templates. This stupid trick is necessary :(.
- inline SmallString() {}
- inline SmallString(const string &in,size_t start) {
- size_t i=0;
- for (;i<in.size()-start&&i<6;++i) data[i]=in[start+i];
- data[i]='\0';
- }
- inline SmallString(const string &in,size_t start,size_t end) {
- size_t i=0;
- for (;i<end-start&&i<6;++i) data[i]=in[start+i];
- data[i]='\0';
- }
- inline bool operator==(const SmallString &ss) const {
- return strcmp(&data[0],&ss.data[0])==0;
- }
- inline bool operator<(const SmallString &ss) const {
- return strcmp(&data[0],&ss.data[0])<0;
- }
- };
- void parse(const string &in,set<SmallString> &out) {
- size_t pos=0,pos2;
- for (;;) {
- pos=in.find_first_not_of(' ',pos);
- if (pos==string::npos) break;
- pos2=in.find_first_of(' ',pos);
- if (pos2==string::npos) {
- out.insert(SmallString(in,pos));
- break;
- } else out.insert(SmallString(in,pos,pos2));
- pos=pos2;
- }
- }
- int main(int argc,char **argv) {
- //The first two lines are not strictly necessary, but make the code a little more handy.
- if (argc>=2) freopen(argv[1],"r",stdin);
- if (argc>=3) freopen(argv[2],"w",stdout);
- string tmp;
- unsigned int N,c;
- getline(cin,tmp);
- if (sscanf(tmp.c_str(),"%u",&N)!=1) return -1;
- vector<pair<string,set<SmallString> > > data(N);
- for (size_t i=0;i<N;++i) {
- //There is a case in which the user inputs twice or more the same command. However, only the first
- //will have effect.
- getline(cin,tmp);
- parse(tmp,data[i].second);
- getline(cin,tmp);
- istringstream iss(tmp);
- iss>>data[i].first;
- if (cin.fail()) return -1;
- }
- getline(cin,tmp);
- if (sscanf(tmp.c_str(),"%u",&c)!=1) return -1;
- set<SmallString> tmpSet;
- for (size_t i=0;i<c;++i) {
- getline(cin,tmp);
- parse(tmp,tmpSet);
- for (size_t j=0;j<N;++j) if (data[j].second==tmpSet) {
- cout<<data[j].first<<endl;
- break;
- }
- tmpSet.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement