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