Code

来源:互联网 发布:访客软件 编辑:程序博客网 时间:2024/06/12 19:07

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 correctn-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 thann 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 of10n-1 internal states. The current state of the safe simply represents the lastn-1 digits that have been entered. One of these states (in the example above, state 456) is marked as theunlocked 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 requiren * 10n keystrokes. By choosing a good digit sequence it is possible to open the safe in at most10n + n - 1 keystrokes. All you have to do is to find a digit sequence that contains alln-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 Specification

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

Output Specification

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

Sample Input

120

Sample Output

012345678900102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990这道题是采用的欧拉回路算出来,但是发现并没有真正的理解,不懂为什么这样可行,从后向前枚举是为了保证字典序最小。
#include <stdio.h>#include <string.h>const int maxn = 1000010;int pr[maxn], stack[maxn], ans[maxn], mod, top;void dfs ( int v ){    while ( pr[v] < 10 )    //表示前缀出现的次数    {        int w = v*10+pr[v];        pr[v] ++;   //此前缀加1        stack[top ++] = w;  //保存此前缀形成的数        v = w%mod;  //保证只有n-1(前缀)    }}int main ( ){    int n, pos;    while ( ~ scanf ( "%d", &n ) && n )    {        memset ( pr, 0, sizeof ( pr ) );        mod = 1;        top = pos = 0;        for ( int i = 1; i < n; i ++ )            mod = mod*10;        dfs ( 0 );        while ( top > 0 )        {            ans[pos ++] = stack[-- top]%10;            //从后往前算出每个数            dfs ( stack[top]/10 );        }        for ( int i = 1; i < n; i ++ )            printf ( "0" ); //前面打印n-1个0        while ( pos > 0 )            printf ( "%d", ans[-- pos] );        printf ( "\n" );    }}


0 0
原创粉丝点击