题目
解题思路
- 从(0,0)点开始,根据输入的指令序列移动坐标
- 对输入字符串按分号分割成单个指令
- 对每个指令进行合法性验证:
- 第一个字符必须是A/D/W/S
- 后面必须是1-2位的数字
- 根据方向更新坐标:
- A: x减去数字
- D: x加上数字
- W: y加上数字
- S: y减去数字
- 输出最终坐标
代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isValidCommand(string& cmd) {
if (cmd.length() < 2 || cmd.length() > 3) return false;
if (cmd[0] != 'A' && cmd[0] != 'D' && cmd[0] != 'W' && cmd[0] != 'S') return false;
for (int i = 1; i < cmd.length(); i++) {
if (!isdigit(cmd[i])) return false;
}
return stoi(cmd.substr(1)) <= 99;
}
int main() {
string input;
getline(cin, input);
int x = 0, y = 0;
string cmd;
for (int i = 0; i < input.length(); i++) {
if (input[i] == ';') {
if (isValidCommand(cmd)) {
int num = stoi(cmd.substr(1));
switch(cmd[0]) {
case 'A': x -= num; break;
case 'D': x += num; break;
case 'W': y += num; break;
case 'S': y -= num; break;
}
}
cmd = "";
} else {
cmd += input[i];
}
}
// 处理最后一个命令
if (isValidCommand(cmd)) {
int num = stoi(cmd.substr(1));
switch(cmd[0]) {
case 'A': x -= num; break;
case 'D': x += num; break;
case 'W': y += num; break;
case 'S': y -= num; break;
}
}
cout << x << "," << y << endl;
return 0;
}
import java.util.Scanner;
public class Main {
public static boolean isValidCommand(String cmd) {
if (cmd.length() < 2 || cmd.length() > 3) return false;
if (!"ADWS".contains(cmd.substring(0, 1))) return false;
try {
int num = Integer.parseInt(cmd.substring(1));
return num <= 99;
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] commands = sc.nextLine().split(";");
int x = 0, y = 0;
for (String cmd : commands) {
if (!cmd.isEmpty() && isValidCommand(cmd)) {
int num = Integer.parseInt(cmd.substring(1));
switch (cmd.charAt(0)) {
case 'A': x -= num; break;
case 'D': x += num; break;
case 'W': y += num; break;
case 'S': y -= num; break;
}
}
}
System.out.println(x + "," + y);
}
}
def is_valid_command(cmd):
if len(cmd) < 2 or len(cmd) > 3:
return False
if cmd[0] not in 'ADWS':
return False
try:
num = int(cmd[1:])
return 0 <= num <= 99
except:
return False
def move_coordinate():
x, y = 0, 0
commands = input().split(';')
for cmd in commands:
if not cmd or not is_valid_command(cmd):
continue
direction = cmd[0]
distance = int(cmd[1:])
if direction == 'A':
x -= distance
elif direction == 'D':
x += distance
elif direction == 'W':
y += distance
elif direction == 'S':
y -= distance
print(f"{x},{y}")
if __name__ == "__main__":
move_coordinate()
算法及复杂度
- 算法:字符串处理 + 模拟
- 时间复杂度: O ( n ) \mathcal{O}(n) O(n),其中n是输入字符串的长度
- 空间复杂度: O ( n ) \mathcal{O}(n) O(n),需要存储分割后的指令列表
解题思路
这是一个字符串处理问题,需要找出字符串中最长的连续数字串。
关键点
-
输入规则:
- 字符串长度: 1 ≤ n ≤ 200 1 \leq n \leq 200 1≤n≤200
- 保证每组输入都至少含有一个数字
-
输出要求:
- 输出最长的数字串及其长度
- 如果有多个相同长度的数字串,需要全部输出
- 按原字符串中的相对位置输出
实现思路
-
遍历处理:
- 遍历字符串,找到连续数字子串
- 记录每个数字串及其长度
- 找出最长长度
-
结果输出:
- 找出所有最长长度的数字串
- 按原顺序输出
代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void findLongestDigits(string& str) {
vector<string> result;
int maxLen = 0;
string current;
// 遍历字符串
for (int i = 0; i <= str.length(); i++) {
if (i < str.length() && isdigit(str[i])) {
current += str[i];
} else if (!current.empty()) {
// 找到一个数字串
if (current.length() > maxLen) {
maxLen = current.length();
result.clear();
result.push_back(current);
} else if (current.length() == maxLen) {
result.push_back(current);
}
current.clear();
}
}
// 输出结果
for (int i = 0; i < result.size(); i++) {
cout << result[i];
}
cout << "," << maxLen << endl;
}
int main() {
string str;
while (getline(cin, str)) {
findLongestDigits(str);
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str = sc.nextLine();
findLongestDigits(str);
}
}
public static void findLongestDigits(String str) {
List<String> result = new ArrayList<>();
int maxLen = 0;
StringBuilder current = new StringBuilder();
// 遍历字符串
for (int i = 0; i <= str.length(); i++) {
if (i < str.length() && Character.isDigit(str.charAt(i))) {
current.append(str.charAt(i));
} else if (current.length() > 0) {
// 找到一个数字串
if (current.length() > maxLen) {
maxLen = current.length();
result.clear();
result.add(current.toString());
} else if (current.length() == maxLen) {
result.add(current.toString());
}
current = new StringBuilder();
}
}
// 输出结果
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i));
}
System.out.println("," + maxLen);
}
}
def find_longest_digits(s):
result = []
max_len = 0
current = ''
# 遍历字符串
for i in range(len(s) + 1):
if i < len(s) and s[i].isdigit():
current += s[i]
elif current:
# 找到一个数字串
if len(current) > max_len:
max_len = len(current)
result = [current]
elif len(current) == max_len:
result.append(current)
current = ''
# 输出结果
print(''.join(result) + ',' + str(max_len))
while True:
try:
s = input()
find_longest_digits(s)
except:
break
算法及复杂度
算法分析
-
遍历过程:
- 逐字符遍历字符串
- 使用临时变量记录当前数字串
- 维护最大长度和结果列表
-
结果处理:
- 记录所有最长数字串
- 按原顺序保存和输出
复杂度分析
- 时间复杂度: O ( n ) \mathcal{O}(n) O(n) - 其中 n n n 是字符串长度,只需要遍历一次
- 空间复杂度: O ( n ) \mathcal{O}(n) O(n) - 需要存储找到的数字串
601

被折叠的 条评论
为什么被折叠?



