Escape from Stones(CodeForces - 264A)(思维)

A. 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.

题目传送

首先让我们来分析一下题意,在区间[0,1]内总在小民(昵称)所站位置的中间处。注意S的长度1e6如果直接作数(求结果)的话很容易就爆long double 而解决方案就是直接给他进行排序。怎么做呢,两种方法分别想象一下:

1.DFS

比较暴躁的一种方法,分为两种情况即遇到'l'还是遇到‘r'

1.遇到r 则可以想象一下接下来的每一个位置都会在他的右边,所以都是比他大的从小到大输出所以可以直接将他输出,再去dfs找值。

2.遇到l 则可以想象一下  接下来的位置都是在他的左边而我们无法确定从小到大排最小的是哪一个。因而先dfs接着去找再输出

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 1e6+5;
char ch[maxn];
int len;
int dfs(int t)
{
    if(t==len)
    {
        return 0;
    }
    if(ch[t] == 'l')//l先dfs找最小在输出
    {
        dfs(t+1);
        printf("%d\n",t+1);
    }
    else  // r的情况一定是最小了 直接输出再继续找
    {
        printf("%d\n",t+1);
        dfs(t+1);
    }
}
int main()
{
    while(scanf("%s",ch)!=EOF)
    {
        len = strlen(ch);
        dfs(0);
    }
    return 0;
}

2.思维

我们可以想象一下l 和 r的两种情况:

1.l 时一定是当前最大得了存入n递减的数组

2.r时 一定是当前最小的存入k递增的数组。

#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e6+5;
char ch[maxn];
int a[maxn];
int main()
{
    while(scanf("%s",ch)!=EOF)
    {
        int len = strlen(ch);
        int k = 0,n = len-1;
        for(int i = 0;i<len;i++)
        {
            if(ch[i] == 'l')//当前最大值
            {
                a[n--] = i+1;
            }
            else if(ch[i] == 'r')//当前最小值
            {
                a[k++] = i+1;
            }
        }
        for(int i = 0;i<len;i++)
        {
            printf("%d\n",a[i]);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码中有几个问题: 1. while 循环的判断条件应该是 `stones.length > 1`,因为只有至少有两个石头时才需要进行破碎的操作。所以应该将 `while( stones.length==1 || stones.length==0 )` 修改为 `while( stones.length > 1 )`。 2. 在交换石头的位置时,应该使用临时变量来保存一个石头的值,然后再进行交换。所以应该将 `a=stones[j];` 修改为 `int temp = stones[i];`,将 `stones[i]=stones[j];` 修改为 `stones[i]=stones[j];`,将 `stones[j]=a;` 修改为 `stones[j]=temp;`。 3. 在判断最后剩下的石头重量时,应该使用索引 `stones.length-1` 和 `stones.length-2` 来获取最后两个石头的重量。所以应该将 `if(stones[stones.length-1]<=stones[stones.length])` 修改为 `if(stones[stones.length-1]<=stones[stones.length-2])`,将 `stones.length=stones.length-2;` 修改为 `stones.length -= 2;`。 4. 最后返回的应该是 `stones[0]`,而不是 `stones[stones.length]`。所以应该将 `return stones[stones.length];` 修改为 `return stones[0];`。 修正后的代码如下: ```java class Solution { public int lastStoneWeight(int[] stones) { while (stones.length > 1) { for (int i = 0; i < stones.length; i++) { for (int j = 1; j < stones.length; j++) { if (stones[i] > stones[j]) { int temp = stones[i]; stones[i] = stones[j]; stones[j] = temp; } } } if (stones[stones.length - 1] <= stones[stones.length - 2]) { stones[stones.length - 2] -= stones[stones.length - 1]; stones = Arrays.copyOf(stones, stones.length - 1); } else { stones = Arrays.copyOf(stones, stones.length - 2); } } return stones[0]; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值