题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=13341&courseid=0

Joke with permutation
Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:65536KB
Total submit users: 85, Accepted users: 57
Problem 13341 : Special judge
Problem description

Joey had saved a permutation of integers from 1 to n in a text file. All the numbers were written as decimal numbers without leading spaces.

Then Joe
made a practical joke on her: he removed all the spaces in the file.

Help
Joey to restore the original permutation after the Joe’s joke!

Input

The input file contains a single line with a single string — the Joey’s
permutation without spaces.

The Joey’s permutation had at least 1 and at
most 50 numbers.

Output

Write a line to the output file with the restored permutation. Don’t forget
the spaces!

If there are several possible original permutations, write
any one of them.

Sample Input
4111109876532
Sample Output
4 1 11 10 9 8 7 6 5 3 2
Problem Source
NEERC 2014

Submit   Discuss   Judge Status  Problems  Ranklist 

题目大意:将一串完整的字符串分成1~n个数。将空格补进去,并将其输出。

解题思路:1、注意这个n是可以求出来的

     2、一个数字一个数字比较,只要满足这个数字小于n,还有保证这个数字没有访问过就ok啦

   

详见代码。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int n,flag;
char ch[];
int ans[];
bool vis[]; bool dfs(int i,int k)
{
if (flag==)
return true;
if (k==n+)
{
for (int i=;i<k-;i++)
{
printf ("%d ",ans[i]);
}
printf ("%d\n",ans[k-]);
flag=;
return true;
}
if (ch[i]-''<=n&&!vis[ch[i]-'']&&ch[i]-''>)
{
ans[k]=ch[i]-'';
vis[ch[i]-'']=;
if(dfs(i+,k+)) return true;
vis[ch[i]-'']=;
}
if ((ch[i]-'')*+ch[i+]-''<=n&&(ch[i]-'')*+ch[i+]-''>&&!vis[(ch[i]-'')*+ch[i+]-''])
{
ans[k]=(ch[i]-'')*+ch[i+]-'';
vis[(ch[i]-'')*+ch[i+]-'']=;
if(dfs(i+,k+)) return true;
vis[(ch[i]-'')*+ch[i+]-'']=;
}
return false;
} int main()
{
while (scanf("%s",ch)!=EOF)
{
flag=;
memset(ans,,sizeof(ans));
memset(vis,,sizeof(vis));
int len=strlen(ch);
if (len>)
n=(len-)/+;
else
n=len;
dfs(,);//dfs();
}
return ;
}

HNU Joke with permutation (深搜dfs)的更多相关文章

  1. 图的遍历 之 深搜dfs

    DFS 遍历 深度优先搜索是一个递归过程,有回退过程. 对一个无向连通图,在访问图中某一起始顶点u 后,由u 出发,访问它的某一邻接顶点v1:再从v1 出发,访问与v1 邻接但还没有访问过的顶点v2: ...

  2. HDU 2553 N皇后问题(深搜DFS)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. 深搜(DFS),Image Perimeters

    题目链接:http://poj.org/problem?id=1111 解题报告: 1.这里深搜有一点要注意,对角线上的点,如果为'.',则total不应该增加,因为这不是他的边长. #include ...

  4. 深搜(DFS),回溯,Fire Net

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 解题报告: 这里的深搜有一点不同,就是,在深搜每一个点时,都要深搜每 ...

  5. 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS

    图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...

  6. 【深搜(DFS)-例题-踏青】-C++

    描述 小白和他的朋友周末相约去召唤师峡谷踏青.他们发现召唤师峡谷的地图是由一块一块格子组成的,有的格子上是草丛,有的是空地.草丛通过上下左右 4 个方向扩展其他草丛形成一片草地,任何一片草地中的格子都 ...

  7. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  8. HDU 2614 Beat 深搜DFS

    这道题目还是比较水的,但是题意理解确实费了半天劲,没办法 谁让自己是英渣呢! 题目大意: 猪脚要解决问题, 他有个习惯,每次只解决比之前解决过的问题的难度要大. 他给我们一个矩阵  矩阵的 i 行 j ...

  9. noj电子老鼠走迷宫(深搜dfs)超时错误

    1042.电子老鼠闯迷宫 时限:1000ms 内存限制:10000K  总时限:3000ms 描述 有一只电子老鼠被困在如下图所示的迷宫中.这是一个12*12单元的正方形迷宫,黑色部分表示建筑物,白色 ...

随机推荐

  1. json 和 pickle

    用于序列化的两个模块 json:用于字符串和python数据类型间进行转换 pickle:用于python特有的类型和python的数据类型间进行转换 json模块提供了四个功能:dumps dump ...

  2. Distributed transactions in Spring, with and without XA

    While it's common to use the Java Transaction API and the XA protocol for distributed transactions i ...

  3. 第124天:移动web端-Bootstrap轮播图插件使用

    Bootstrap JS插件使用 > 对于Bootstrap的JS插件,我们只需要将文档实例中的代码粘到我们自己的代码中> 然后作出相应的样式调整 Bootstrap中轮播图插件叫作Car ...

  4. 【JavaScript&jQuery】购物车自动结算

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. [一]SpringBoot 之 HelloWorld

    (1)新建一个Maven Java工程 (2)在pom.xml文件中添加Spring BootMaven依赖 2.1在pom.xml中引入spring-boot-start-parent spring ...

  6. Matrix Walk CodeForces - 954C

    题意: 就是给出一连串的数字 这些数字是从第一个数字依次走过的 emm..就是这样..  然后让你判断这个矩阵是否存在  如果存在输出行和列的数量  其中行..开到最大就好了...主要是判断列 在输入 ...

  7. JAVA中的堆、栈等内存分析

    在 JAVA 中,有六个不同的地方可以存储数据 1. 寄存器( register ) 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...

  8. Gradle及eclipse插件安装

    1.  下载Gradle Gradle需要jdk7或以上版本,使用Java –version命令进行测试:Gradle自带了Groovy库,所以无需再安装Groovy,已经安装的Groovy会被Gra ...

  9. mysql互为主从复制配置笔记--未读,稍后学习

    MySQL-master1:192.168.72.128 MySQL-master2:192.168.72.129 OS版本:CentOS 5.4MySQL版本:5.5.9(主从复制的master和s ...

  10. Nginx报错 nginx: [error] open() "/usr/local/nginx-1.6.3/logs/nginx.pid" failed (2: No such file or directory)

    问题: 解决: http://www.jianshu.com/p/918eb337a206 dd