TLV编码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main() {
string target;
getline(cin, target);
string line;
getline(cin, line);

vector<string> data;
istringstream iss(line);
string sig;
while (iss >> sig) {
data.push_back(sig);
}

size_t size = data.size();
for (int i = 0; i < size;) {
// 读取一个字节
auto &compare_val = data[i];
// 读取两个字节
auto step = stoi(data[i + 2]) + stoi(data[i + 1]);
// 找到符合的目标值
if (compare_val == target) {
for (int j = i + 3; j < (i + 3 + step); ++j) {
cout << data[j] << " ";
}
break;
} else {
// 更新下标
i += 1 + 2 + step;
}
}

return 0;
}

说说阅读题目遇到的障碍:

1-Length 固定占两个字节,字节序为小端序,求和?

实际上就是 把 两个字符串 各自转换为 数字,然后求两个数的和,代表后面 value 的 个数。

2-最后一个数不允许输出空格?

我还以为输出会有特殊要求,即最后一个数不允许输出空格,事实并非如此。看来没有在这个上来搞幺蛾子。