题目链接: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. python接口自动化测试框架实现之字符串插入变量(字符串参数化)

    问题: 在做接口自动化测试的时候,请求报文是json串,但是根据项目规则必须转换成字符串,然后在开头拼接“data=” 接口中很多入参值需要进行参数化. 解决方案: 1.Python并没有对在字符串中 ...

  2. shell 一些符号的使用

    给你个全的,你在Linux环境下多试下就明白了:$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式 ...

  3. ubuntu下安装 openssl&&编译运行测试代码

    检查是否已安装 openssl: sudo apt-get install openssl 如果已安装执行以下操作:sudo apt-get install libssl-devsudo apt-ge ...

  4. WCF跨时区自动转换问题

    背景:api端 用wcf做的 客户端是silverlight, 服务和消费 不是同一个时区 状况:客户端调用返回对象有个字段是datetime ,返回的时间和数据库相差好几个小时,找了很久,最后把da ...

  5. 使用JMeter代理录制app测试脚本

    准备条件:JMeter.手机app 上一篇介绍过录制Web测试脚本的方式有两种,使用代理和使用第三方工具.本篇录制app测试脚本只讨论使用代理的方式,其他方式以后有机会再补充.其实Web和app使用代 ...

  6. 【刷题】BZOJ 4566 [Haoi2016]找相同字符

    Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别为 ...

  7. S-T平面图

    给定一个平面图和一个源点S.汇点T都在图中无边界的区域上,这样的图叫S-T平面图 我们把图中每一个独立的面看做一个点,对于每条边e,将它两侧的面连一条边,其中靠近S的一段与S相连,与T相连的一段与T相 ...

  8. HDU1863畅通工程---并查集+最小生成树

    #include<cstdio> #include<algorithm> #define MAX 105 struct edge { int from,to; long lon ...

  9. selenium - webdriver - 定位一组元素

    八种方法: find_elements_by_id() find_elements_by_name() find_elements_by_class_name() find_elements_by_t ...

  10. novaclient源码分析

    源码版本:H版 FAULT_OS_COMPUTE_API_VERSION = "1.1" 一.目录结构及概况 novaclient/ |---client.py --------- ...