题目

链接

题意:一个$1$到$n$的序列被去掉空格,需要将其还原。例如$4111109876532$可还原成$4 \ 1 \  11 \  10 \  9 \  8 \  7 \  6 \  5 \  3 \  2$。字符串的长度在$1$到$100$之间。

解决方法

由于是1到n的序列,根据长度可以求出n。

由于长度不超过100,所以n不超过54,即最多两位数。因此只需分别取首部的1位和2位,进行记录,对剩下的递归。

 #include<bits/stdc++.h>
using namespace std; const int maxn = +;
char s[maxn];
int n, len; int getnum1(int index)
{
return s[index] - '';
}
int getnum2(int index)
{
return (s[index] - '') * + s[index + ] - '';
} int res[maxn];
bool vis[maxn];
int cnt = ;
bool dfs(int index)
{
if(index >= len)
{
bool flag = true;
for(int i = ; i < n;i++)
if(!vis[i]) flag = false;
if(flag)
{
for(int i = ;i < cnt;i++)
printf("%d%c", res[i], i == cnt - ? '\n' : ' ');
return true;
}
return false;
} int num1 = getnum1(index);
if(num1 > n) return false;
if(!vis[num1])
{
vis[num1] = true;
res[cnt++] = num1;
if(dfs(index + )) return true; vis[num1] = false;
cnt--;
} int num2 = getnum2(index);
if(num2 > || num2 > n) return false; //长度为100,n最大为54 if(index < len - && !vis[num2])
{
vis[num2] = true;
res[cnt++] = num2;
if(dfs(index + )) return true; vis[num2] = false;
cnt--;
} return false;
} int main()
{
scanf("%s", s);
len = strlen(s);
if(len <= ) n = len;
else n = (len - ) / + ; dfs(); return ;
}

置换的玩笑——DFS&&暴力的更多相关文章

  1. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  2. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  3. hdu 5612 Baby Ming and Matrix games(dfs暴力)

    Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...

  4. hdu 1010 Tempter of the Bone(dfs暴力)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  5. NOIP 2002提高组 选数 dfs/暴力

    1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…, ...

  6. 计蒜客 置换的玩笑(DFS)

    传送门 题目大意: 小蒜头又调皮了.这一次,姐姐的实验报告惨遭毒手. 姐姐的实验报告上原本记录着从 1 到 n 的序列,任意两个数字间用空格间隔.但是“坑姐”的蒜头居然把数字间的空格都给删掉了,整个数 ...

  7. Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力

    B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...

  8. UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)

    题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...

  9. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

随机推荐

  1. Oracle数据库应用系统结构

    在安装.部署oracle数据库软件时,需要根据不同应用结构(即硬件平台.操作系统平台)采用不同的方法(基本安装.高级安装),下面介绍几种常见的应用结构. 1.应用系统的数据接口 客户端应用程序或应用服 ...

  2. Django2.2连接mysql数据库出现django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None问题

    在使用Django2.2开发的时候,想要使用mysql数据库,在settings.py文件中更改命令: DATABASES = { 'default': { 'ENGINE': 'django.db. ...

  3. mac更新后,xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    解决方案: xcode-select --install

  4. dede时间标签

    dedecms首页时间标签:1.12-27 样式([field:pubdate function='strftime("%m-%d",@me)'/]) 2.May 15, 2009 ...

  5. css 颜色自动变化 炫彩

    .layui-icon-login-qq:hover{ color:rgb(0, 156, 255); transition: 0.5s; animation:change 10s linear 0s ...

  6. 美团CodeM初赛B轮 合并字符串的价值 (线段树,分类讨论)

    输入两个字符串a和b,合并成一个串c,属于a或b的字符在c中顺序保持不变.如"ACG"和"UT"可以被组合成"AUCTG"或"AC ...

  7. Oracle练习(一)

    Oracle 1.检索部门编号.部门名称.部门所在地及其每个部门的员工总数. select d.deptno,d.dname,d.loc,count(*) from emp e,dept d wher ...

  8. Scala学习二十一——隐式转换和隐式参数

    一.本章要点 隐式转换用于类型之间的转换 必须引入隐式转换,并确保它们可以以单个标识符的形式出现在当前作用域 隐式参数列表会要求指定类型的对象.它们可以从当前作用域中以单个标识符定义的隐式对象的获取, ...

  9. boost random library的使用

      生成满足一定分布的随机数,是统计模拟.系统仿真等应用中最基本的要求.matlab中提供了函数可以生成各种常见分布的随机数,c++使用boost random库也可以很容易实现. 一.例子 boos ...

  10. js之数据类型(对象类型——构造器对象——正则)

    正则(regular expression)描述了一种字符串的匹配式.一般应用在一些方法中,用一些特殊的符号去代表一些特定的内容,对字符串中的信息实现查找,替换,和提取的操作.js中的正则表达式用Re ...