"POJ 1416" shredding Company
DFS does not seem to be pruning can also be data water but practice pruning to expand the idea
Two numbers per group T num input 0 0 End
Split digit num for any group let these groups plus and nearest T (and <=t) no solution output error multiple solution output rejected otherwise output plus and there is a segmentation situation
Do the search pruning a little bit of experience when searching the reverse search for the largest from the big to small search for the smallest to large search such a lack of (for maximum) or overflow (minimum) immediately return to achieve efficient pruning because at this time the subsequent situation is smaller than the current (large) (for its foliage)
The problem is a bit of a rejected case if there are multiple maximum splits or maximum splits in which a group leading to 0 (0 can be individually deducted as a group) is rejected
The code is as follows:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace STD;intnum[7],tmp[7],len,mm,t,f;//num-split tmp-len-split group number mm-maximum segmentation and F-Award multiple Solutions (rejected)intGetlen (intX//Find x-digits corresponding to 10^n for DFS (split){intCNT =1; while(x) {x/=Ten; CNT *=Ten; }returncnt/Ten;}voidDfsintRestintSumintSiteintLintP//p 0 No leading 1 has a leading{if(sum + rest <= t && sum + rest >= mm) {if(sum + rest = = mm | | p = =1)//satisfies a condition when multiple solutions or a group has a leading 0{f =1; MM = sum + rest;return; } f =0; MM = sum + rest;memcpy(Num,tmp,sizeof(TMP)); num[site++] = rest; len = Site;return; }if(sum + rest < T && sum + rest < mm)return;//Current small successor smaller for(inti = l; I >=1; I/=Ten) {Tmp[site] = rest/i;if(I >Ten&& rest%i/(i/Ten) ==0) DFS (rest%i, sum + rest/i, site+1, i/ -,1);//The remaining number after division has a leading 0 ElseDFS (rest%i, sum + rest/i, site+1, i/Ten, p); }}intMain () {intN while(~scanf("%d%d", &t,&n) && (t+n)) {f =0; Len =0; MM =-1; DFS (N,0,0, Getlen (N),0);if(mm = =-1)puts("Error");Else if(f)puts("Rejected");Else{printf("%d", MM); for(inti =0; i < Len; ++i)printf("%d", Num[i]);puts(""); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"POJ 1416" shredding Company