Codeforces Round #180 (Div. 2) AB
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 thet-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.
..RRLL...
.RRRLLLLL..
The first test sample is the one in the picture.
#include <iostream>
using namespace std;
char str[1005];
int sR=0,sL=0;
int posR,posL;
int main()
{
int begin,end;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>str
if(str
{
if(sR==0)
posR=i;
sR=1;
}
if(str
{
if(sL==0)
posL=i;
sL=1;
}
}
if(sR&&sL)
{
begin=posR+1;
end=posL;
}
else if(sR==1&&sL==0)
{
begin=posR+1;
for(int i=posR;i<n;i++)
{
if(str
{
end=i+1;
break;
}
}
}
else if(sL==1&&sR==0)
{
end=posL;
for(int i=posL;i<n;i++)
{
if(str
{
begin=i;
break;
}
}
}
cout<<begin<<" "<<end<<endl;
return 0;
}
The polar bears are going fishing. They plan to sail from (sx,sy) to (ex,ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x,y).
- If the wind blows to the east, the boat will move to (x+1,y).
- If the wind blows to the south, the boat will move to (x,y-1).
- If the wind blows to the west, the boat will move to (x-1,y).
- If the wind blows to the north, the boat will move to (x,y+1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x,y). Given the wind direction for t seconds, what is the earliest time they sail to (ex,ey)?
The first line contains five integers t,sx,sy,ex,ey (1≤t≤105,-109≤sx,sy,ex,ey≤109). The starting location and the ending location will be different.
The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
If they can reach (ex,ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
SESNW
NENSWESNEE
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination.