zoukankan      html  css  js  c++  java
  • 8VC Venture Cup 2016

    A. Robot Sequence

    题目连接:

    http://www.codeforces.com/contest/626/problem/A

    Description

    Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n 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.

    Sample Input

    6
    URLLDR

    Sample Output

    2

    Hint

    题意

    给你一个命令,然后让你输出这个命令中有多少个子串的起点和终点是一样的

    题解:

    数据范围只有200,所以直接n^2暴力就好了

    记录一下前缀和。

    其实不记录前缀和 n^3也是兹瓷的

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    const int maxn = 503;
    char s[maxn];
    int x[maxn],y[maxn];
    
    int main()
    {
        int n;
        scanf("%d",&n);
        scanf("%s",s+1);
        for(int i=1;i<=n;i++)
        {
            x[i]=x[i-1];
            y[i]=y[i-1];
            if(s[i]=='U')x[i]++;
            if(s[i]=='D')x[i]--;
            if(s[i]=='L')y[i]++;
            if(s[i]=='R')y[i]--;
        }
        int ans = 0;
        for(int i=0;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                if(x[i]-x[j]==0&&y[i]-y[j]==0)
                    ans++;
        cout<<ans<<endl;
    }
  • 相关阅读:
    组合数学+结论——cf1359E
    【模拟】分类讨论大模拟+数论——cf1358F
    【思维】前缀和——cf1358E
    Android基础—— 自定义RecyclerView内部控件的监听器
    【思维】模拟+暴力——icpc nwrrc 2019 K
    分治模板
    树上问题&图论模板整理
    数据结构模板整理
    一些计数题
    Codeforces Round #567 (Div. 2)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5188832.html
Copyright © 2011-2022 走看看