DFS、栈、双向队列:CF264A- Escape from Stones
题目:
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);
}
}
#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]);
}
}
最新文章
- B样条曲线曲面(附代码)
- Chrome console命令整理
- springmvc中的controller是单例的
- struts2重点——ValueStack和OGNL
- 一种透明效果的view
- Bestcoder Round# 80
- 6-Highcharts曲线图之带标识
- 车牌识别LPR(八)-- 字符识别
- 浅谈C中的指针和数组(三)
- zend studio修改字体
- 导入import com.sun.image.codec.jpeg.JPEGCodec出错
- 热门开源项目:Guns-后台管理系统
- 无忧代理免费ip爬取(端口js加密)
- JQuery跳出each循环的方法
- HOG特征(Histogram of Gradient)学习总结
- 16-(基础入门篇)GPRS(Air202)关于多个文件中的变量调用和定时器
- sql语句查询排序
- U3D学习005——输入操作
- Greys--JVM异常诊断工具
- Code Signal_练习题_Circle of Numbers
热门文章
- mysql忘记数据库密码
- 常用模块random,time,os,sys,序列化模块
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
- Swagger的使用
- 【MFC】可以换行的编辑框
- uvm_svcmd_dpi——DPI在UVM中的实现(二)
- amap -bq 192.168.5.9 80 3306
- linux中BASH_SOURCE[0](转)
- ansible 2.1.0 api 编程
- python 函数学习之sys.argv[1]