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

2782 단어 venturecup20168VC
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
",cnt); } return 0; }

좋은 웹페이지 즐겨찾기