CodeForces 626 A. Robot Sequence(水~)

来源:互联网 发布:c语言产生0 9的随机数 编辑:程序博客网 时间:2024/05/01 13:17

Description
给出一个操作串,UDLR分别表示机器人向上下左右走一步,问这个串有多少非空子串使得操作完这个子串后机器人回到原地
Input
第一行为一个整数n表示操作串长度,之后为一长度为n的字符串(1<=n<=200)
Output
输出满足条件的子串个数
Sample Input
6
URLLDR
Sample Output
2
Solution
水题,枚举子串起点终点即可
Code

#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define maxn 222int n;char s[maxn];int main(){    while(~scanf("%d",&n))    {        scanf("%s",s);        int ans=0;        for(int i=0;i<n;i++)            for(int j=i+1;j<n;j++)            {                int res1=0,res2=0;                for(int k=i;k<=j;k++)                    if(s[k]=='U')res1++;                    else if(s[k]=='D')res1--;                    else if(s[k]=='L')res2++;                    else if(s[k]=='R')res2--;                if(!res1&&!res2)ans++;            }        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击