WRONG - Wrong directions

no tags 

Farmer John has just purchased a fancy new programmable tractor. To make the tractor move, he types in a string of length N (1 <= N <= 100,000) consisting of only the characters F, L, and R. Each 'F' instructs the tractor to move forward one unit, and the characters 'L' and 'R' result in left and right turns of 90 degrees, respectively. The tractor starts out at the origin (0,0) facing north.

After programming his tractor by typing in his intended command string, FJ remembers that he typed exactly one character in the command string incorrectly, but he can't remember which one! For example, he might have typed 'F' or 'L' when his intended string contained the character 'R'. Please compute the number of different locations in the plane at which the tractor might end up as a result (the direction the tractor faces in its final location does not matter).

Input:

* Line 1: Farmer John's intended command string.

Output

* Line 1: The number of positions at which the tractor might end up, given that FJ mistypes one of the characters in his command string.

Example

INPUT:
FF

OUTPUT:
3

Explanation

Farmer John wants the tractor to advance forward twice, ideally ending at position (0,2). There are 4 possible mistyped sequences: FL, FR, LF, an RF. These will land the tractor at (0,1), (0,1), (-1,0), and (1,0) respectively - a total of 3 distinct locations.


hide comments
nadstratosfer: 2019-12-27 06:22:14

Where did the 3 oldest comments come from? ;)

Interesting, original problem but despite having a clear idea, the way I ended up solving it was tedious and frustrating as hell. Having to translate to C because of useless TL and struggling with stupid C issues for an extra half an hour did not cheer me up. Just relieved that I'm done here.

Edit: Doh, PyPy does pass after all.

Last edit: 2019-12-27 06:27:39
(Tjandra Satria Gunawan)(曾毅昆): 2012-07-30 11:17:56

My algorithm complexity is O(n*log(n)) where n=(length of command string)*2, got AC 1.31s (slow).
Any faster way to solve this problem?

Last edit: 2012-09-28 14:06:52
Darko Aleksic: 2012-07-03 16:54:43

I guess Java time limit problems were transferred as well (which is the reason I stopped doing USACO long time ago)
I do not see a reason to keep it so tight because O(n^2) would fail with much higher time limit.

:D: 2012-06-14 19:11:55

I don't know what are you all talking about. It's a nice problem, but it only involves some basic algorithmic approaches. Definitively not trivial, though.
RE: the only reason I added this problem, was because my friends asked me to, it was sort of a little practice for them. But yes, it's a nice problem.

Last edit: 2012-06-15 07:05:34
Edelweiss: 2012-06-13 03:14:03

Just need some preprocessing. Enjoy the game. ^_^

Thomas: 2012-06-12 07:16:32

Does someone knows the formula??
RE: what formula???

Last edit: 2012-06-12 07:18:15
Santhana Krishnan: 2012-06-12 06:50:06

A problem involving careful case analysis, but for some reason was very satisfying to solve.


Added by:Ikhaduri
Date:2012-06-09
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Usaco march 2012