分类:字符串
知识点:
-  正则匹配 re.match(pattern, move) 
-  格式字符串,可以在字符串中直接引用变量 f"{x},{y}" 
题目来自【牛客】

import re  def is_valid_coordinate(move):  # 使用正则表达式验证移动是否合法  # ^: 表示字符串的开始。# [ADWS]: 匹配一个字符,该字符必须是 A、D、W 或 S 中的一个。# [0-9]{1,2}: 匹配 1 到 2 个数字。[0-9] 表示匹配任意一个数字,{1,2} 表示前面的数字可以重复 1 到 2 次。# $: 表示字符串的结束。pattern = r'^[ADWS][0-9]{1,2}$' # 返回匹配则返回<re.Match object; span=(0, 3), match='A12'> # 没有则返回Nonereturn re.match(pattern, move) is not None  def calculate_coordinates(input_string):  # 初始化坐标为原点  x, y = 0, 0  # 遍历输入字符串中的每个字符  moves = input_string.split(';')  for move in moves:  # 检查移动是否有效  if is_valid_coordinate(move):  # 获取移动方向和距离  direction = move[0]  distance = int(move[1:])  # 距离为输入字符串的剩余部分,不需要额外验证  # 根据方向移动对应的距离  if direction == 'A':  # 向左移动  x -= distance  elif direction == 'D':  # 向右移动  x += distance  elif direction == 'W':  # 向上移动  y += distance  elif direction == 'S':  # 向下移动  y -= distance  else:  # 如果移动无效,忽略它  pass  return f"{x},{y}"  # 返回最终的坐标,格式化为字符串  # input_string = "A10;S20;W10;D30;X;A1A;B10A11;;A10;"  
input_string = input().strip()
output_string = calculate_coordinates(input_string)  
print(output_string)  # 输出:10, -10