fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <set>
  5. #include <map>
  6. #include <sstream>
  7.  
  8. bool findMatch(char * s1, std::string s2){
  9. std::map<char, std::string> letter_to_word;
  10. std::set<std::string> words_seen;
  11. std::istringstream iss(s1);
  12. std::string word;
  13. for (std::string::size_type i = 0; i < s2.size(); ++i)
  14. {
  15. if (!(iss >> word))
  16. return false; // more letters than words
  17. std::string& expected_word = letter_to_word[s2[i]];
  18. if (expected_word == "")
  19. {
  20. // if different letters require different words...
  21. if (words_seen.find(word) != words_seen.end())
  22. return false; // multiple letters for same word
  23. words_seen.insert(word);
  24.  
  25. expected_word = word; // first time we've seen letter, remember associated word
  26. }
  27. else if (expected_word != word)
  28. return false; // different word for same letter
  29. }
  30. return !(iss >> word); // check no surplus words
  31. }
  32.  
  33. int main(int argc, char * argv[]){
  34. bool b = findMatch("red blue blue red red yellow yellow", "abbaacd");
  35. std::cout << b << std::endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
0