title Link:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ problem&problem=5215
Test Instructions: Give a number U (1≤u≤1018), ask for a maximum number smaller than u, this number to meet in this number of each number not more than 2 times.
idea:DFS search maintains an array of records of how many times each number has been present.
1 //#include <bits/stdc++.h>2#include <cstring>3#include <cstdio>4#include <string>5#include <iostream>6 using namespacestd;7 Long LongU;8 intnum[ -];9 intans[ -];Ten intmark[ A]; One intLen; A BOOLFind; - voidDfsintCurintlimit) - { the if(Find)return; - if(Cur <0) - { - BOOLFlag =true; + for(inti = len-1; I >=0; i--) - { + if(flag && ans[i] = =0)Continue; A Else if(Flag && ans[i]! =0) at { -printf"%d", Ans[i]); -Flag =false; - } - Elseprintf"%d", Ans[i]); - } inprintf"\ n"); -Find =true; to return; + } - intup = Limit?num[cur]:9; the for(inti = up; I >=0; i--) * { $ if(Mark[i] >=2)Continue;Panax Notoginsengmark[i]++; -Ans[cur] =i; theDFS (cur-1, limit && i = =Up ); + if(Find)return; Amark[i]--; the } + return; - $ } $ voidSlove (Long Longx) - { -Len =0; the while(x) - {Wuyinum[len++] = x%Ten; theX/=Ten; - } Wumemset (Mark,0,sizeof(Mark)); -DFS (len-1,1); About } $ intMain () - { - //freopen ("In.txt", "R", stdin); - //freopen ("OUT.txt", "w", stdout); A while(~SCANF ("%lld", &U)) + { theFind =false; - Slove (U); $ } the return 0; the}
Uvalive 7203 at the most twice