G - Domino Effect CodeForces - 405B

Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show".

Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.

After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.

Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!

Input

The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to

  • "L", if the i-th domino has been pushed to the left;
  • "R", if the i-th domino has been pushed to the right;
  • ".", if the i-th domino has not been pushed.

It is guaranteed that if si = sj = "L" and i < j, then there exists such k that i < k < jand sk = "R"; if si = sj = "R" and i < j, then there exists such k that i < k < j and sk = "L".

Output

Output a single integer, the number of the dominoes that remain vertical at the end of the process.

Examples

Input

14
.L.R...LR..L..

Output

4

Input

5
R....

Output

0

Input

1
.

Output

1

Note

The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.

In the second example case, all pieces fall down since the first piece topples all the other pieces.

In the last example case, a single piece has not been pushed in either direction.

题意:小克里斯知道玩多米诺骨牌没有乐趣,他认为它太随意,不需要技巧。相反,他决定玩多米诺骨牌,做一个“多米诺秀”。克里斯把n张多米诺骨牌排成一行,把每块骨牌竖直摆放。一开始,他同时把一些多米诺骨牌推到左边或右边。然而,在每两个向同一方向推的多米诺骨牌之间的某个地方,至少有一个向相反方向推的多米诺骨牌。每过一秒钟,每个落在左边的domino都会将相邻的domino推到左边。同样地,向右倒下的多米诺骨牌会推动站在右边的相邻的多米诺骨牌。当一张垂直的多米诺骨牌有多米诺骨牌从两边掉下来时,由于力的平衡,它会保持静止。图中显示了该流程的一个可能示例。给定Chris推动多米诺骨牌的初始方向,找出在这个过程结束时垂直放置的多米诺骨牌的数量!输入第一行包含一个整数n(1≤n≤3000),即一行中多米诺骨牌的个数。下一行包含长度为n的字符串s。字符串si的第i个字符等于“L”,如果第i个domino被推到左边;“R”,如果第i个domino被推到右边;“。”,如果没有推第i个domino。保证如果si = sj = "L"且i < j,则存在i < k < j且sk = "R"的k;如果si = sj = "R", i < j,则存在这样的k, i < k < j, sk = "L"。输出输出一个整数,即在进程结束时保持垂直的多米诺骨牌的数量。

思路:常规判断

从左向右判断,

1 如果是.就sum++

2 如果遇到了L就判断:(1) 前面出现过R,那么中立的数量就是这之间.的数量%2,(2)如果没有R,那么前面连续的.牌全部倒下;

3 如果遇到了R,那么连续的.都可以幸存,并且标记出现了R,方便L进行判断.

就是这样

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
   char a[3100];
   int n,sum=0,ans=0,flag=0;
   scanf("%d",&n);
   scanf("%s",a);
   for(int i=0;i<n;i++)
   {
       if(a[i]=='.')
        sum++;
       if(a[i]=='L')
       {
           if(flag==1)
           {
               ans+=sum%2;
               sum=0; //每进行一段就对.的数量重新计数
               flag=0;
           }
           else
               sum=0;  //每进行一段就对.的数量重新计数
       }
       if(a[i]=='R')
       {
           ans+=sum;
           sum=0; //每进行一段就对.的数量重新计数
           flag=1;
       }
   }
   if(flag==0)
    ans+=sum;
   printf("%d",ans);

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值