题目:

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);
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的更多相关文章

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

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

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

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

  3. python 模块一(random,counter,defaultdict,time,wraps,reduce) 栈 队列 双向队列

    ####################总结####################### 模块:你写的py文件 引用其他模块 1.import 模块 2.from 模块 import 功能,类,变量 ...

  4. 算法进阶面试题02——BFPRT算法、找出最大/小的K个数、双向队列、生成窗口最大值数组、最大值减最小值小于或等于num的子数组数量、介绍单调栈结构(找出临近的最大数)

    第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) pu ...

  5. Python 栈和队列,双向队列

    # 栈 # 特点: 先进后出 class StackFullException(Exception): pass class StackEmptyException(Exception): pass ...

  6. python基础知识4——collection类——计数器,有序字典,默认字典,可命名元组,双向队列

    1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能  Counter 我们从中挑选一些相对常用的方法来举例: 在上面的例子 ...

  7. YTU 3006: 迷宫问题(栈与队列)

    3006: 迷宫问题(栈与队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 1 题目描述 编写一个求解迷宫问题的程序,要求输出迷宫的所有路径,并求最短路径长度及最短路径 ...

  8. 线性表 及Java实现 顺序表、链表、栈、队列

    数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...

  9. PHP — 用PHP实现一个双向队列

    1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...

随机推荐

  1. Spring注解之@Lazy注解,源码分析和总结

    一 关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类.spring的好处就是文档都在代码里,网上百度大多是无用功. 不如,直接看源码.所以把当时源码分析的思路丢上来一波. 二 源码分析 ...

  2. json解析数组类型的数据

    //微信里一个检测是否有发送模版消息的权限的方法//此处的openid代表的微信用户openid,templateId代表的是模版消息idpublic boolean checkIsSendTempM ...

  3. kickstart2019 round_A B. Parcels

    思路: 利用了曼哈顿距离和切比雪夫距离之间的转化. 参考: https://blog.csdn.net/Dylan_Frank/article/details/88985444 https://www ...

  4. Mybatis介绍(一)

    这里介绍的mybatis比较简单, 我做为一个初学者, 记录下个人在学习中方法, 如果那里出错, 希望读者朋友们见谅. 首先这里介绍一下我们下面用的表结构: author表是保存了作者的个人信息, 因 ...

  5. 解决“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问……”【转】

    SQL Server 阻止了对组件 /'Ad Hoc Distributed Queries/' 的访问 在Sql Server中查询一下Excel文件的时候出现问题: SELECT *  FROM ...

  6. pat甲级1114

    1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...

  7. linux 命令——7 mv(转)

    mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...

  8. SVN和Git的区别

    这个地方就简单介绍一下 svn 的模式是: 1.写代码. 2.从服务器拉回服务器的当前版本库,并解决服务器版本库与本地代码的冲突. 3.将本地代码提交到服务器. Git分布式版本管理的模式是: 1.写 ...

  9. tensorflow构建CNN模型时的常用接口函数

    (1)tf.nn.max_pool()函数 解释: tf.nn.max_pool(value, ksize, strides, padding, data_format='NHWC', name=No ...

  10. AngularJs学习笔记-数据绑定、管道

    数据绑定.管道 (1)数据绑定(Angular中默认是单向绑定) 1.[]方括号 可以用于子组件传值 由于是单向绑定,所以当子组件中的iStars属性发生改变时,不会影响到父组件中product.ra ...