8VC Venture Cup 2016 - Elimination Round A. Robot Sequence

A. Robot Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list ofn commands, each either 'U', 'R', 'D', or 'L' — instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices.

Input

The first line of the input contains a single positive integer, n (1 ≤ n ≤ 200) — the number of commands.

The next line contains n characters, each either 'U', 'R', 'D', or 'L' — Calvin's source code.

Output

Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square.

Examples
Input
6
URLLDR
Output
2
Input
4
DLUU
Output
0
Input
7
RLRLRLR
Output
12
Note

In the first case, the entire source code works, as well as the "RL" substring in the second and third characters.

Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result.



题意:给你一个字符串,里面有UDLR,分别表示四个方向,问这个串的连续子串有多少是能使其回到起点的。。


思路:直接暴力枚举就好了


ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#define MAXN 1010000
#define LL long long
#define ll __int64
#include<iostream>
#include<algorithm>
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int main()
{
	int len;
	char s[222];
	while(scanf("%d",&len)!=EOF)
	{
		scanf("%s",s);
		int cnt=0;
		for(int i=0;i<len;i++)
		{
			for(int j=i+1;j<len;j++)
			{
				int x=1,y=1;
				for(int k=i;k<=j;k++)
				{
					if(s[k]=='U')
					y++;
					else if(s[k]=='D')
					y--;
					else if(s[k]=='L')
					x--;
					else
					x++;
				}
				if(x==1&&y==1)
				cnt++;
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}


你可能感兴趣的:(venture,cup,2016,8VC)