Advertisement
Nbrevu

Tuenti Contest 10 (Pablo Moreno)

Jun 20th, 2011
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /*
  2.     Author: Pablo Moreno Olalla
  3.     Email address: darthbrevu@yahoo.es
  4. */
  5. #include <set>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #include <cstring>
  10. #include <utility>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <algorithm>
  14.  
  15. using namespace std;
  16.  
  17. //Using this array we save some memory.
  18. class SmallString   {
  19. public:
  20.     char data[7];   //C++ complains when using arrays as data types for templates. This stupid trick is necessary :(.
  21.     inline SmallString()    {}
  22.     inline SmallString(const string &in,size_t start)   {
  23.         size_t i=0;
  24.         for (;i<in.size()-start&&i<6;++i) data[i]=in[start+i];
  25.         data[i]='\0';
  26.     }
  27.     inline SmallString(const string &in,size_t start,size_t end)    {
  28.         size_t i=0;
  29.         for (;i<end-start&&i<6;++i) data[i]=in[start+i];
  30.         data[i]='\0';
  31.     }
  32.     inline bool operator==(const SmallString &ss) const {
  33.         return strcmp(&data[0],&ss.data[0])==0;
  34.     }
  35.     inline bool operator<(const SmallString &ss) const  {
  36.         return strcmp(&data[0],&ss.data[0])<0;
  37.     }
  38. };
  39.  
  40. void parse(const string &in,set<SmallString> &out)  {
  41.     size_t pos=0,pos2;
  42.     for (;;)    {
  43.         pos=in.find_first_not_of(' ',pos);
  44.         if (pos==string::npos) break;
  45.         pos2=in.find_first_of(' ',pos);
  46.         if (pos2==string::npos) {
  47.             out.insert(SmallString(in,pos));
  48.             break;
  49.         }   else out.insert(SmallString(in,pos,pos2));
  50.         pos=pos2;
  51.     }
  52. }
  53.  
  54. int main(int argc,char **argv)  {
  55.     //The first two lines are not strictly necessary, but make the code a little more handy.
  56.     if (argc>=2) freopen(argv[1],"r",stdin);
  57.     if (argc>=3) freopen(argv[2],"w",stdout);
  58.     string tmp;
  59.     unsigned int N,c;
  60.     getline(cin,tmp);
  61.     if (sscanf(tmp.c_str(),"%u",&N)!=1) return -1;
  62.     vector<pair<string,set<SmallString> > > data(N);
  63.     for (size_t i=0;i<N;++i)    {
  64.         //There is a case in which the user inputs twice or more the same command. However, only the first
  65.         //will have effect.
  66.         getline(cin,tmp);
  67.         parse(tmp,data[i].second);
  68.         getline(cin,tmp);
  69.         istringstream iss(tmp);
  70.         iss>>data[i].first;
  71.         if (cin.fail()) return -1;
  72.     }
  73.     getline(cin,tmp);
  74.     if (sscanf(tmp.c_str(),"%u",&c)!=1) return -1;
  75.     set<SmallString> tmpSet;
  76.     for (size_t i=0;i<c;++i)    {
  77.         getline(cin,tmp);
  78.         parse(tmp,tmpSet);
  79.         for (size_t j=0;j<N;++j) if (data[j].second==tmpSet)    {
  80.             cout<<data[j].first<<endl;
  81.             break;
  82.         }
  83.         tmpSet.clear();
  84.     }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement