Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2259 | Accepted: 820 |
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
呃其实刚做这个题是20多天前了。。当时就不会 现在终于会了 竟然是图论的题
题目大意是:
你需要输入n位的密码 如果前n-1位对了 第n位也对的话 就打开了
否则此时的后n-1位变成了前n-1位 等待最后一位的输入 依次下去 题目要求给出按键的顺序 使得字典序最小
相当于给出一串数组序列 有一个长度为n的窗户 从1-n开始 每次后移一个格
如样例
2
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990依次是00 01 10 02 20 03 30 04 ... 80 09 91 11 12 21 13 .... 90
如果从n个0 到n个9 一个一个试的话 最坏情况是n*10^n次
给出的答案长度是10^n+n-1 如果每一次的数字都不重复的话 而且两个数之间共享了n-1位相同的数字
窗口每向后滑一次 产生一个新的数 一共10^n个数 滑了10^n-1次 加上第一个数的长度n 所以答案一共10^n+n-1个数字
而所有数字要求出现且仅出现1次
把每个数字转化为边 每个数前n-1位到后n-1位之间有一条边 求图的欧拉回路
而且还要用非递归写。。第一次写模拟递归用的goto
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
struct self
{
int x,y,nxt;
}s[3000001];
int fst[100001];
int flag[3000001];
int ret[3000001];
int ans;
int m,n,x,y;
int lim;
void calc(int k)
{
lim=1;
k--;
n=0;
memset(flag,0,sizeof(flag));
memset(fst,-1,sizeof(fst));
ans=0;
while(k)
{
k--;
lim*=10;
}
}
void add(int x,int y)
{
n++;
s[n].x=x;
s[n].y=y;
s[n].nxt=fst[x];
fst[x]=n;
}
struct node
{
int u,e;
}st[3000001];
int top;
void dfs()
{
top=1;
st[1].u=0;
st[1].e=-1;
a:
while(top)
{
node u=st[top];
for(int e=fst[u.u];e!=-1;e=s[e].nxt)
{
if(!flag[e])
{
flag[e]=1;
top++;
st[top].u=s[e].y;
st[top].e=e;
goto a;
}
}
top--;
ans++;
ret[ans]=u.e;
}
}
inline void out(int x)
{
if(x>9)
out(x/10);
putchar(x%10+'0');
}
int main()
{
//freopen("1.out","w",stdout);
while(scanf("%d",&m)==1 && m)
{
if(m==1)
{
printf("0123456789\n");
continue;
}
calc(m);
int tt=lim/10;
for(int i=0;i<lim;i++)
for(int j=9;j>=0;j--)
{
x=i;
y=x%tt*10+j;
add(x,y);
}
dfs();
/*
for(int i=1;i<m;i++)printf("0");
for(int i=ans-1;i>=1;i--)
printf("%d",s[ret[i]].y%10);
printf("\n");
*/
for(int i=1;i<m;i++)out(0);
for(int i=ans-1;i>=1;i--)
out(s[ret[i]].y%10);
printf("\n");
}
return 0;
}
不用输出外挂
加上输出外挂 G++竟然比C++还快