题目链接: 传送门

Domino Effect

time limit per test:1 second     memory limit per test:256 megabytes

Description

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.

Sample Input

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

5
R....

1
.

Sample Output

4

0

1

解题思路:

题目保证测试数据(看input加重字眼)保证L 与 L 之间一定有 R,R 与 R 之间一定有L,所以有了这些保证就很好判断了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int N;
    char str[3005];
    int ans[3005];
    bool flag = true,IsL = false;
    memset(str,0,sizeof(str));
    memset(ans,0,sizeof(ans));
    scanf("%d",&N);
    scanf("%s",str);
    for (int i = 0; i < N; i++)
    {
        if (str[i] == 'L' || str[i] == 'R')
        {
            flag = false;
            if (str[i] == 'L')
            {
                IsL = true;
                break;
            }
            else
            {
                if (str[i] == 'R')
                {
                    IsL = false;
                    break;
                }
            }
        }
    }
    int j = 0,sum = 0,len = 0;
    for (int i = 0; i < N; i++)
    {
        if (str[i] == '.')
        {
            continue;
        }
        else
        {
            ans[j++] = i;
        }
    }
    if (flag)
    {
        printf("%d\n",N);
    }
    else
    {
        len = j;
        sum = 0;
        if (IsL)
        {
            for (int i = 0; i < len - 1; i++)
            {
                if (i%2==0)
                {
                    sum += (ans[i+1]-ans[i]-1);
                }
                else if (i&1)
                {
                    if ((ans[i+1]-ans[i])%2 == 0)
                    {
                        sum++;
                    }
                }
            }
            if (str[ans[len-1]] == 'L')
            {
                sum += (N-ans[len-1]-1);
            }
        }
        else
        {
            sum += ans[0];
            for (int i = 0; i < len - 1; i++)
            {
                if (i&1)
                {
                    sum += (ans[i+1] - ans[i] - 1);
                }
                else if (i % 2 == 0)
                {
                    if ((ans[i+1]-ans[i])%2 == 0)
                    {
                        sum++;
                    }
                }
            }
            if (str[ans[len-1]] == 'L')
            {
                sum += (N-ans[len-1]-1);
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

最新文章

  1. 前端工程师技能之photoshop巧用系列第三篇——切图篇
  2. CodeFirst进行数据迁移之添加字段
  3. Android经典完美退出方法
  4. XPM转换与查看工具
  5. Cocos2d-X3.0 刨根问底(八)----- 场景(Scene)、层(Layer)相关源码分析
  6. IDEA中 @override报错的处理步骤
  7. Content-type 的说明
  8. hdu 2052 Picture(java)
  9. ubuntu 折腾之路
  10. Count Color
  11. DSOframer 微软官方API的查阅方法
  12. 细节!重点!易错点!--面试java基础篇(一)
  13. YII 实现布局
  14. Android查看stdout 和stderr
  15. MySQL 删除数据库中反复数据(以部分数据为准)
  16. web.config中configSections section节 -Z
  17. 2013 QCon北京演讲:跨终端的WebKit渲染机制
  18. 2018-2019-2 网络对抗技术 20165328 Exp2 后门原理与实践
  19. 【温故而知新】HTTP 报文
  20. atcoder NIKKEI Programming Contest 2019 E - Weights on Vertices and Edges

热门文章

  1. FineUI大版本升级,外置ExtJS库、去AXD化、表格合计行、表格可编辑单元格的增删改、顶部菜单框架
  2. EF增删改查操作
  3. C# WinForm应用程序降低系统内存占用方法
  4. android中的图片处理
  5. 对C语言中指针的一些新认识
  6. 【BZOJ 4456】【UOJ #184】【ZJOI 2016】旅行者
  7. 写chrome插件---一个优酷自动加粉丝助手
  8. Echarts-画堆积柱状图,折线图
  9. Hibernate @OneToMany 一对多注解
  10. TODO: 图片加载框架ImageLoader的实现