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;
    }
  • 相关阅读:
    Cookies
    一个完整的upstart脚本分析
    squid总结
    python递归读取目录列表
    python删除文件
    ubuntu切割mp3文件
    TP-LINK TL-WN725N V2 / rtl8188eu Linux驱动安装
    ubuntu启动脚本
    su对环境变量做了什么
    sudoers文件配置
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5188832.html
Copyright © 2011-2022 走看看