题目链接:

http://poj.org/problem?

id=1780

Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2037   Accepted: 751

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

Source

[Submit]   [Go Back]   [

problem_id=1780" style="text-decoration:none">Status]  
[Discuss]

题目意思:

n位password,每位能够是0~9随意一个,求一个串,使得这个串中随意两个n位都不同样。

解题思路:

欧拉路径+手动开栈

欧拉路径求法是dfs走到不能走的时候后纪录路径。然后逆序输出。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
//#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 1000000
int n,s;
int List[Maxn+10],St[Maxn+10];
char ans[Maxn+10]; void solve(int cur,int M)
{
while(List[cur]<10)
{
int w=cur*10+List[cur];
List[cur]++;
St[++s]=w;
cur=w%M;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n)&&n)
{
int M=pow(10.0,double(n-1)); for(int i=0;i<M;i++)
List[i]=0;
s=0;
int a=0;
solve(0,M); while(s)
{
ans[++a]=St[s]%10+'0';
s--;
solve(St[s+1]/10,M);
}
for(int i=1;i<n;i++)
printf("0");
while(a)
printf("%c",ans[a--]);
putchar('\n'); }
return 0;
}

[欧拉回路+手动开栈] poj 1780 Code的更多相关文章

  1. 【手动开栈】【dfs序】【树状数组】【Tarjan】bzoj2819 Nim

    考虑树状数组区间修改(只对其子树的答案有影响)点查询,每个点记录的是它到根路径上的权值异或和. 答案时query(L)^query(R)^a[lca]. 这种方法在支持区间加法.减法的树上询问的时候可 ...

  2. POJ 1780 Code(欧拉回路+非递归dfs)

    http://poj.org/problem?id=1780 题意:有个保险箱子是n位数字编码,当正确输入最后一位编码后就会打开(即输入任意多的数字只有最后n位数字有效)……要选择一个好的数字序列,最 ...

  3. poj 1780 Code

    //题目描述:KEY公司开发出一种新的保险箱.要打开保险箱,不需要钥匙,但需要输入一个正确的.由n位数字组成的编码.这种保险箱有几种类型,从给小孩子玩的玩具(2位数字编码)到军用型的保险箱(6位数字编 ...

  4. poj 1780 code(欧拉路)

    /* 对于n为密码想要序列最短 那么 1234 2345 这两个一定挨着 就是说 前一个的后n-1位是后一个的前n-1位 假设n==3 我们用0-99作为点的编号建图 然后每个点连出去10条边 两个相 ...

  5. POJ 1780 Code(有向图的欧拉通路)

    输入n(1<=n<=6),输出长度为10^n + n -1 的字符串答案. 其中,字符串以每n个为一组,使得所有组都互不相同,且输出的字符串要求字典序最小. 显然a[01...(n-1)] ...

  6. Cells UVALive - 3486(dfs序+手动开栈)

    给一棵树,每次每次询问一个点是否是另一个点的祖先? 输入时是每个下标对应节点的儿子的数量 用dfs序 时间戳.. 如果一个点是另一个点的祖先,那么它的两个标记一定在祖先的范围之内 #include & ...

  7. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  8. poj 1780 , poj 1392 欧拉回路求前后相互衔接的数字串

    两道题目意思差不多 第一题是10进制 , 第二题是2进制的 都是利用欧拉回路的fleury算法来解决 因为我总是希望小的排在前面,所以我总是先将较小数加入栈,再利用另一个数组接收答案,但是这里再从栈中 ...

  9. HDU 4612 Warm up(手动扩栈,求树上哪两个点的距离最远)

    题目大意: 给你一个无向图,问加一条边之后最少还剩下几座桥. (注意重边处理)   分析:其实当我们把边双连通分量给求出来之后我们就能将连通块求出来,这样我们就可以重新构图.重新构造出来的图肯定是一颗 ...

随机推荐

  1. 【SQL】行转列过滤,使用别名和不使用别名的区别用法。

    需求为: 仿太平洋网站筛选. 多选类型的字段应采用‘并且’:单选和录入类型的字段应采用‘或者’ 比如有如下选项: 参数头 参数体 操作系统(多选) win7 win8 运行内存(单选) 2G 4G 商 ...

  2. C++静态成员函数和静态成员变量的探索

    静态数据成员属于类,非属于类对象,所以,定义位置就有了限制. 静态数据成员要实际地分配空间,故不能在类的声明中定义(只能声明数据成员).类声明只声明一个类的“尺寸和规格”,并不进行实际的内存分配,所以 ...

  3. HTTPS 中双向认证SSL 协议的具体过程

    HTTPS 中双向认证SSL 协议的具体过程: 这里总结为详细的步骤: ① 浏览器发送一个连接请求给安全服务器.② 服务器将自己的证书,以及同证书相关的信息发送给客户浏览器.③ 客户浏览器检查服务器送 ...

  4. 为什么国内的网盘公司都在 TB 的级别上竞争,成本会不会太高?(还有好多其它回复)

    作者:杜鑫链接:http://www.zhihu.com/question/21591490/answer/18762821来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...

  5. ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解

    原文 ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details 方法 打开M ...

  6. ios创建画笔的样例(双笔画效果)

    定义一个UIView:主要是在这个View里面加一个UIImageView,绘图都在这个UIImageView里面进行 @property(nonatomic) CGPoint prePoint; / ...

  7. eclipse上 安装php插件

    首先在安装之前需要有eclipse   以及SDK环境已经搭建好 eclipse开发工具下载路径: http://dl.oschina.net/soft/eclipse java sdk下载路径: h ...

  8. 变相的取消Datagridview控件的选中状态

    思路:把每一列的文字颜色设为黑色,选中时候的背景为白色,颜色为黑色.每一列都这样设置,那么变相的达到了取消选中效果. 图:

  9. RequiredFieldValidator控件--必填验证控件

    RequiredFieldValidator控件: ·RequiredFieldValidator控件也被称之为必填验证控件,顾名思义,也就是与RequiredFieldValidator控件关联的控 ...

  10. NSStringDrawingOptions

    如果options参数为NSStringDrawingUsesLineFragmentOrigin,那么整个文本将以每行组成的矩形为单位计算整个文本的尺寸.(在这里有点奇怪,因为字体高度大概是13.8 ...