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.
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 n lines — on the i-th line you should print the i-th stone's number from the left.
llrlr
35421
rrlll
12543
lrlrr
24531
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;}
- Escape from Stones -DFS
- CodeForces 264A Escape from Stones dfs
- [CodeForces264A]Escape from Stones[dfs][two pointers]
- DFS、栈、双向队列:CF264A- Escape from Stones
- A. Escape from Stones
- codeforces-264A-Escape from Stones
- Codeforces 265 Escape from Stones 思维(极限)
- codeforces 265C - Escape from Stones 思维问题
- SPOJ_AMR10I_Dividing Stones(DFS)
- SPOJ AMR10I Dividing Stones --DFS
- hdu1813 Escape from Tetris
- UVALive 5116 Dividing Stones(dfs + 打表)
- 【Stones from other hills】跨公网监控
- 【Stones from other hills】水煮TCPMP
- 【Stones from other hills】视频解码优化
- 【Stones from other hills】RTP各种协议
- 【Stones from other hills】 编程习惯
- 【Stones from other hills】如何成为项目经理
- 条件判断4个代码片段
- vim的查找字符串
- 摄像头采集程序linux下
- 解析c++中的引用和const引用,以及了解指针和引用的区别
- 安卓动态分析工具 Inspeckage
- Escape from Stones -DFS
- Service基本入门及AIDL跨进程通讯
- tensorflow(1) mnist_softmax.py
- 栈与队列的原理与实现
- PYTHON 开发 正则表达式
- Spring-boot整合Quartz,2、Bean配置
- Android ORM 框架之 greenDAO
- MyBatis基础
- LoRa简介