Playing Dominoes
思路:
1.找到第一個非’.‘的位置 若爲n則全爲所求
2.第一個非’.‘如果是R 則...若爲L 則...
3.記錄每一個非’.‘的位置
4.遍歷所有R或L至倒數第二個,只考慮當前R或L與下一個L或R的位移,
5.考慮最後一個R或L後面的’.‘
Time Limit:1000MS Memory Limit:262144KB 64bit
IO Format:%I64d & %I64u
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..
4
5 R....
0
1 .
1
Hint
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.
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[3030];
int lr[3030];
int main()
{
int n,place,i,j;
char first;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
int llrr=0,count=0;
for(i=0;i<n;i++)
{
if(s[i]!='.') break;
}
if(i==n) { count=n;}
else {
first=s[i];
place=i;
if(first=='R') count+=place;
for(i=place;i<n;i++)
{
if(s[i]=='.') continue;
if(s[i]!='.') {lr[llrr]=i;llrr++;}
}
for(i=0;i<llrr-1;i++)
{
if(s[lr[i]]=='R') count+=((lr[i+1]-lr[i])%2==1)?0:1;
if(s[lr[i]]=='L') count+=lr[i+1]-lr[i]-1;
}
if(s[lr[llrr-1]]=='L') count+=(n-1-lr[llrr-1]);
}
printf("%d\n",count);
}
return 0;
}