ANARC05I - What is your Logo

no tags 

 

English Vietnamese

Imagine a 2D diagram drawn in the following way: Starting at the origin, you’re given a sequence of letters which is entirely made of the following four letters ’U’, ’D’, ’L’,and ’R’.A ’U’ is an instruction for you to move one unit upward and drawing a segment at the same time. Similarly, ’D’ is for moving down, ’L’ for left, and ’R’ for right.

For example, figure (a) is drawn by giving the sequence ’UURDLL’ while figure (b) is the result of ’UURRRDLLLLUURRRDDD’ (in both figures, the starting point is identified by a small circle.)


While segments are allowed to intersect, they’re not allowed to overlap. In other words, any two segments will have, at most, one point in common. We’re interested in knowing the number of closed polygons, not containing any lines inside, in such diagrams. Figure (a), has only one closed polygon while figure (b) has three. Write a program to do exactly that.

 

Input

Your program will be tested on one or more test cases. Each test case is specified on a separate line. The diagram is specified using a sequence made entirely of (U|D|L|R) and terminated by the letter ’Q’. All letters are capital letters. None of the segments in a test case will overlap.


The end of test cases is identified by the letter ’Q’ on a line by itself.

Length of each sequence is smaller than 1000.

Output

For each test case, write the answer on a separate line.

Sample

input
UURDLLQ
UURRRDLLLLUURRRDDDQ
Q

output
1
3


hide comments
iqbal_57: 2017-08-11 07:55:07

I'm getting RE. How can I take Input? I do not understand clearly

iqbal_57: 2017-08-11 07:55:03

Last edit: 2017-08-11 07:57:20
hamjosh1: 2016-09-17 15:28:25

Loved it :D

sam_29: 2016-04-16 22:11:10

Test Cases are very weak no reason to solve the question!!!!!!
For the test case given below the ans should be 3 but the code with output 4 is also accepted
UUURRRDDDLLLLLUUUUURRRRDDDDLLLUUURRDDQ
Q

Last edit: 2016-04-16 22:15:34
monkz: 2015-10-04 11:32:29

very weak test cases....

arunpatala: 2015-07-25 22:47:31

LINK: http://www.spoj.com/problems/ANARC05I/en/

anuveshkothari: 2015-07-24 17:49:16

English version of the problem says:- NOT FOUND ON THE SERVER...
please check

HITESH GARG: 2015-01-04 21:38:43

if u catch the logic then its only programming :)

Archit Jain: 2014-08-28 20:57:23

easy but enjoyed solving it

shashank: 2014-05-24 04:03:29

Awesome problem ...


Added by:~!(*(@*!@^&
Date:2009-07-05
Time limit:0.25s-0.790s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ANARC 2005