Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1642 | Accepted: 624 |
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 10 n-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 * 10 n keystrokes. By choosing a good digit sequence it is possible to open the safe in at most 10 n + 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...
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 10 n-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 * 10 n keystrokes. By choosing a good digit sequence it is possible to open the safe in at most 10 n + 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 10
n + n - 1 digits that contains each n-digit sequence exactly once.
Sample Input
1 2 0
Sample Output
0123456789 00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990
Source
十分蛋疼的一题啊....网上看的题解....题意就是对于n位数,产生一个字串,在这个字串里每n位数都只能出现一次,对于n位数,假设是3位数,那么就可以从00到00?,?为0~9连上一条边,也就是说对于2位数ab,可以产生abc,那么以此类推,然后每个数都只能出现一次,所以可以想到欧拉回路。递归会溢出,还是自己建个栈吧....
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1000010;
int n,edge,cnt;
int res[maxn],stak[maxn];
int len[]= {1,10,100,1000,10000,100000,1000000};
bool vis[maxn],used[maxn/10];
struct Edge
{
int to,next;
} e[maxn];
int head[maxn/10];
inline void add(int u,int v)
{
e[edge].to=v;
e[edge].next=head[u];
head[u]=edge++;
}
void dfs()
{
int top=0,u,v,i;
stak[top++]=0;
while(top>0)
{
u=stak[--top];
for(i=head[u]; i!=-1; i=e[i].next)
{
if(!vis[i]&&!used[v=e[i].to])
{
stak[top++]=v;
vis[i]=true;
head[u]=i;
break;
}
}
if(!(e[i].next+1)) used[u]=true;
res[cnt++]=v%10;
}
}
int main()
{
int i,num,j;
while(~scanf("%d",&n)&&n)
{
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
edge=0;
if(n==1)
{
puts("0123456789");
continue;
}
for(i=0; i<len[n-1]; ++i)
{
num=(i*10)%len[n-1];
for(j=9; j>=0; --j) add(i,num+j);
}
cnt=0;
dfs();
for(i=1; i<n; i++) printf("0");
for(i=0; i<cnt-1; i++)
printf("%d",res[i]);
for(; i<len[n]; ++i)//我的搜索到最后的时候会产生n+1个9,而且会少掉90...0这个n位数,在这里补上
printf("0");
printf("\n");
}
return 0;
}