CF-298A(模拟+找规律)
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.

At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
9
..RRLL...
3 4
11
.RRRLLLLL..
7 5
The first test sample is the one in the picture.
题意:有个数组,从第i格走到第i+1格会在第i格留下R的脚印。从第i+1格走到第i格会在第i+1格留下L的脚印。给你总共的格数和每格上的脚印情况。问你从出发点s和结束点t是第几格。输出任一满足条件的s,t。格子从1开始算。
思路:找规律。一共有3种情况。只有L的脚印,只有R的脚印,和LR都有的脚印,而且所有的R脚印一定在所有的L脚印的左边,即RRRRRRLLLLLLLL。
扫一遍数组就行了。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int n; 7 char s[1010]; 8 9 int main() 10 { 11 while(~scanf("%d", &n)){ 12 scanf("%s", s); 13 int start, end;//第一个R字符的位置,第一个L字符的位置 14 bool findstart = false, findend = false;//找到了出发位置,结束位置 15 int zuo;//找到第一个不是.的字符位置 16 int you;//找到最后一个不是.的字符的位置 17 bool flag1 = false; 18 19 for (int i = 0; i < strlen(s); i++){ 20 if(s[i] != '.' && !flag1){ 21 zuo = i; 22 flag1 = true; 23 } 24 if(s[i] != '.') 25 you = i; 26 if(s[i] == 'R' && !findstart){ 27 start = i+1; 28 findstart = true; 29 } 30 if(s[i] == 'L' && !findend){ 31 end = i; 32 findend = true; 33 } 34 } 35 36 if(findstart && findend){//第一种情况既有R又有L型(所有的R一定在所有的L左边) 37 printf("%d %d\n", start, end); 38 } 39 else if(!findstart){//第二种情况只有L型 40 printf("%d %d\n", you + 1, zuo); 41 } 42 else if(!findend){//第三种情况只有R型 43 printf("%d %d\n", zuo + 1, you+2); 44 } 45 } 46 return 0; 47 }

浙公网安备 33010602011771号