1. 程式人生 > >poj 1780 Code【尤拉通路+非遞迴DFS】

poj 1780 Code【尤拉通路+非遞迴DFS】

Code
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 2723Accepted: 951

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

1
2
0

Sample Output

0123456789
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990
  • 題解:首先要明白為什麼選擇一個好的數字序列,至多隻需按鍵10n + n - 1 次就可以開啟保險箱了。n 位數有10n 種編碼方案(即10n 組數),要使得一個數字序列包含這10n 組n 位數,且序列的長度最短,唯一的可能是每組數出現一次且僅一次、且前一組數的後n-1 位是後一組數的前n-1 位,這樣10n 組數各取1 位,共10n 位,再加上最後一組數的後n-1 位,總位數是10n + n - 1。求序列的方法為:對於當前長度為n-1 的序列, 其後新增一個數字, 使得新增後的序列沒有在前面出現過。需要注意的地方是,由於1≤n≤6,直接用遞迴方法會造成棧溢位,需要顯式地用棧來實現演算法。這時演算法的輸出順序為從後往前,故用棧儲存結果時,優先儲存的應是較大值。這樣最後對結果棧進行逆序輸出的時候得到的串是按字典序排列的。
  • 程式碼:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 100000
int List[M];
int Stack[M * 10]; //用陣列模擬棧結構
char ans[M * 10]; //結果棧,序列逆序存放
int s, a; //陣列棧的大小以及結果棧的大小
//對於當前長度為n-1 的序列, 其後新增一個數字, 使得新增後的序列沒有在前面出現過
void Search(int v,int m){//將當前頂點延伸
    int w;
    while(List[v]<10){//可以在v(n-1位的序列)後加0~9構成10條邊
        w=v*10+List[v];
        List[v]++;
        Stack[s++]=w;
        v=w%m;
    }
}
int main(){
    int n,m;
    int i,v;
    while(scanf("%d",&n)!=EOF&&n){
        if(n==1){
            printf("0123456789\n");
            continue;
        }
        s=0,a=0,v=0;
        m=pow(10.0,double(n-1));
        for(int i=0;i<m;i++){
            List[i]=0;
        }
        Search(v,m);
        while(s){
            v=Stack[--s];
            ans[a++]=v%10+'0';
            v/=10;
            Search(v,m);
        }
        for(int i=1;i<n;i++) printf("0");
        while(a) printf("%c",ans[--a]);
        printf("\n");
    }
    return 0;
}