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 < 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
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"。
输出
输出一个整数,即在进程结束时保持垂直的多米诺骨牌的数量。
例子
输入
14
.L.R…LR . . L . .
输出
4
输入
5
R ....
输出
0
输入
1
.
输出
1
请注意
图中显示了第一个例子。四个垂直放置的部分用橙色突出显示。
在第二个示例中,由于第一个块将所有其他块压到一起,所以所有块都将掉下来。
在上一个例子中,没有向任何一个方向推一块。
思路:
分类讨论,首先判断有没有对这些多米诺骨牌进行操作,即字符串中有没有出现"L"或"R",如果没有的话就说明所有都没有倒,直接输出。如果有就要先判断第一个出现的是"L"还是"R",如果是"L"左边的就全倒了,如果是"R"的话,左边的就不会倒,接着就判断如果有骨牌出在"L"和"R"的中间的话全部都不会倒,但是如果出现在"R"和"L"的中间的话,就要进一步判断这些骨牌的数量是奇数个还是偶数个,如果是奇数个他们中间的那个就不会倒,如果是偶数个他们就全倒了。最后判断最后出现的是"R"还是"L",如果是"R"的话后面的就全倒,如果是"L"的话后面就不会倒。大致就是这样。
AC代码:
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <string.h>
#define N 3010
using namespace std;
char a[N];
int main()
{
int n;
while(~scanf("%d%s",&n,a))
{
int cnt=0,ans=0,k=0;
for(int i=0; i<n; i++)
{
if(a[i]=='.')
cnt++;
else if(a[i]=='L')
{
ans=ans+cnt+1;
if(k==2 && cnt%2==0)
ans++;
cnt=0;
k=1;
}
else if(a[i]=='R')
{
cnt=0;
k=2;
}
}
if(k==2)
ans=ans+cnt+1;
printf("%d\n",n-ans);
}
return 0;
}