题目

链接

题意:一个$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. Java基础语法知识你真的都会吗?

    第一阶段 JAVA基础知识 第二章 Java基础语法知识 在我们开始讲解程序之前,命名规范是我们不得不提的一个话题,虽说命名本应该是自由的,但是仍然有一定的"潜规则",通过你对命名 ...

  2. [转帖]Cookie与Passport安全

    Cookie与Passport安全 https://www.cnblogs.com/xinzhao/p/6395153.html 感觉自己对网络安全性的理解还是不深入 不透彻. 对于web系统而言,由 ...

  3. Windows 与 linux文件相互传输的方法

    公司里面办公机器大部分都是 windows 但是现在随着云计算.docker.linux等的越来越兴起,需要大量的操作linux服务器. 最重要和最直接的需要将windows 上面的文件上传到 lin ...

  4. 【AtCoder】AGC002

    AGC002 A - Range Product #include <bits/stdc++.h> #define fi first #define se second #define p ...

  5. 创建Vofuria工程,获取产品密钥

    进入Vofuria官网 https://developer.vuforia.com/vui/develop/licenses/free/new 然后点击 然后在License Name中填写izji刚 ...

  6. 级联-city

    <!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title> ...

  7. python3 虚拟环境的创建

    创建虚拟环境的方法有很多种,我来分享一下我最常用的虚拟环境的创建方法和一些命令的使用, 什么是虚拟环境? 知道的可以略过,不知道的可以听我简单的说下.虚拟环境这四个字,一听你就明白什么意思了,首先理解 ...

  8. 【AC自动机】Censoring

    [题目链接] https://loj.ac/problem/10059 [题意] 有一个长度不超过  1e5 的字符串 .Farmer John 希望在 T 中删掉 n 个屏蔽词(一个屏蔽词可能出现多 ...

  9. cas sso 整合记录

    首先说明下,我使用的cas-server版本是4.2.1 整合过程中遇到的问题及解决方式如下 1.因为使用https的话证书是个麻烦事,所以启用http 修改cas-server-webapp下的ca ...

  10. winfrom_关于打印小票

    1.使用的是PrintDocument控件,在工具箱  ,将其托到窗体上: 2. private void btnprint_Click(object sender, EventArgs e) { p ...