题目:

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);}
}
栈:
其实和dfs差不多只不过看起来没那么迷糊!
#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);elseprintf("%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]);}
}

转载于:https://www.cnblogs.com/GoldenFingers/p/9107354.html

DFS、栈、双向队列:CF264A- Escape from Stones相关推荐

  1. 剑指offer:按之字形打印二叉树(栈|双向队列+中序遍历)

    1. 题目描述 /**请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. */ 2. 双向队列 /*思路: ...

  2. 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)

    1. 题目描述 /**请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /**1.只要pRoot.left和pRo ...

  3. 双向(端)链表、栈、队列

    双端链表 双端栈 双端队列 从实用角度,感受不出双端队列的好处,但其可以充当栈和队列的角色. 参考资料:http://baike.baidu.com/view/1627726.htm Test sta ...

  4. [LeetCode]-Python刷题第三周(栈和队列)

    20. Valid Parentheses 合法括号(Easy) Given a string containing just the characters '(', ')', '{', '}', ' ...

  5. Algs4-1.3.33一个双向队列Deque-双向链表实现

    1.3.33Deque.一个双向队列(或者称为deque)和栈或队列类似,但它同时支持在两端添加或删除元素.Deque能够存储一组元素并支持表1.3.9中的API: 表1.3.9泛型双向队列的API ...

  6. android栈和队列

    android栈和队列 栈和队列是两种特殊的线性表,它们的逻辑结构和线性表相同,只是其运算规则较线性表有更多的限制,故又称它们为运算受限的线性表.LinkedList数据结构是一种双向的链式结构,每一 ...

  7. char栈java,Java数据结构与算法-栈和队列(示例代码)

    (摘录加总结)------ 栈和队列不属于基础的数据结构,它们都属于线性表. 一.栈 对于栈存储操作元素只能在栈结构的一端进行元素的插入和删除,是一种性质上的线性表结构.按照"先进后出&qu ...

  8. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  9. 栈和队列都是什么结构_数据结构与算法之初识栈与队列

    栈和队列 学习目标 本节我们将初步认识栈和队列,栈和队列是限定插入和删除只能在表的"端点"进行的线性表. 开始学习 01栈 是什么? 限定仅在表尾进行插入和删除操作的线性表,表尾- ...

最新文章

  1. 【python教程入门学习】Python实现自动玩贪吃蛇程序
  2. ArrayBlockingQueue队列
  3. 【AI初识境】深度学习模型中的Normalization,你懂了多少?
  4. Android 隐藏状态栏,沉浸式状态栏,状态栏背景色,状态栏字体色,透明状态工具类
  5. Java synchronized到底锁住的是什么?
  6. Xamarin Android中引用Jar包的方法
  7. Python+Tensorflow+CNN实现车牌识别
  8. xampp中mysql使用教程_XAMPP的安装及使用教程
  9. Linux (Ubuntu): bash: tailf: command not found
  10. 有道云笔记本 html,有道云笔记怎么保存网页 有道云笔记保存路径在哪
  11. 【转】图像视觉开源代码
  12. Kafka:分布式消息系统
  13. Android平台epub阅读器推荐
  14. 100个精选Python实战项目案例,初学者练手必备
  15. 劳易测BCB G40 H47 L030 - 条码带
  16. 统计一个字符串中大写字母,小写字母,以及数字的个数。
  17. 简单实现基于 STM32F407+ESP8266+RFID 的物联网小项目
  18. 3D模型汇总----骨骼模型
  19. window电脑可以上QQ但是连不上网解决办法
  20. 中国数据竞赛解决方案汇总

热门文章

  1. TScreen 类 - 获取字体列表
  2. SSL只是基本安全措施
  3. 给计算机专业学生的忠告
  4. linux怎么获取当前路径,linux 下获取当前工作路径的实例
  5. Mac用Java写出hello,mac中c#的helloworld
  6. 如何定义中文转语音的语气
  7. String 中的hashCode方法
  8. php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理
  9. 关于运维体系谈谈我的想法
  10. Hibernate中的实体映射