• Codeforces 321A


    x[i] 表示:从第一步到第i步为止,横坐标的位置;

    y[i]表示:从第一步到第i步为止, 纵坐标的位置;

    设 字符串S的长度为 len;

    则有 k * x[len] + x[i] = a; 而且 k * y[len] + y[i] = b; (这里 下标从1开始),而且要注意的是 k 为非负整数, 还要考虑 x[len] 或 y[len]为0的情况。然后从1到len枚举 i 就可以了。


    附上代码:

     1 # by Stomach_ache
     2 def move(s, i):
     3         if s[i] == 'U':
     4             return 0, 1;
     5         elif s[i] == 'D':
     6             return 0, -1;
     7         elif s[i] == 'L':
     8             return -1, 0;
     9         else:
    10             return 1, 0;
    11 a, b = map(long, raw_input().split());
    12 s = raw_input();
    13 l = len(s);
    14 x, y = [0]*(l+1), [0]*(l+1)
    15 f = 0;
    16 for i in xrange(1, l+1):
    17     x[i], y[i] = x[i-1], y[i-1];
    18     tmpx, tmpy = move(s, i-1)
    19     x[i] += tmpx;
    20     y[i] += tmpy;
    21 if a == 0 and b == 0:
    22     print "Yes";
    23 else:
    24     # x[l] and y[l] can both be zero, so we can not divide directly
    25     # k must be a nonegative interger
    26     for i in xrange(1, l+1):
    27         if x[l] == 0 and y[l] != 0:
    28             if a == x[i] and (b - y[i]) % y[l] == 0 and (b - y[i]) / y[l] >= 0:
    29                 f = 1
    30                 break;
    31         elif y[l] == 0 and x[l] != 0:
    32             if b == y[i] and (a - x[i]) % x[l] == 0 and (a - x[i]) / x[l] >= 0:
    33                 f = 1
    34                 break;    
    35         elif x[l] == 0 and y[l] == 0:
    36             if a == x[i] and b == y[i]:
    37                 f = 1;
    38                 break;
    39         elif (a - x[i]) % x[l] == 0 and (b - y[i]) % y[l] == 0:
    40             if (a - x[i]) / x[l] == (b - y[i]) / y[l] and (b - y[i]) / y[l] >= 0:
    41                 f = 1;
    42                 break;
    43 
    44     if f:
    45         print "Yes";
    46     else:
    47         print "No";


  • 相关阅读:
    【转】程序员杂志:2011程序员薪资调查报告
    过滤HTML格式
    学习ASP.NET中的细节问题
    自己写的一些类代码
    "rs.open sql,conn,1,3 "的1,3的用处
    无须重装 Windows常遇问题通用解决方法
    Vista Beta下载
    asp学习
    SharePoint 和RMS装在同一台机器上可以么?
    介绍SharePoint与RMS集成的两篇重磅文章
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3703246.html
Copyright © 2020-2023  润新知