Codeforces Problem 405B. Domino Effect

B. Domino Effect
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 playwith 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 < 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.

题目意思:多米诺影响,提供一个字符串表示多米诺的状态,每一个字符带面一个多米诺牌。其中的R L代表当前的那块多米诺牌的运动方向。题目的输入保重不出现两个连续的方向。

思路1:分段考虑(前端,中间,和后面)

先处理掉前面的 '.',让当前指针指向第一个字母字符,判断:如果是 ’R' 就记录下 ‘.' 的数目;‘L’就不管。

再处理后面的‘.', 让后一个指针指向最后一个字母字符,判断:如果是 ’L‘ 就记录下'.'的数目;’R'就不管。

中间的有点复杂:没一个‘R’都会对应一个‘L’,而夹在这两个字母中间的‘.'会受到影响,如果数目为奇数,最后竖着的会剩下一块,否着就都会倒。而在’L' 和 'R'中间的 '.' 字符将不会受到影响(容易忽略地方),'.'的个数需要加到总数里面去。

实现:

 

/*
* @author:ckeling
* @codeforces Problem 405B. domino Effect
* @description:implemacation
***/

#include <stdio.h>

int main(){
    int n;
    char *domino[3000];
    scanf("%d", &n);
    scanf("%s", domino);
    char *pd, *qd;
    int dcount;
    int dd = 0;    // 竖着的domino数量
    //想法1:前端、中间、后端
    pd = domino;
    qd = pd + n - 1;

    //前端
    dcount = 0;
    while(*pd == '.'){
        dcount++;
        pd++;       // pd 指向第一个字母字符
    }
    if(*pd == 'R' || *pd == '\0')
        dd += dcount;


    // 后端
    dcount = 0;
    //printf("pd = %c\n", *qd);
    while(*qd == '.'){
        dcount++;
        qd--;       // qd 指向最后一个字母字符
    }
    if(*qd == 'L')
        dd += dcount;

    // 中间
    dcount = 0;
    while(pd < qd){
        if(*pd == 'L'){    // L 与 R 中间的点全部要加上
           while(pd < qd && *++pd == '.')
                ++dd;
        }
        else if(*pd == 'R'){
            while(pd < qd && *++pd == '.')
                ++dcount;
            if(dcount % 2)
                ++dd;
            dcount = 0;
        }
    }

    printf("%d\n", dd);
    return 0;
}

思路2:模拟,通过标志量(不太成熟)

刚开始,Flag=FLASE。当出现一个字母后就对Flag取非。在扫描字符串的过程中记录 '.' 字符的个数,当出现非 '.' 字符的时候判断 Flag 的值 和 当前字符的值。如果为TRUE,则说明是奇数个字母字符,判断当前字符的值,如果是‘R' ,则需要记录下 '.' 的个数, 如果是 'L' 就不用管。如果Flag是FALSE 的话,怎么说明当前的字母字符与前一个字母字符已经配对,则需要判断 中间 '.' 的个数的奇偶性,为奇数就对总个数加 1,否则不就管(加0)。最后需要处理结束的点。怎么处理呢?如果按前面所说的走,就需要每次都记录下上一次的字母,通过上一次的字母来确定最后的 '.' 字符要不要加。而一种做法是将后面的单独处理,就和上面的分段是一样的。

另一种模拟是单独模拟,就是在遇到字符的时候进行判断,如果当前字符是'R',则总数需要加上当前'.'的个数,并将点的个数清零,如果是‘L' 就直接将点的个数清零。将最后的点单独拿出来处理。不然在判断到字符串结尾 '\0' 后就又要回头去判断,或者用一个字符变量记录前一个字母字符的值。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值