codeforces 405B. Domino Effect



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 siis 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 < j and 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.

Sample test(s)
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.

题目大意:多米诺骨牌,L表示向左推,R表示向右推,求最后剩下站立的数量。注意若某个骨牌左边倒过来的数量=右边倒过来的数量,那么这个骨牌最后站立。
思路:先从左向右,把R右边的‘.’都推倒,再从右向左,把L左边的‘.’推倒,最后在寻找夹在中间的没有被推倒的。
按照这样的思路是超时的,只需稍微调整一下即可
ac代码
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    int a,b;
    char ch[3010];
    int flag[3010];
    cin>>n;
    cin>>ch;
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++)
    {
        if(ch[i]=='R')
        {
            flag[i]=1;
            i++;
            while(ch[i]=='.'&&i<n)
            {
                flag[i]=1;
                i++;
            }
        }
        if(ch[i]=='L')
        {
           flag[i]=1;
           int I=i;
           I--;
           while(ch[I]=='.'&&I>=0)
            {
                flag[I]=1;
                I--;
            }
        }
    }
   /* for(i=n-1;i>=0;i--)
    {
        if(ch[i]=='L')
        {
            flag[i]=1;
            i--;
            while(ch[i]=='.'&&i>=0)
            {
                flag[i]=1;
                //cout<<i<<endl;
                i--;
            }
        }
    }*/
    a=-1,b=-1;
    for(i=0;i<n;i++)
    {
        if(ch[i]=='R')
        {
            a=i;
        }
        if(ch[i]=='L')
        {
            b=i;
        }
        if(b>a&&(b-a)%2==0&&a!=-1&&b!=-1)//注意a和b的初值,自己揣摩
        {
            flag[(b+a)/2]=0;

        }
    }
    int num=0;
    for(i=0;i<n;i++)
    {
        if(flag[i]==0)
        {
        num++;
        }
    }
    cout<<num<<endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值