fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int dictionaryContains(string word)
  5. {
  6. string dictionary[] = {"i", "like", "go", "hired", "gohired", "site", "sweet","fruit", "man", "go", "mango"};
  7. int size = sizeof(dictionary)/sizeof(dictionary[0]);
  8. for (int i = 0; i < size; i++)
  9. if (dictionary[i].compare(word) == 0)
  10. return true;
  11. return false;
  12. }
  13. bool wordBreak(string str)
  14. {
  15. int size = str.size();
  16.  
  17. // Base case
  18. if (size == 0) return true;
  19.  
  20. // Try all prefixes of lengths from 1 to size
  21. for (int i=1; i<=size; i++)
  22. {
  23. cout << str.substr(0, i) <<"-" <<str.substr(i, size-i)<<"\n";
  24. if (dictionaryContains( str.substr(0, i) ) &&
  25. wordBreak( str.substr(i, size-i) ))
  26. return true;
  27. }
  28.  
  29. return false;
  30. }
  31.  
  32. // Driver program to test above functions
  33. int main()
  34. {
  35. wordBreak("ilikemango")? cout <<"Yes\n": cout << "No\n";
  36. // wordBreak("iiiiiiii")? cout <<"Yes\n": cout << "No\n";
  37. //wordBreak("")? cout <<"Yes\n": cout << "No\n";
  38. // wordBreak("ilikelikeimangoiii")? cout <<"Yes\n": cout << "No\n";
  39. // wordBreak("samsungandmango")? cout <<"Yes\n": cout << "No\n";
  40. // wordBreak("samsungandmangok")? cout <<"Yes\n": cout << "No\n";
  41. return 0;
  42. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
i-likemango
l-ikemango
li-kemango
lik-emango
like-mango
m-ango
ma-ngo
man-go
g-o
go-
Yes