poj1780(模拟+回溯)

来源:互联网 发布:天音网络发展有限公司 编辑:程序博客网 时间:2024/06/11 13:23
Code
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2696 Accepted: 937

Description

KEY Inc., the leading company in security hardware, has developed a new kind of safe. To unlock it, you don't need a key but you are required to enter the correct n-digit code on a keypad (as if this were something new!). There are several models available, from toy safes for children (with a 2-digit code) to the military version (with a 6-digit code). 

The safe will open as soon as the last digit of the correct code is entered. There is no "enter" key. When you enter more than n digits, only the last n digits are significant. For example (in the 4-digit version), if the correct code is 4567, and you plan to enter the digit sequence 1234567890, the door will open as soon as you press the 7 key. 

The software to create this effect is rather simple. In the n-digit version the safe is always in one of 10n-1 internal states. The current state of the safe simply represents the last n-1 digits that have been entered. One of these states (in the example above, state 456) is marked as the unlocked state. If the safe is in the unlocked state and then the right key (in the example above, 7) is pressed, the door opens. Otherwise the safe shifts to the corresponding new state. For example, if the safe is in state 456 and then you press 8, the safe goes into state 568. 

A trivial strategy to open the safe is to enter all possible codes one after the other. In the worst case, however, this will require n * 10n keystrokes. By choosing a good digit sequence it is possible to open the safe in at most 10n + n - 1 keystrokes. All you have to do is to find a digit sequence that contains all n-digit sequences exactly once. KEY Inc. claims that for the military version (n=6) the fastest computers available today would need billions of years to find such a sequence - but apparently they don't know what some programmers are capable of...

Input

The input contains several test cases. Every test case is specified by an integer n. You may assume that 1<=n<=6. The last test case is followed by a zero.

Output

For each test case specified by n output a line containing a sequence of 10n + n - 1 digits that contains each n-digit sequence exactly once.

Sample Input

120

Sample Output

012345678900102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990

Source

Ulm Local 2004
/*题意:密码由n位组成,问字典序最小的输入顺序,输入时如果是前n-1位加最后一位,错了继续取最后面n-1位,再次输入*/#include<iostream>#include<cstdio>#include<string>#include<queue>#include<stack>#include<map>#include<vector>#include<list>#include<set>#include<iomanip>#include<cstring>#include<cctype>#include<cmath>#include<cstdlib>#include<ctime>#include<cassert>#include<sstream>#include<algorithm>using namespace std;#define MAXN 1000005#define INF 0x3f3f3f3f#define PI acos(-1.0)typedef long long ll;char ans[MAXN];int vis[MAXN];int main(){int n;while(~scanf("%d",&n)&&n){int mod=1,end=1;for(int i=0; i<n; i++){mod*=10;end*=10;}mod/=10;//%前n-1位end+=n-1;//总共多少数for(int i=0; i<n; i++)ans[i]='0';//前n位全为0memset(vis,0,sizeof(vis));vis[0]=true;int now=0;//当前数前n-1位的值int i=0;//最后一位的值int pos=n;//当前数到第几位了while(pos<end){now%=mod;for(; i<10; i++)if(!vis[now*10+i]){vis[now*10+i]=true;ans[pos]=i+'0';pos++;now=now*10+i;i=0;break;}if(i==10&&pos<end)//例如90,后退,变91。l路径回溯{pos--;now=(ans[pos-n+1]-'0')*mod+now;vis[now]=false;i=now%10+1;now/=10;}}ans[end]='\0';printf("%s\n",ans);}return 0;}

原创粉丝点击