Word break problem: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments.
(Added C++ solution)
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 1,171:
=={{header|REXX}}==
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. */
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' /*maybeNot usespecififed? the defaults ?Use default*/
if x=='' | x=="," then x= 'a bc abc cd b' /* " " " " " */
na= words(a) /*the number of words to be tested. */
nx= words(x) /* " " " " " 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.*/
aw=0; 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. */
end /*i*/ /* [↑] AW is used to align the output*/
@.=0 0 /*initialize the dictionary to "null". */
xw= 0
xw=0; do i=1 for nx; _=word(x, i) /*obtain a word from the dictionary. */
do xwi=max(xw,1 length(_) )for nx; @._=1 _= word(x, i) /*findobtain widesta widthword dictionaryfrom wordthe dictionary. */
xw= end /*i*/ max(xw, length(_) ); @._= 1 /*find [↑]widest define awidth dictionary word. */
p=0 end /*i*/ /* [] processdefine a dictionary word. in the A list.*/
p= 0 do j=1 for na; yy=word(a, j) /*YY: [↓] testprocess a word fromin the A list.*/
do tj=(nx+1)**(xw+1) byfor na; -1 to 1 until y==''; y=yy= word(a, j) /*tryYY: test a word possibility from the A list.*/
do $t=(nx+1)**(xw+1) by -1 to 1 until y==''; y= yy /*nullifytry theword (possible) result listpossibility. */
$= do try=t while y\='' /*keep testing until Y is exhausted /*nullify the (possible) result list. */
do ptry=(tryt + p)while y\='' // xw + 1 /*usekeep testing until a possibleY width foris thisexhausted. attempt*/
p=fw (y,try + p); if p==0// xw then iterate t /*is+ this1 part of the word not found ? /*use a possible width for this attempt*/
$p=$ ?fw(y, p); if p==0 then iterate t /*is this part of the word /*It wasnot found. Add partial to the? list*/
$= $ y=substr(y,? p + 1) /*now, use and test the rest of word /*It was found. Add partial to the list*/
y= substr(y, p + 1) end /*trynow, use and test the rest of word. */
end end /*ttry*/
end /*t*/
 
if t==0 then $= '(not possible)' /*indicate that the word isn't possible*/
say right(yy, aw) '───►' strip($) /*display the result to the terminal. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/