Oiclass P3309 题解

这道是模拟题

思路如下

  • 先输入 \(n\) 和 字符串
  • 遍历字符串,如果没有出现过且\(当前排队人数<n\) ,将其标记,并将当前排队人数 \(+1\),如果\(当前排队人数 ≥n\) ,不标记,同时将答案 \(+1\)
  • 如果已经出现过了,将当前人数 \(-1\) 同时将其标位为出现

实现

实现起来很简单,首先定义一个整数 \(n\) 、一个字符串 \(str\) 、一个布尔数组 \(p[200]\),分别用于存储:题目中的 \(n\) 、题目中的字符串、标记字母是否出现过

然后按思路模拟,注意输出的时候 \(ans\)\(÷ 2\) 也可以 \(>>1\) 更快一些(尽管只有几纳秒)

代码如下:

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
#include <iostream>

using namespace std;
int n, cnt, ans;
string str;
bool p[200];

int main() {
cin >> n >> str;
for (char i: str) {
if (!p[i]) {
p[i] = true;
if (cnt >= n) {
p[i] = false;
ans += 1;
} else {
cnt += 1;
}
} else {
p[i] = false;
cnt -= 1;
}
}
cout << (ans >> 1);
}

Oiclass P3309 题解
https://lixuannan.github.io/posts/22979
作者
CodingCow Lee
发布于
2023年1月1日
许可协议