Escape from Stones -DFS

来源:互联网 发布:软件测试中的单元测试 编辑:程序博客网 时间:2024/06/10 11:14

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
35421
Input
rrlll
Output
12543
Input
lrlrr
Output
24531
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;}




原创粉丝点击