CodeForces - 264A - Escape from Stones(栈和队列的应用)

Squirrel Liss lived 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.

Examples

Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be在这里插入图片描述 , respectively. So you should print the sequence: 3, 5, 4, 2, 1.
题目链接

  • 题意:一只松鼠要躲避落下的岩石,岩石总是落在松鼠所在的区间。松鼠有两种选择,向左‘l’或者向右‘r’, 可以理解为岩石总是落在松鼠所在区间的中间位置,这样好想一点。然后题目会给你一个字符串,里面代表了松鼠的移动方向,要求你按照从左到右的方向输出石头的编号。
  • 题目分析:这个题目找了好多题解,说是要用二分,最后也没看懂是什么东西(还是要靠自己),然后我就想要用栈来做。先别急着说我是胡说,我第一感觉真的是用栈。我们这样想(对了,看下面叙述的过程中建议读者自己先画一个图),当一个石头落下来,掉落到区间中间,松鼠只有两个选择:向左或向右(假设原来区间为[n - k, n + k])
    1、向左,那么松鼠所在的区间就变成了[n - k, n],也就是右边已经被石头挡住了,所以这块石头一定是在当前区间的最右边。那么按照从左向右的方向,他就要最后输出了,可是在它之后还有石头的话,说明这个石头是先落下,却要最后被输出,那么就是一个入栈操作了。
    2、向右,其实和向左是一样的分析思路,松鼠的活动区间就变成了[n, n + k],左边被石头挡住,那么以后松鼠的活动范围被限制在了这个石头的右边,也就是说这块石头成为了当前区间最左边的一块,按照从左往右的顺序这个石头要最先被输出,那么其实就是先进先出了那么就将他存在一个队列里面。
    最终会得到一个栈和一个队列,我们需要先输出队列,因为队列里面存的是松鼠向右移动的操作,所以里面的石头都是位于左面的,不用担心位置,因为先落下的石头一定在最左侧,栈的也是一样的道理,所以先输出队列里的元素,然后再输出栈,就是最终答案了。
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e6 + 5;
int Stack[maxn], que[maxn];

int main()
{
    char dir;
    int top = 0, tail = 0, cnt = 1;
    while((dir = getchar()) != '\n')
    {
        if(dir == 'l')
            Stack[top++] = cnt++;
        else
            que[tail++] = cnt++;
    }
    for(int i = 0; i < tail; i++)
        printf("%d\n", que[i]);
    while(top != 0)
        printf("%d\n", Stack[--top]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/84062659
今日推荐