Code
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
Source
题目类型:欧拉回路
算法分析:这是旋转鼓轮问题的一个直接应用,将两个相邻密码序列的前一个的后n-1位和后一个的前n-1位看作是一个顶点,边的id为0~10^n。这里直接dfs会爆栈,所以需要手动模拟dfs,对于建好的图按照顺序遍历输出。由于这里的图是按照从大到小的方向建的,所以最后直接倒着输出即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
/************************************************* Author :supermaker Created Time :2016/3/18 13:10:24 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 6666; struct Node { int v, w, nxt; }; int head[maxn], edgelen; Node edge[maxn]; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int w) { edge[edgelen].v = v, edge[edgelen].w = w; edge[edgelen].nxt = head[u]; head[u] = edgelen++; } bool vis[maxn]; int out[maxn], len, dfstack[maxn], slen; void SDfs (int val) { slen = len = 0, dfstack[slen++] = val; while (slen > 0) { int tt = dfstack[--slen]; bool is_find = false; for (int i = head[tt]; i != -1; i = edge[i].nxt) if (!vis[edge[i].w]) { vis[edge[i].w] = true; dfstack[slen++] = tt; dfstack[slen++] = edge[i].v; is_find = true; break; } if (!is_find) out[len++] = tt; } } int main() { //CFF; //CPPFF; int n; while (scanf ("%d", &n)) { if (n == 0) break; if (n == 1) puts ("0123456789"); else { int nodenum = 1; for (int i = 1; i <= n - 1; i++) nodenum *= 10; Init (); for (int i = 0; i < nodenum; i++) { int tt = i % (nodenum / 10); for (int j = 9; j >= 0; j--) AddEdge (i, tt * 10 + j, i * 10 + j); } memset (vis, false, sizeof (vis)); SDfs (0); for (int i = 1; i <= n - 2; i++) printf ("0"); for (int i = len - 1; i >= 0; i--) printf ("%d", out[i] % 10); puts (""); } } return 0; } |
- « 上一篇:poj3469
- poj1392:下一篇 »