POJ-1416 shredding company Enumeration

Source: Internet
Author: User

Given two numbers, the length of these two numbers cannot exceed 6 digits. Then, how can we determine the split of the last number to bring the value closer to the previous one, in fact, this problem is equivalent to inserting a board in the next digit. You can use state compression to enumerate the partitions.

The Code is as follows:

# Include <iostream> # include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <queue> # include <vector> # include <stack> # include <list> # include <set> using namespace STD; int T, num, bit [10], idx; void break () {idx =-1; while (Num) {bit [++ idx] = num % 10; num/= 10 ;}for (INT I = 0, j = idx; I <j; ++ I, -- j) {swap (bit [I], bit [J]) ;}} Int get (int A, int B) {int ret = 0; For (INT I = A; I <B; ++ I) {RET * = 10, RET + = bit [I];} return ret;} void print (INT state) {int last = 0; For (INT I = 1; I <= idx + 1; ++ I) {If (State & (1 <I) {printf ("% d", get (last, I); last = I ;}}} bool judge (INT state, Int & RET) {int last = 0; ret = 0; For (INT I = 1; I <= idx + 1; ++ I) {If (State & (1 <I) {RET + = get (last, I); last = I ;}} Return ret <= T;} int main () {int mask, Max, CNT, State; while (scanf ("% d", & T, & num ), T | num) {int I; If (t = num) {printf ("% d \ n", T, num); continue ;} mask = CNT = 0; max = 0; break (); for (I = 1; I <= idx; ++ I) {mask | = (1 <I ); // assign a value to the mask} for (I = 0; I <= mask; I ++ = 2) {int T = I; T | = (1 <(idx + 1); int ret; If (Judge (T, RET) {If (max <RET) {max = ret; state = T; CNT = 1;} Else if (max = RET) ++ CNT;} If (max = 0) puts ("error"); else if (CNT! = 1) puts ("REJECTED"); else {printf ("% d", max); print (State); puts ("") ;}} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.