// A recursive program to print all possible partitions of a given string
// into dictionary words
#include <iostream>
using namespace std;
/* A utility function to check whether a word is present in dictionary or not.
An array of strings is used for dictionary. Using array of strings for
dictionary is definitely not a good idea. We have used for simplicity of
the program*/
int dictionaryContains( string word)
{
string dictionary[ ] = { "mobile" ,"samsung" ,"sam" ,"sung" ,"man" ,"mango" ,
"icecream" ,"and" ,"go" ,"i" ,"love" ,"ice" ,"cream" } ;
int size = sizeof ( dictionary) / sizeof ( dictionary[ 0 ] ) ;
for ( int i = 0 ; i < size; i++ )
if ( dictionary[ i] .compare ( word) == 0 )
return true ;
return false ;
}
//prototype of wordBreakUtil
void wordBreakUtil( string str, int size, string result) ;
void wordBreak( string str)
{
//last argument is prefix
wordBreakUtil( str, str.size ( ) , "" ) ;
}
// result store the current prefix with spaces between words
void wordBreakUtil( string str, int size, string result)
{
//Process all prefixes one by one
for ( int i= 1 ; i<= size; i++ )
{
//extract substring from 0 to i in prefix
string prefix = str.substr ( 0 , i) ;
// if dictionary conatins this prefix, then we check
// for remaining string. Otherwise we ignore this prefix
// (there is no else for this if) and try next
if ( dictionaryContains( prefix) )
{
// if no more elements are there, print it
if ( i == size)
{
// add this element to previous prefix
result + = prefix;
cout << result << endl; //print result
return ;
}
wordBreakUtil( str.substr ( i, size- i) , size- i, result+ prefix+ " " ) ;
}
} //end for
} //end function
int main( )
{
cout << "First Test:\n " ;
wordBreak( "iloveicecreamandmango" ) ;
cout << "\n \n Second Test:\n " ;
wordBreak( "ilovesamsungmobile" ) ;
return 0 ;
}
Ly8gQSByZWN1cnNpdmUgcHJvZ3JhbSB0byBwcmludCBhbGwgcG9zc2libGUgcGFydGl0aW9ucyBvZiBhIGdpdmVuIHN0cmluZwovLyBpbnRvIGRpY3Rpb25hcnkgd29yZHMKI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLyogQSB1dGlsaXR5IGZ1bmN0aW9uIHRvIGNoZWNrIHdoZXRoZXIgYSB3b3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VuIHdvcmRzCnZvaWQgd29yZEJyZWFrVXRpbChzdHJpbmcgc3RyLCBpbnQgc2l6ZSwgc3RyaW5nIHJlc3VsdCkKewogICAgLy9Qcm9jZXNzIGFsbCBwcmVmaXhlcyBvbmUgYnkgb25lCiAgICBmb3IgKGludCBpPTE7IGk8PXNpemU7IGkrKykKICAgIHsKICAgICAgICAvL2V4dHJhY3Qgc3Vic3RyaW5nIGZyb20gMCB0byBpIGluIHByZWZpeAogICAgICAgIHN0cmluZyBwcmVmaXggPSBzdHIuc3Vic3RyKDAsIGkpOwoKICAgICAgICAvLyBpZiBkaWN0aW9uYXJ5IGNvbmF0aW5zIHRoaXMgcHJlZml4LCB0aGVuIHdlIGNoZWNrCiAgICAgICAgLy8gZm9yIHJlbWFpbmluZyBzdHJpbmcuIE90aGVyd2lzZSB3ZSBpZ25vcmUgdGhpcyBwcmVmaXgKICAgICAgICAvLyAodGhlcmUgaXMgbm8gZWxzZSBmb3IgdGhpcyBpZikgYW5kIHRyeSBuZXh0CiAgICAgICAgaWYgKGRpY3Rpb25hcnlDb250YWlucyhwcmVmaXgpKQogICAgICAgIHsKICAgICAgICAgICAgLy8gaWYgbm8gbW9yZSBlbGVtZW50cyBhcmUgdGhlcmUsIHByaW50IGl0CiAgICAgICAgICAgIGlmIChpID09IHNpemUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIGFkZCB0aGlzIGVsZW1lbnQgdG8gcHJldmlvdXMgcHJlZml4CiAgICAgICAgICAgICAgICByZXN1bHQgKz0gcHJlZml4OwogICAgICAgICAgICAgICAgY291dCA8PCByZXN1bHQgPDwgZW5kbDsgLy9wcmludCByZXN1bHQKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgICAgICB3b3JkQnJlYWtVdGlsKHN0ci5zdWJzdHIoaSwgc2l6ZS1pKSwgc2l6ZS1pLCByZXN1bHQrcHJlZml4KyIgIik7CiAgICAgICAgfQogICAgfSAgICAgIC8vZW5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