DFS、栈、双向队列:CF264A- Escape from Stones
题目:
Squirrel Liss liv
Escape from Stonesed in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from
the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the
right, her new interval will be [k, k + d].
You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r".
Output
Output n lines — on the i-th line you should print the i-th stone's number from the left.
Example
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
解题心得:
1、刚开始看到这个题的时候挺蒙蔽的,第一想法就是使用暴力,但是毫无疑问的是暴力肯定会将精度丢失,即使骗数据过了也容易被Hack。其实这个题有很多的解法
2、先说思路,题意上面说的很明白,当向左的时候最左方的一个是第一个r的前一个,最左方的r的是第一个r出现的时候,可以画一个图了解一下。
3、做法很多,了解了思路之后很简单,直接贴代码:
DFS:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
char a[maxn];
int n;
void dfs(int step)
{
if(step == n)
return;
if(a[step] == 'l')
{
dfs(step+1);
printf("%d ",step+1);
}
if(a[step] == 'r')
{
printf("%d ",step+1);
dfs(step+1);
}
}
int main()
{
while(scanf("%s",a)!=EOF)
{
n = strlen(a);
dfs(0);
}
}
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
char a[maxn];
int main()
{
scanf("%s",a);
int len = strlen(a);
stack <int> s;
for(int i=0; i<len; i++)
{
if(a[i] == 'l')
s.push(i+1);
else
printf("%d ",i+1);
}
while(!s.empty())
{
printf("%d ",s.top());
s.pop();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int k[maxn];
char s[maxn];
int main()
{
scanf("%s",s);
{
int len = strlen(s);
int start = 0,End = len - 1;
for(int i=0 ;i<len ;i++)
{
if(s[i] == 'l')
{
k[start] = i+1;
start++;
}
if(s[i] == 'r')
{
k[End] = i+1;
End--;
}
}
for(int i=len-1;i>=0;i--)
printf("%d ",k[i]);
}
}
DFS、栈、双向队列:CF264A- Escape from Stones的更多相关文章
- 剑指offer:按之字形打印二叉树(栈|双向队列+中序遍历)
1. 题目描述 /** 请实现一个函数按照之字形打印二叉树, 即第一行按照从左到右的顺序打印, 第二层按照从右至左的顺序打印, 第三行按照从左到右的顺序打印, 其他行以此类推. */ 2. 双向队列 ...
- 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)
1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...
- python 模块一(random,counter,defaultdict,time,wraps,reduce) 栈 队列 双向队列
####################总结####################### 模块:你写的py文件 引用其他模块 1.import 模块 2.from 模块 import 功能,类,变量 ...
- 算法进阶面试题02——BFPRT算法、找出最大/小的K个数、双向队列、生成窗口最大值数组、最大值减最小值小于或等于num的子数组数量、介绍单调栈结构(找出临近的最大数)
第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) pu ...
- Python 栈和队列,双向队列
# 栈 # 特点: 先进后出 class StackFullException(Exception): pass class StackEmptyException(Exception): pass ...
- python基础知识4——collection类——计数器,有序字典,默认字典,可命名元组,双向队列
1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 Counter 我们从中挑选一些相对常用的方法来举例: 在上面的例子 ...
- YTU 3006: 迷宫问题(栈与队列)
3006: 迷宫问题(栈与队列) 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 1 题目描述 编写一个求解迷宫问题的程序,要求输出迷宫的所有路径,并求最短路径长度及最短路径 ...
- 线性表 及Java实现 顺序表、链表、栈、队列
数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...
- PHP — 用PHP实现一个双向队列
1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...
随机推荐
- java向上取整向下取整
向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...
- C#简单代码转移数据库数据
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;u ...
- hibernate丢失更新
如果多个线程操作基于同一个查询结构对表中的记录进行修改,那么后修改的记录将会覆盖前面修改的记录,前面的修改就丢失掉了,这就叫做更新丢失.Serializable可以防止更新丢失问题的发生.其他的三个隔 ...
- sublime 常用快捷键(转)
Sublime text 3是码农最喜欢的代码编辑器,每天和代码打交道,必先利其器,掌握基本的代码编辑器的快捷键,能让你打码更有效率.刚开始可能有些生疏,只要花一两个星期坚持使用并熟悉这些常用的快捷键 ...
- ps钢笔工具路径问题
问题描述:ps钢笔工具画出路径后用文字工具打字 路径出现一个空心圆点字,不能在路径上打字或者无法确认终止的位置. 解决:1.如果要在路径上全都打满字,要将文字对齐改为左对齐,2.如果要实现自定义结束位 ...
- CSS单词换行and断词,你真的完全了解吗
背景 某天老板在群里反馈,英文单词为什么被截断了? 很显然,这是我们前端的锅,自行背锅.这个问题太简单了,css里加两行属性,分分钟搞定. 开心的提交代码,刷新页面.我擦,怎么还是没有断词?不可能啊! ...
- css 选择器 & UI元素的伪类选择器 & 伪元素选择器
UI元素的伪类选择器 1. :focus 用来选取获取焦点事件 2. :enabled 用来指定当元素处于可用状态时的样式 3. :disable 用来指定元素处于不可用时的状态 表单里应用 ...
- 基于python3.6的如何爬取百度地图
先前参考了其他的代码,大多数是python2.7写的,而3.6用的类库以及规则有了很大的变动,所以自己写了一个这样的代码,供给大家参考. def get_station(i): station=[] ...
- SQLSERVER编译与重编译
SQLSERVER编译与重编译 编译的含义 当SQLSERVER收到任何一个指令,包括查询(query).批处理(batch).存储过程.触发器(trigger) .预编译指令(prepared st ...
- shell 快速浏览
总结自: https://github.com/qinjx/30min_guides/blob/master/shell.md: http://blog.itpub.net/14293828/view ...