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. 获取镜像tag

    # curl -k https://k8s.gcr.io/v2/fluentd-elasticsearch/tags/list|jq .tags % Total % Received % Xferd ...

  2. 递归算法结合数据库 解析 java树形结构

    1.准备表结构及对应的表数据a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节 ...

  3. ./configure: error: the HTTP rewrite module requires the PCRE library解决

    ./configure: error: the HTTP rewrite module requires the PCRE library解决   有时候,我们需要单独安装nginx,来处理大量的下载 ...

  4. FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK

    [FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK] 1.FLAG_ACTIVITY_NEW_TASK 2.FLAG_ACTIVITY_CLEAR_T ...

  5. Emulating private methods with closures

    [Emulating private methods with closures] JavaScript does not provide a native way of doing this, bu ...

  6. Access sql语句创建表及字段类型(转)

    http://www.cnblogs.com/hnyei/archive/2012/02/23/2364812.html 创建一张空表: Sql="Create TABLE [表名]&quo ...

  7. sql去除重复语句

    转自芙蓉清秀的BLOG http://blog.sina.com.cn/liurongxiu1211  sql去除重复语句 (2012-06-15 15:00:01) sql 单表/多表查询去除重复记 ...

  8. avcodec_decode_video2少帧问题

    使用libav转码视频时发现一个问题:使用下面这段代码解码视频时,解码中会不时丢掉几帧. ){ ret = avcodec_decode_video2(video_dec_ctx, vframe, & ...

  9. 缩点+最小路径覆盖 hdu 3861

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...

  10. Gym - 101911C Bacteria (规律题)

    传送门:点我 Time limit2000 ms Memory limit262144 kB Recently Monocarp has created his own mini-laboratory ...