Jokewithpermutation

Input file: joke.in
Output file: joke.out
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 and output
joke.in

4111109876532

joke.out

4 1 11 10 9 8 7 6 5 3 2

题目链接:http://codeforces.com/gym/100553/attachments/download/2885/20142015-acmicpc-northeastern-european-regional-contest-neerc-14-en.pdf




题意:输入一排数字,所有的数字之间没有空格隔开,这些数字由1-n组,字符串的长度最少为1,最多为50。输出那一排数字,并用空格隔开。

这一题是一道经典的dfs搜索,搜索到的当前状态是字符串的第i个字符,如果这个数字没有被标记,那就搜索dfs(i+1,t+1);如果i+1<len的话,那么和后面那个字符一起组合成一个数字,如果这个数字不会超过n并且没有被标记的话,那么就搜索dfs(i+2,t+1);如果都不符合的话,那就return回溯。要注意dfs搜索不符合要求的话,要记得把数字的标记状态回溯恢复一下。当i==len时,说明其中一种情况已经搜索完毕也有可能搜索成了一种非正常状态,这时就要用一个for循环遍历判断是否说有的数字均被标记。




#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[55];
int flag[55];
int x[55];
int len,n;
int gg;
void dfs(int i,int t);
int main()
{
freopen("joke.in","r",stdin);
freopen("joke.out","w",stdout);
int i;
memset(s,0,sizeof(s));
memset(x,0,sizeof(x));
memset(flag,0,sizeof(flag));
scanf("%s",s);
len=strlen(s);
if(len<10) //优化,如果都是十以内
{
n=len;
for(i=0; i<n-1; i++)
printf("%c ",s[i]);
printf("%c\n",s[i]);
}
else
{
n=(len-9)/2+9;
gg=0;
dfs(0,0);
}
return 0;
}
void dfs(int i,int t)
{
if(i==len)
{
int sign=1;
for(int j=1; j<=n; j++)
if(flag[j]==0)
{
sign=0;
break;
}
if(sign)
{
for(int j=0; j<n-1; j++)
printf("%d ",x[j]);
printf("%d\n",x[n-1]);
gg=1;
}
return;
}
if(gg==1) return;
if(flag[s[i]-48]==0)
{
flag[s[i]-48]=1;
x[t]=s[i]-48;
dfs(i+1,t+1);
if(gg) return;
flag[s[i]-48]=0;
}
if((i+1)<len&&((s[i]-48)*10+(s[i+1]-48))<=n&&flag[(s[i]-48)*10+(s[i+1]-48)]==0)
{
flag[(s[i]-48)*10+(s[i+1]-48)]=1;
x[t]=(s[i]-48)*10+(s[i+1]-48);
dfs(i+2,t+1);
if(gg) return;
flag[(s[i]-48)*10+(s[i+1]-48)]=0;
}
return;
}

  

codeforces 数字区分 搜索的更多相关文章

  1. 【BZOJ1853】幸运数字(搜索,容斥)

    [BZOJ1853]幸运数字(搜索,容斥) 题面 BZOJ 洛谷 题解 成功轰下洛谷rk1,甚至超越了一个打表选手 这题思路很明显吧,先搞出来所有范围内的合法数字,然后直接容斥, 容斥的话显然没有别的 ...

  2. BZOJ2393 & 1853 [Scoi2010]幸运数字 【搜索 + 容斥】

    题目 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是" ...

  3. Codeforces 58E Expression (搜索)

    题意:给你一个可能不正确的算式a + b = c, 你可以在a,b,c中随意添加数字.输出一个添加数字最少的新等式x + y  = z; 题目链接 思路:来源于这片博客:https://www.cnb ...

  4. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...

  5. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  6. 【BZOJ1853】[Scoi2010]幸运数字 容斥原理+搜索

    Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,88 ...

  7. Cleaner Robot - CodeForces 589J(搜索)

    有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面 ...

  8. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  9. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

随机推荐

  1. grabcut 分割 Rect

    #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src = imread("E:\ ...

  2. 05_ssm基础(六)之SpringMVC

    36.springMVC之入门 1.springMVC简介 Spring MVC是基于MVC模式的一个Web框架,它解决WEB开发中常见的问题(参数接收.文件上传.表单验证.国际化.等等),而且使用简 ...

  3. appium API接口

    appium API接口 标签(空格分隔): appium常用api 1.contexts contexts(self) 返回当前会话的上下文,使用可以识别H5页面的控件: driver.contex ...

  4. Cacti日志时区问题

    cacti默认是以美国的时间为准的,监测的适合要纠正到UTC+8的时区. 打开vi /home/cacti/include/config.php 文件,在里面加入一行 date_default_tim ...

  5. windows上java中文乱码-指定字符集 -Dfile.encoding=UTF-8

    jvm启动中增加参数: -Dfile.encoding=UTF-8 重启即可.

  6. js实现商品颜色尺码联动以及购买数量的选择

    <script type="text/javascript"> $(function(){ //初始化点击第一个颜色 jquery $("#colors a: ...

  7. flash Air 在同一个目录下面创建txt,写入字

    import flash.filesystem.*; var file:File=new File(File.applicationDirectory.nativePath + '/HelloWorl ...

  8. linux下的C++项目创建

    CMake项目的完整构建 Linux下的CMake项目通常由几个文件夹组成.小伙伴们可以先在自己的电脑上新建一个文件夹,作为你代码的根目录,然后往里面建几个子文件夹,这里并不涉及具体的代码,只是可以作 ...

  9. 98. Validate Binary Search Tree (Tree; DFS)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  10. java链接JDBC中的?问题

    String sql = "select * from student where name= ?"; PreparedStatement pst = conn.prepareSt ...