CodeForces - 405B G - Domino Effect

题目链接:
https://codeforces.com/problemset/problem/405/B

B. Domino Effect
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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 < 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.

Examples
14
.L.R…LR…L…

4

5
R…

0

1
.
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"就是将这块多米诺骨牌往右推倒。还有要注意的就是如果这块多米诺骨牌的两边都往中间倒的话这块多米诺骨牌算没有倒。

思路:
看懂题目后,其实是不难的,可以分几类进行讨论,首先判断有没有对这些多米诺骨牌进行操作,即字符串中有没有出现"L"或"R",如果没有的话就说明所有都没有倒,直接输出。如果有就要先判断第一个出现的是"L"还是"R",如果是"L"左边的就全倒了,如果是"R"的话,左边的就不会倒,接着就判断如果有骨牌出在"L"和"R"的中间的话全部都不会倒,但是如果出现在"R"和"L"的中间的话,就要进一步判断这些骨牌的数量是奇数个还是偶数个,如果是奇数个他们中间的那个就不会倒,如果是偶数个他们就全倒了。最后判断最后出现的是"R"还是"L",如果是"R"的话后面的就全倒,如果是"L"的话后面就不会倒。大致就是这样。

代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char s[100005];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        scanf("%s",s);
        int ww=0;
        for(int i=1;i<=n;i++)             //判断有没有出现"L"或"R"
        {
            if(s[i-1]=='R'||s[i-1]=='L')
            {
                ww=1;
                break;
            }
        }
        if(ww==0)
        {
            printf("%d\n",n);
            continue;
        }
        int flag=0;
        int sum=0;
        int k=0;
        int s1=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i-1]=='L'&&flag==0)        
            {
                s1=i;
                k=0;
                flag++;
                continue;
            }
            if(s[i-1]=='R'&&flag==0)
            {
                k=1;
                s1=i;
                sum+=(i-1);
                flag++;
                continue;
            }
            if(s[i-1]=='L'&&k==1)
            {
                k=0;
                if((i-s1)%2==0)
                    sum+=1;
                s1=i;
            }
            if(s[i-1]=='R'&&k==0)
            {
                k=1;
                sum+=(i-s1-1);
                s1=i;
            }
        }
        for(int i=n;i>=1;i--)        //判断最后出现的是“L”还是“R”
        {
            if(s[i-1]=='L')
            {
                sum+=(n-i);
                break;
            }
            if(s[i-1]=='R')
            {
                break;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44137005/article/details/88983436