欧拉回路的典型应用
来源:互联网 发布:淘宝送一个月优酷会员 编辑:程序博客网 时间:2024/06/09 11:55
Code
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1943 Accepted: 706
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...
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
120
Sample Output
012345678900102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990
分析:
用DFS找一个欧拉回路,按照字典序输出路径。
由于状态过多,不能用递归,要用非递归实现DFS(好麻烦,,,),然后不能用stack会超时,自己写个数组吧。
关于这题为什么会是欧拉回路呢。
首先题意:题目告诉我们了密码的长度为n (那么一个密码一共可能有10^n种可能),然后由于当输入密码长度大于n时,只有最后n为有效。这样我们只要用一个(10^n+n-1)长的数字序列,就能破解这个密码。至于为什么是这个长度,那是因为一共有10^n个状态分别拿出第一个数字排列下去,然后最后一个密码会多出n-1位。直接放在后面。这样实际上这个序列就是一个欧拉回路,它实际包含了10^n个密码(每个状态都出现了一次);
关于边与节点的定义。 加入密码为3位
那么我们的节点为前2为数,然后边会在前两位数后加上一个数形成的完整密码,然后后继点为新密码的后两位。
最后一共会有10^n条边,我们要找一个通路走完每一条边,也就是说每个密码状态都要出现一次,
这个题目实际上就是欧拉回路的旋转鼓轮的应用。
建图:
例如n==4
要想形成1234 建边为123-->234 权值为1234
程序:
#include"stdio.h"#include"string.h"#include"queue"#include"stack"#include"iostream"#include"string"#include"map"#include"stdlib.h"#define inf 99999999#define M 1000009using namespace std;struct st{ int u,v,w,next;}edge[M];int t,head[M],use[M],s[M],cnt,top,num;struct node{ int u,id;}Stack[M];void init(){ t=0; memset(head,-1,sizeof(head));}void add(int u,int v,int w){ edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++;}void dfs(int u){ top=num=0; int i; memset(use,0,sizeof(use)); top++; Stack[top].u=u; Stack[top].id=head[u]; while(top) { node x=Stack[top]; for(i=head[x.u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!use[i]) { use[i]=1; top++; Stack[top].u=v; Stack[top].id=head[v]; break; } } if(i==-1) { s[num++]=x.id; top--; } }}int main(){ int n,i,j; while(scanf("%d",&n),n) { if(n==1) { printf("0123456789\n"); continue; } int cnt=1; for(i=1;i<=n;i++) cnt*=10; int len=cnt/10; init(); for(i=0;i<len;i++) { int tep=i%(len/10); tep*=10; for(j=9;j>=0;j--) { add(i,tep+j,i*10+j); } } dfs(0); for(i=num-1;i>0;i--) printf("%d",s[i]/len); printf("%d",s[i]/10); for(i=1;i<n-1;i++) printf("0"); printf("\n"); } return 0;}
0 0
- 欧拉回路的典型应用
- hdu4850(欧拉回路的应用)
- hdu1116 Play on Words(典型欧拉回路)
- POJ - 1392 Ouroboros Snake (欧拉回路的应用)
- 单词接龙 WordsCanRange --欧拉回路和欧拉路的应用
- 欧拉回路(混合图的欧拉回路)
- HDU1878欧拉回路(欧拉回路的判定)
- 欧拉回路--输出欧拉回路的路径
- 欧拉回路的使用
- 欧拉回路的总结
- 欧拉回路的判断
- 欧拉回路的判定
- 欧拉回路的判断
- 欧拉回路的判断
- 图的欧拉回路
- 欧拉回路及其简单应用
- poj1300doorman欧拉回路简单应用
- 欧拉图,欧拉回路,以及求欧拉回路的Fleury算法
- sqlserver中删除数据表中重复的数据或是某一列中重复的字段,紧保留一条数据
- 集训日志(三) 动态规划
- OC中的instancetype和id区别
- android面试准备2
- Oracle建表主键策略
- 欧拉回路的典型应用
- POJ 2299-Ultra-QuickSort (树状数组+离散化)
- poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图
- J2EE之初识JSP
- [亲测] 解决在 ubuntu14.04 安装完 vmware 后,启动出现下列问题的解决方案
- DataSet.Tables[0].Rows[0][1]的含义
- cocos2d-x环境搭建
- android 4.0.4系统下实现apk的静默安装和启动
- 理论知识整理(C语言)