Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <map>
- using namespace std;
- //Each letters worth in points
- map<char, int> LetterValues = {
- {'a',1}, {'b',3}, {'c',3}, {'d',2},
- {'e',1}, {'f',1}, {'g',2}, {'h',4},
- {'i',1}, {'j',8}, {'k',5}, {'l',1},
- {'m',3}, {'n',1}, {'o',1}, {'p',3},
- {'q',10},{'r',1}, {'s',1}, {'t',1},
- {'u',1}, {'v',4}, {'w',4}, {'x',8},
- {'y',4}, {'z',10}
- };
- struct WORD
- {
- string word;
- int score;
- };
- int calculatePointValue(string word)
- {
- int val =0;
- for(int i =0; i<word.length(); ++i)
- {
- if(LetterValues[word[i]])
- val += LetterValues[word[i]];
- }
- return val;
- }
- //O(I*J), could be optimized
- bool compareStrings(string lhs, string rhs)
- {
- int count = 0;
- for(int i =0; i < lhs.length(); ++i)
- {
- char l = lhs[i];
- for(int j =0; j < rhs.length(); ++j)
- {
- char r = rhs[j];
- if(l == r)
- {
- count++;
- break;
- }
- }
- if(count == rhs.length())
- {
- return true;
- }
- }
- return false;
- }
- int main()
- {
- int N;
- cin >> N; cin.ignore();
- vector<WORD> dictionary;
- for (int i = 0; i < N; i++) {
- string W;
- getline(cin, W);//dictionary of available words
- int val = calculatePointValue(W);
- WORD temp;
- temp.word = W;
- temp.score = val;
- dictionary.push_back(temp);
- cerr << W << " " << val << endl;
- }
- string LETTERS; //letters provided
- getline(cin, LETTERS);
- if(LETTERS.length() > 0)
- {
- int maximum = -9999, index = 0;
- string tempWord = "";
- int tempVal = 0;
- for(int i =0; i < dictionary.size(); ++i)
- {
- tempWord = dictionary[i].word;
- tempVal = dictionary[i].score;
- if(compareStrings(LETTERS, tempWord))
- {
- if(tempVal > maximum)
- {
- maximum = tempVal;
- index = i;
- }
- }
- }
- cout << dictionary[index].word << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment