题目链接:

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. BZOJ 3040: 最短路(road) ( 最短路 )

    本来想学一下配对堆的...结果学着学着就偏了... 之前 kpm 写过这道题 , 前面的边不理它都能 AC .. 我也懒得去写前面的加边了... 用 C++ pb_ds 库里的 pairing_hea ...

  2. JNI之HelloWorld

    什么是JNI? JNI java本地开发接口 JNI 是一个协议 这个协议用来沟通java代码和外部的本地代码(c/c++). 通过这个协议,java代码就可以调用外部的c/c++代码 外部的c/c+ ...

  3. HDU 4121

    http://www.bnuoj.com/v3/problem_show.php?pid=10277 //#pragma comment(linker, "/STACK:16777216&q ...

  4. RAD Studio 10 自带Demo代码汇总说明

    大家好,好多朋友来信咨询Delphi和C++Builder的移动开发.DataSnap架构等问题,希望能有Demo代码学习.其实Delphi和C++Builder本身自带有很多示例代码,已经覆盖了大部 ...

  5. ASP.NET MVC 5 学习教程:数据迁移之添加字段

    原文 ASP.NET MVC 5 学习教程:数据迁移之添加字段 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符 ...

  6. android如何添加桌面图标和卸载程序后自动删除图标

    android如何添加桌面图标和卸载程序后自动删除桌面图标,这是一个应用的安装与卸载过程对桌面图标的操作,下面与大家分享下具体是如何实现的,感兴趣的朋友可以参考下哈 1:创建图标如下 Intent i ...

  7. 编写存储过程导出oracle表数据到多个文本文件

    1.测试表和数据: create table test(id )); begin .. loop insert into test values(k,'test'||k); end loop; end ...

  8. 进入MFC讲坛的前言(二)

    MFC的WinMain 使用MFC编程的程序员刚开始都会提出这样一个问题:我的程序是从哪儿开始执行的?回答是:从WinMain()开始执行的.提出这样的问题是由于在他们所编写的MFC应用中看不到Win ...

  9. [置顶] jeecg-framework-3.3.2-RELEASE 最新版本发布

      平台介绍 JEECG(J2EE CodeGeneration)是一款基于代码生成器的智能开发平台,引领新开发模式(OnlineCoding模式->代码生成器模式->手工MERGE智能开 ...

  10. 【分享】如何使用sublime代码片段快速输入PHP头部版本声明

    作者:zhanhailiang 日期:2013-06-25 Sublime 菜单栏->Tools→New Snippet→输入以下内容: <snippet> <content& ...