题目链接: 传送门
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;
}