DFS、栈、双向队列:CF264A- Escape from Stones
来源:互联网 发布:动态图修改软件 编辑:程序博客网 时间:2024/06/08 14:58
题目:
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]); }}
- DFS、栈、双向队列:CF264A- Escape from Stones
- Escape from Stones -DFS
- CodeForces 264A Escape from Stones dfs
- [CodeForces264A]Escape from Stones[dfs][two pointers]
- A. Escape from Stones
- codeforces-264A-Escape from Stones
- Codeforces 265 Escape from Stones 思维(极限)
- codeforces 265C - Escape from Stones 思维问题
- CF264A
- SPOJ_AMR10I_Dividing Stones(DFS)
- hdu 1896 stones 队列
- Stones(优先队列)
- Hdu1896 Stones(优先队列)
- Stones(优先队列)
- SPOJ AMR10I Dividing Stones --DFS
- HDU 1896 Stones(优先队列)
- Stones(hdu1896)优先队列
- 1896 Stones(优先队列)
- 腾讯笔试题--微信红包
- 递归删除一个文件夹
- JNI开发流程
- Spring 源码之 BeanDefinition阅读
- SpringMvc的DispatcherServlet和Control的线程安全问题
- DFS、栈、双向队列:CF264A- Escape from Stones
- NGUI 类皇室战争(CR)的滚动列表效果完整实现(四)
- hdu 2962 Trucking(二分+最短路)
- POJ 1020 Anniversary Cake(dfs+分割思想)【转】
- 有意思的程序员
- hdu 1254 推箱子 (bfs+dfs+预处理)
- java计算器,可以运算括号
- 【机器学习】支持向量机
- iOS mac终端下的SQL语句