zoukankan      html  css  js  c++  java
  • poj 1780 Code【欧拉通路+非递归DFS】

    Code
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 2723 Accepted: 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
    ");
                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("
    ");
        }
        return 0;
    }
    

  • 相关阅读:
    SSH深度历险(五) 深入浅出-----IOC AND AOP
    Hbuilder X下载及安装教程
    如何用Prometheus监控十万container的Kubernetes集群
    使用并部署Flutter Web的步骤实例
    回顾 Android 11 中的存储机制更新
    移动端UI一致性解决方案
    使用 tail 结合 grep 查找日志关键字并高亮及显示所在行上下文
    Nginx PHP 报504 Gateway time-out错误的解决方法
    SPSS 24 安装详细教程及下载
    CoRL 2020奖项公布,斯坦福获最佳论文奖,华为等摘得最佳系统论文奖
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205205.html
Copyright © 2011-2022 走看看