【ACM_1】H - Line Gimmick

  题目链接:https://vjudge.net/contest/148585#problem/H

Line Gimmick

You are in front of a linear gimmick of a game. It consists of $N$ panels in a row, and each of them displays a right or a left arrow.

You can step in this gimmick from any panel. Once you get on a panel, you are forced to move following the direction of the arrow shown on the panel and the panel will be removed immediately. You keep moving in the same direction until you get on another panel, and if you reach a panel, you turn in (or keep) the direction of the arrow on the panel. The panels you passed are also removed. You repeat this procedure, and when there is no panel in your current direction, you get out of the gimmick.

For example, when the gimmick is the following image and you rst get on the 2nd panel from the left, your moves are as follows.

  • Move right and remove the 2nd panel.
  • Move left and remove the 3rd panel.
  • Move right and remove the 1st panel.
  • Move right and remove the 4th panel.
  • Move left and remove the 5th panel.
  • Get out of the gimmick.
 

You are given a gimmick with $N$ panels. Compute the maximum number of removed panels after you get out of the gimmick.

Input

The input consists of two lines. The first line contains an integer $N$ ($1 \leq N \leq 100,000$) which represents the number of the panels in the gimmick. The second line contains a character string $S$ of length $N$, which consists of '>' or '<'. The $i$-th character of $S$ corresponds to the direction of the arrow on the $i$-th panel from the left. '<' and '>' denote left and right directions respectively.

Output

Output the maximum number of removed panels after you get out of the gimmick.

Sample Input 1

7
>><><<<

Output for the Sample Input 1

7

Sample Input 2

5
>><<<

Output for the Sample Input 2

5

Sample Input 3

6
><<><<

Output for the Sample Input 3

6

Sample Input 4

7
<<><<>>

Output for the Sample Input 4

5

大概题意:一串由“>”和“<”组成的字符串,“>”表示向右走一步,“<”表示向左走一步,从任意一个位置开始走,每次按照当前位置上的命令走一步,再删去这个字符,问最多能走多少。

分析:非常精妙的一道题目,我们考虑任意处开始走,如下一个命令一致,则继续走,否则掉头,那么其实连续一段相同的命令(如“>>>>>”或“<<<”等)并不影响最终能否走完,问题就简化为一串由“>”和“<”间隔排列的字符串能否被走完,那么根据首尾字符,可以分为三种情况:1、<><>……<><>;2、<><>……<><(反过来的情况也一样);3、><><><><;

第一种情况肯定不可行,因为无法从边界往回走,所以只能最后到达边界,而且又有两个边界,就不可行了。

第二种情况可以把字符串看成一段第一种情况下的串+一个单独的朝里的箭头,从第一段的中间开始走,经过一左一右消,到达有朝里箭头的一端,再掉头走到另一个边界

第三种情况可以看成第二种情况在另一端添上一个朝里的箭头,因为第二种情况成立,且走到最后一个边界时不用掉头,所以可以走到最后,即改情况成立

所以只要正反各走一趟,找到离边界最近的一个朝里箭头即可

贴代码,短的奇诡……

#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int main(){
	int i,L=0,R;
	char ch;
	for (scanf("%d",&n),ch=getchar();ch!='<' && ch!='>';ch=getchar());
	for (i=1;i<=n;ch=getchar(),++i){
	  	if (ch=='<') R=i;
	  	if (ch=='>' && L==0) L=i;
	  }
	printf("%d\n",max(n-L+1,R));
	return 0;
}

【写的有漏洞的,欢迎路过大神吐槽】

  2017/1/22 22:30:07

  Ending.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值