| Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
|---|---|---|---|---|---|---|---|---|
| 160617 | 郦辰希 | 字符串反转 | C++ | Accepted | 5 MS | 296 KB | 1283 | 2026-09-24 20:33:21 |
#include <bits/stdc++.h> using namespace std; int main() { // 优化IO效率 ios::sync_with_stdio(false); cin.tie(NULL); int T; if (!(cin >> T)) return 0; string line; // 消耗掉读取T后留下的换行符,防止getline读取空行 getline(cin, line); while (T--) { // 1. 读取整行文本 getline(cin, line); int n = line.length(); int start = 0; // 2. 遍历字符串,识别每个单词并反转 while (start < n) { // 跳过空格 if (line[start] == ' ') { start++; continue; } // 找到单词的结束位置 int end = start; while (end < n && line[end] != ' ') { end++; } // 3. 反转区间 [start, end) 内的字符 // 使用标准库 reverse 函数,安全且高效 reverse(line.begin() + start, line.begin() + end); // 移动 start 到下一个位置 start = end; } // 4. 输出处理后的整行 cout << line << endl; } return 0; }