Word break problem: Difference between revisions

Content added Content deleted
(Added C++ solution)
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 1,171: Line 1,171:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX version allows the words to be tested (and the dictionary words) to be specified on the command line.
This REXX version allows the words to be tested (and the dictionary words) to be specified on the command line.
<lang rexx>/*REXX program breaks up a word (or string) into a list of words from a dictionary. */
<lang rexx>/*REXX program breaks up a word (or string) into a list of words from a dictionary.*/
parse arg a '/' x; a=space(a); x=space(x) /*get optional args; elide extra blanks*/
parse arg a '/' x; a=space(a); x=space(x) /*get optional args; elide extra blanks*/
if a=='' | a=="," then a= 'abcd abbc abcbcd acdbc abcdd' /*maybe use the defaults ? */
if a=='' | a=="," then a= 'abcd abbc abcbcd acdbc abcdd' /*Not specififed? Use default*/
if x=='' | x=="," then x= 'a bc abc cd b' /* " " " " */
if x=='' | x=="," then x= 'a bc abc cd b' /* " " " " */
na=words(a) /*the number of words to be tested. */
na= words(a) /*the number of words to be tested. */
nx=words(x) /* " " " " " the dictionary*/
nx= words(x) /* " " " " " the dictionary*/
say nx ' dictionary words: ' x /*display the words in the dictionary. */
say nx ' dictionary words: ' x /*display the words in the dictionary. */
aw= 0 /*maximum word width obtained (so far).*/
say /*display a blank line to the terminal.*/
say /*display a blank line to the terminal.*/
aw=0; do i=1 for na; _=word(a, i) /*obtain a word that will be tested. */
do i=1 for na; _= word(a, i) /*obtain a word that will be tested. */
aw=max(aw, length(_) ) /*find widest width word being tested. */
aw= max(aw, length(_) ) /*find widest width word being tested. */
end /*i*/ /* [↑] AW is used to align the output*/
end /*i*/ /* [↑] AW is used to align the output*/
@.=0 /*initialize the dictionary to "null". */
@.= 0 /*initialize the dictionary to "null". */
xw= 0
xw=0; do i=1 for nx; _=word(x, i) /*obtain a word from the dictionary. */
xw=max(xw, length(_) ); @._=1 /*find widest width dictionary word. */
do i=1 for nx; _= word(x, i) /*obtain a word from the dictionary. */
end /*i*/ /* [↑] define a dictionary word. */
xw= max(xw, length(_) ); @._= 1 /*find widest width dictionary word. */
p=0 /* [] process a word in the A list.*/
end /*i*/ /* [] define a dictionary word. */
do j=1 for na; yy=word(a, j) /*YY: test a word from the A list.*/
p= 0 /* [↓] process a word in the A list.*/
do t=(nx+1)**(xw+1) by -1 to 1 until y==''; y=yy /*try word possibility.*/
do j=1 for na; yy= word(a, j) /*YY: test a word from the A list.*/
$= /*nullify the (possible) result list. */
do t=(nx+1)**(xw+1) by -1 to 1 until y==''; y= yy /*try word possibility.*/
do try=t while y\='' /*keep testing until Y is exhausted. */
$= /*nullify the (possible) result list. */
p=(try + p) // xw + 1 /*use a possible width for this attempt*/
do try=t while y\='' /*keep testing until Y is exhausted. */
p=fw(y, p); if p==0 then iterate t /*is this part of the word not found ? */
p= (try + p) // xw + 1 /*use a possible width for this attempt*/
$=$ ? /*It was found. Add partial to the list*/
p= fw(y, p); if p==0 then iterate t /*is this part of the word not found ? */
y=substr(y, p + 1) /*now, use and test the rest of word. */
$= $ ? /*It was found. Add partial to the list*/
end /*try*/
y= substr(y, p + 1) /*now, use and test the rest of word. */
end /*t*/
end /*try*/
end /*t*/


if t==0 then $= '(not possible)' /*indicate that the word isn't possible*/
if t==0 then $= '(not possible)' /*indicate that the word isn't possible*/
say right(yy, aw) '───►' strip($) /*display the result to the terminal. */
say right(yy, aw) '───►' strip($) /*display the result to the terminal. */
end /*j*/
end /*j*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/