标签:des style blog http color os io strong
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4114 | Accepted: 2324 |
Description
Input
Output
Sample Input
50 12346 376 144139 927438 927438 18 3312 9 3142 25 1299 111 33333 103 862150 6 1104 0 0
Sample Output
43 1 2 34 6 283 144 139 927438 927438 18 3 3 12 error 21 1 2 9 9 rejected 103 86 2 15 0 rejected
题目链接:Shredding Company
思路:DFS,有n个数字,则有n-1个切口,回溯这n-1个刀口,因为记录路径不知所措了很长时间,不得不拆开记录。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : ShreddingCompany.cpp 4 * Creat time : 2014-08-07 11:33 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <cstdlib> 12 #include <queue> 13 #include <cmath> 14 #define clr(a,b) memset(a,b,sizeof(a)) 15 #define M 16 using namespace std; 17 int t,n,m,ans; 18 char str[10]; 19 int cut[10],bestcut[10],cntmax; 20 /*---切纸操作,每次切两刀,但a刀是重复的---*/ 21 int CutPaper(int a,int b,int sum) /*sum表示当前数*/ 22 { 23 int as = pow(10,n-a); 24 sum = sum-(m % as); 25 if(sum <= t){ /*a刀切完后判断当前sum是否满足条件*/ 26 cut[a] = 1; /*满足标记此刀*/ 27 } 28 int s = 0; 29 for(int i = a; i < b; i ++){ 30 s = s*10 + (str[i] - ‘0‘); 31 } 32 sum += s; 33 if(sum <= t){ /*b刀切完后判断当前sum是否满足条件*/ 34 cut[b] = 1; /*满足标记*/ 35 } 36 s = 0; 37 for(int i = b; i < n; i++){ /*计算剩余部分*/ 38 s = s*10 + (str[i] - ‘0‘); 39 } 40 sum += s; 41 return sum; 42 } 43 /*-------------------深搜函数---------------------*/ 44 void DFS(int id,int sum) 45 { 46 if(id == n) return ; /*id代表当前刀的位置,若等于n说明切到了最后*/ 47 for(int i = id+1; i <= n-1; i++){ 48 int temp = CutPaper(id,i,sum); 49 if(temp <= t && temp > ans){ /*若此次切纸操作满足要求*/ 50 cntmax = 0; /*将标记最大值次数置零*/ 51 ans = temp; /*更新最大值*/ 52 memcpy(bestcut,cut,sizeof(bestcut)); /*拷贝当前最优结果到bestcut*/ 53 } 54 else if(temp <= t && temp == ans){ /*若当前最优结果再次出现*/ 55 cntmax++; 56 } 57 DFS(i,temp); 58 cut[id] = 0; cut[i] = 0; /*回溯必须*/ 59 } 60 } 61 int main(int argc,char *argv[]) 62 { 63 while(scanf("%d%s",&t,str)!=EOF){ 64 if(t == 0 && str[0] == ‘0‘){ 65 break; 66 } 67 m = atoi(str); 68 n = strlen(str); 69 if(t >= m){ 70 printf("%d %d\n",m,m); 71 continue; 72 } 73 ans = -1; 74 cntmax = 0; 75 DFS(0,m); 76 if(ans == -1){ 77 printf("error\n"); 78 } 79 else if(cntmax >= 1){ 80 printf("rejected\n"); 81 } 82 else{ 83 printf("%d",ans); 84 for(int i = 0; i < n; i++){ 85 if(bestcut[i]){ 86 printf(" "); 87 } 88 printf("%c",str[i]); 89 } 90 printf("\n"); 91 } 92 clr(str,0); 93 } 94 return 0; 95 }
poj 1416 -- Shredding Company,布布扣,bubuko.com
标签:des style blog http color os io strong
原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3898648.html