Guest User

Untitled

a guest
Aug 9th, 2017
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. //Each letters worth in points
  10. map<char, int> LetterValues = {
  11.     {'a',1}, {'b',3}, {'c',3}, {'d',2},
  12.     {'e',1}, {'f',1}, {'g',2}, {'h',4},
  13.     {'i',1}, {'j',8}, {'k',5}, {'l',1},
  14.     {'m',3}, {'n',1}, {'o',1}, {'p',3},
  15.     {'q',10},{'r',1}, {'s',1}, {'t',1},
  16.     {'u',1}, {'v',4}, {'w',4}, {'x',8},
  17.     {'y',4}, {'z',10}
  18. };
  19.  
  20. struct WORD
  21. {
  22.     string word;
  23.     int score;
  24. };
  25.  
  26. int calculatePointValue(string word)
  27. {
  28.     int val =0;
  29.     for(int i =0; i<word.length(); ++i)
  30.     {
  31.         if(LetterValues[word[i]])
  32.             val += LetterValues[word[i]];
  33.     }
  34.     return val;
  35. }
  36.  
  37. //O(I*J), could be optimized
  38. bool compareStrings(string lhs, string rhs)
  39. {
  40.     int count = 0;
  41.    
  42.     for(int i =0; i < lhs.length(); ++i)
  43.     {
  44.         char l = lhs[i];
  45.         for(int j =0; j < rhs.length(); ++j)
  46.         {
  47.             char r = rhs[j];
  48.             if(l == r)
  49.             {
  50.                 count++;
  51.                 break;
  52.             }
  53.         }
  54.         if(count == rhs.length())
  55.         {
  56.             return true;
  57.         }
  58.     }
  59.    
  60.     return false;
  61. }
  62.  
  63. int main()
  64. {
  65.     int N;
  66.     cin >> N; cin.ignore();
  67.    
  68.     vector<WORD> dictionary;
  69.    
  70.     for (int i = 0; i < N; i++) {
  71.         string W;
  72.         getline(cin, W);//dictionary of available words
  73.         int val = calculatePointValue(W);
  74.         WORD temp;
  75.         temp.word = W;
  76.         temp.score = val;
  77.         dictionary.push_back(temp);
  78.         cerr << W << " " << val << endl;
  79.     }
  80.     string LETTERS; //letters provided
  81.     getline(cin, LETTERS);
  82.    
  83.     if(LETTERS.length() > 0)
  84.     {
  85.         int maximum = -9999, index = 0;
  86.         string tempWord = "";
  87.         int tempVal = 0;
  88.         for(int i =0; i < dictionary.size(); ++i)
  89.         {
  90.             tempWord = dictionary[i].word;
  91.             tempVal = dictionary[i].score;
  92.        
  93.             if(compareStrings(LETTERS, tempWord))
  94.             {
  95.                 if(tempVal > maximum)
  96.                 {
  97.                     maximum = tempVal;
  98.                     index = i;
  99.                 }
  100.             }
  101.         }
  102.         cout << dictionary[index].word << endl;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment