Poj1416-shredding company

Source: Internet
Author: User

 


Want to see more problem-solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006

 

Question:

The company wants to invent a new shredder that requires the new shredder to cut the number on the paper into the nearest value without exceeding the target value. For example, if the value of target is 50, and the number on the paper is 12346, you should cut the number into four parts: 1, 2, 34, and 6. Because the sum of 43 (= 1 + 2 + 34 + 6) obtained in this way is the closest possible and not more than 50. (For example, 1, 23, 4, and 6 are not allowed, because their sum is not as close as 43 to 50, and 12, 34, and 6 are not allowed, because their sum exceeds 50. There are three requirements for broken paper:

1. If the target value is equal to the value on the paper, it cannot be switched.
2. If there is no way to cut the number on the paper into smaller than target, an error is output. For example, if the value of target is 1 and the number on the paper is 123, the sum you get is greater than 1.
3. If more than one cut method is used to obtain the optimal value, the rejected is output. For example, if the value of target is 15 and the number on the paper is 111, the following two cut methods are available: 11, 1, or 11.
Your task is to write a program to divide the numbers for the best value.

Solution:

Use the brute force DFS search to list all the situations. For example, 111 is used to first split 111 into, 1, and the second step into, and the third step, in this way, all the search results will be completed, and only the output can be judged during statistics.

Code:

#include <iostream>using namespace std;int dis[1000000];int maxn,sum,v[10],vt[10],len,t;char num[10];void dfs(int x,int m,int cap){int i,y,flag;if(x>len-1){flag=1;for(i=0;i<cap;i++)if(vt[i]!=v[i]) flag=0;if(maxn<m){maxn=m;sum=cap;for(i=0;i<cap;i++) vt[i]=v[i];}if(flag==0) dis[m]++;return;}y=0;for(i=x;i<len;i++){y=y*10+(num[i]-'0');if(y+m<=t){v[cap]=y;dfs(i+1,y+m,cap+1);}else break;}}int main(){int s,i;while(scanf("%d%s",&t,num)){memset(v,0,sizeof(v));memset(vt,0,sizeof(vt));memset(dis,0,sizeof(dis));len = strlen(num);if(t==0 && len==1 && num[0]=='0') break;s=0;for(i=0;i<len;i++)s+=num[i]-'0';if(s>t) {printf("error\n");continue;}maxn=0,sum=0;dfs(0,0,0);if(dis[maxn]!=1)printf("rejected");else{printf("%d",maxn);for(i=0;i<sum;i++) printf(" %d", vt[i]);}printf("\n");}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.