Jokewithpermutation (DFS)
Problem J. 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 joke.out
4111109876532 4 1 11 10 9 8 7 6 5 3 2
这是区域赛的题目!!!!
DFS 暴力搜索 看了yan的代码 自己理解了一下
注意几点 1.vetor的使用 姿势
2.字符串长度判断最大n
3.一位或二位字符组成一个数
4.dfs姿势要熟练!!!!!
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
vector<int>re;
int s=;
int po;
char a[];
int len;
void dfs(int n)
{
if(n==len)
{
int flag=;
for(int i=;i<=s;i++)
{
if(!mp[i])
flag=;
}
//cout<<re.size()<<endl;
if(flag)
{
for(unsigned int i=;i<re.size()-;i++)
printf("%d ",re[i]);
printf("%d\n",re[re.size()-]);
po=;
}
return ;
}
if(po)
return ;
if(!mp[a[n]-''])
{
mp[a[n]-'']=;
re.push_back(a[n]-'');
dfs(n+);
if(po)
return ;
re.pop_back();
mp[a[n]-'']=;
}
if(!mp[(a[n]-'')*+a[n+]-'']&&((a[n]-'')*+a[n+]-'')<=s&&po==&&n+<len)
{
mp[(a[n]-'')*+a[n+]-'']=;
re.push_back((a[n]-'')*+a[n+]-'');
dfs(n+);
if(po)
return;
mp[(a[n]-'')*+a[n+]-'']=;
re.pop_back();
}
}
int main()
{
freopen("joke.in","r",stdin);
freopen("joke.out","w",stdout);
gets(a);
len=strlen(a);
re.clear();
mp.clear();
if(len<=)
{
for(int i=; i<len-; i++)
printf("%c ",a[i]);
printf("%c\n",a[len-]);
return ;
}
po=;
s=(len-)/+;
dfs();
return ;
}
Jokewithpermutation (DFS)的更多相关文章
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
- DFS Gym 100553J Jokewithpermutation
题目传送门 /* 题意:将字符串分割成一个全排列 DFS:搜索主要在一位数和两位数的处理,用d1, d2记录个数,在不饱和的情况下,两种都试一下 DFS还是写不来,难道是在家里懒? */ #inclu ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
随机推荐
- 【button】 按钮组件说明
原型: <button size="[default | mini]" type="[primary | default | warn]" plain=& ...
- Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】
Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-26 | 本文接之前的<Spring Clou ...
- Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】
Spring Cloud(十):服务网关 Zuul(路由)[Finchley 版] 发表于 2018-04-23 | 更新于 2018-05-09 | 通过之前几篇 Spring Cloud 中 ...
- 使用maven构建web项目(简易版)
在eclipse中使用maven开发一个web项目 第一步:安装maven:在Windows上安装Maven 中间省略很多步骤....(包括关于eclipse中配置maven) 第二步:不用懂任何ma ...
- 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道——最小分割分数
题目: 给 n 个正整数 a_1,…,a_n, 将 n 个数顺序排成一列后分割成 m 段,每一段的分数被记为这段内所有数的和,该次分割的分数被记为 m 段分数的最大值.问所有分割方案中分割分数的最小值 ...
- Python3 Tkinter-Scrollbar
1.创建 from tkinter import * root=Tk() Scrollbar(root).pack() root.mainloop() 2.设置silder的位置 from tkint ...
- Java经典问题
1.JAVA初学者都应该搞懂的问题 对于这个系列里的问题,每个学Java的人都应该搞懂.当然,如果只是学Java玩玩就无所谓了.如果你认为自己已经超越初学者了,却不很懂这些问题,请将你自己重归初学者行 ...
- Python中的print
Python 3.X的print 在Python 3.X中,print是一个内置函数,完整的声明形式如下: print([object, ...][, sep=' '][, end='\n'][, f ...
- 左值&右值
一.引子 我们所谓的左值.右值,正确的说法应该是左值表达式.右值表达式. 因为C++的表达式不是左值就是右值. 在C中,左值指的是既能够出现在等号左边也能出现在等号右边的表达式,右值指的则是只能出现在 ...
- java集合类小结
1 集合的框架体系 List简介 集合的使用场合 List(链表|线性表)和Set(集) java.util.Collection ---| Collection 描述所有接口的共性 ----| Li ...