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. BOS物流项目第十三天

    教学计划 1.Quartz概述 a. Quartz介绍和下载 b. 入门案例 c. Quartz执行流程 d. cron表达式 2.在BOS项目中使用Quartz创建定时任务 3.在BOS项目中使用J ...

  2. 微信小程序----搜索框input回车搜索事件

    在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text"  placeholder="搜索" value="{ ...

  3. apache启动报错(98)Address already in use: make_sock: could not bind to address [::]:80

    说明80端口被用 终端:  ps -ef|grep httpd察看占用的进程或者用netstat -lnp|grep 80 找到后kill掉,如果都不行那么再试试以下方法(试过可以) 终端输入: fi ...

  4. webpack config

    [webpack config] 1.entry Simple rule: one entry point per HTML page. SPA: one entry point, MPA: mult ...

  5. linus jsch文件下载

    package com.osplat.util;import java.io.File;import java.io.FileNotFoundException;import java.io.File ...

  6. SQLServer 学习笔记 序

    邀月的博客 http://www.cnblogs.com/downmoon/archive/2011/03/10/1980172.html

  7. spket插件安装并设置JQuery自动提示(转)

    spket是一个开发JavaScript.jQuery.Ext_js等的开发工具,它可以 是独立的IDE,也可以作为Eclipse的插件使用,下面介绍如何在Eclipse中安装spket插件: 1.首 ...

  8. python--第二天总结

    一.作用域只要变量在内存中存在,则就可以使用.(栈) 二.三元运算result = 值result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result ...

  9. Javascript重点汇总

    LazyMan 实现LazyMan(什么是LazyMan?请自行google) function _LazyMan(_name) { var _this = this; _this.tasks = [ ...

  10. 树的子结构(python)

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # -*- coding:utf-8 -*- # class TreeNode: # def __ ...