Escape from Stones -DFS

8 篇文章 0 订阅
7 篇文章 0 订阅

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, nstones 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
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.


题意:一个人站在一个数轴的正中心,有一块石头会从她头顶掉下,她会选择向左或者右逃跑,逃跑后被石头挡住的路不可以回去,意思是区间减少一半,而每次她总会站在区间的正中间。石头按1-n编号,当所有的石头掉下后,从左到右输出石头的编号。输入石头的块数n,以及一串由l或r组成的字符串,表示第i块石头落下的时候,她选择的逃跑方向。

  题解:如果A在第i块石头掉下的时候选择了向左逃跑(l)那么以后的石头不会再掉在这块石头的右边,即这块石头是后n-i块石头的最后一块,同理,如果A选择向右(r),那么这块石头是后n-i块石头的第一块。

所以此题有两种解法,模拟或者DFS。代码如下:

#include<stdio.h>//模拟
#include<string.h>
int main()
{
    char s[1000005];
    int a[1000005],b[1000005];
    scanf("%s",s);
    int n=strlen(s);
    int x=0,y=0;
    for(int i=1;i<=n;i++){
        if(s[i-1]=='l')a[x++]=i;
        else b[y++]=i;
    }
    for(int i=0;i<y;i++)printf("%d ",b[i]);
    for(x--;x>=0;x--)printf("%d ",a[x]);
    return 0;
}
#include<stdio.h>//dfs
#include<string.h>
char s[1000005];
int len;
int dfs(int n)
{
    if(n==len)return 0;
    if(s[n]=='l'){
        dfs(n+1);
        printf("%d\n",n+1);
    }
    else{
        printf("%d\n",n+1);
        dfs(n+1);
    }
}
int main()
{
    scanf("%s",s);
    len=strlen(s);
    dfs(0);
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值