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. mysql 和 Oracle 数据类型对照

    MySQL与Oracle两种数据库在工作中,都是用的比较多的数据库,由于MySQL与Oracle在数据类型上有部分差异,在我们迁移数据库时,会遇上一定的麻烦,下面介绍MySQL与Oracle数据库数据 ...

  2. 基于docker的wekan部署

    镜像地址: https://hub.docker.com/r/wekanteam/wekan/ wiki: https://github.com/wekan/wekan/wiki#Developmen ...

  3. Real Time Rendering 2

    [Real Time Rendering 2] 1.The light vector l is usually defined pointing in a direction opposite to ...

  4. OWAPSP_ZAP使用

    启动OWAPSP_ZAP后 netstat -pantu | grep 8080

  5. 使用tor网络

    在www.torproject.org/projects/torbrowser.html.en上找到合适的版本下载 下载好tor浏览器之后,解压双击Tor Browser,出现这个错误 这是因为kal ...

  6. Jmeter(十九) Md5加密操作之-------BeanShell PreProcessor(转载)

    转载自 http://www.cnblogs.com/yangxia-test 背景: 有一些登录会做一些md5校验,通过jmeter的BeanShell可以解决MD5加密情况. 1.首先需要一个解码 ...

  7. JMeter学习(二)录制脚本(转载)

    转载自 http://www.cnblogs.com/yangxia-test 环境 Badboy  version 2.1.1 JDK: 1.7.0_67 Apache  JMeter-2.11 - ...

  8. Python基础之Python分类

    python环境 编译型: 一次性将所有程序编译成二级制文件,开发效率极低,因为一旦出现BUG所有的程序需要全部重新编译 缺点: 开发效率低,不能跨平台 优点: 执行速度快 解释型: 当程序执行时,一 ...

  9. scrapy 琐碎的东西

    1.深度指定 DEPTH_LIMIT=1 2.常用命令 scrapy startproject name scrapy genspider name name.com scrapy crawl nam ...

  10. 安卓自动生成代码插件-Android code Generator(转)

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 介绍 A ...