You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
/*
5
2 3 4 5 2
答案4
5
3 2 4 2 5
答案4
5
3 2 4 2 3
答案3
7
3 2 4 2 5 3 1
答案5
5
3 2 3 2 5
答案4
*/
int res; // 一共处理几次
// 是不是全是字符零
bool check(string s) {
for (int i = 0; i < s.size(); i++)
if (s[i] != '0') return false;
return true;
}
// 结构体
struct Node {
int c;
int count;
int st, ed;
bool const operator<(const &Node t) {
if (count != t.count) return count > t.count;
if (st < t.st && ed > t.ed) return false;
return true;
}
};
int p1[10], p2[10];
vector<Node> a;
int main() {
int n; // n个长度的字符串
string s; // 字符串
cin >> n >> s;
// 多次循环,判断是不是已经全是'0'的字符串,如果不是,则继续处理
while (!check(s)) {
// 记录有多少个非0数字都是多少个
memset(b, 0, sizeof b);
memset(p1, -1, sizeof p1);
memset(p2, -1, sizeof p2);
for (int i = 0; i < s.size(); i++) {
if (s[i] != '0') {
b[s[i] - '0']++;
if (p1[s[i] - '0'] == -1)
p1[s[i] - '0'] = i; // 记录起点
else
p2[s[i] - '0'] = i; // 记录终点
}
}
// 整理一下
vector<Node> q;
for (int i = 1; i <= 9; i++)
if (b[i]) q.push_back({i, b[i], p1[i], p2[i] == -1 ? p1[i] : p2[i]}); // 几个在前,是啥数字在后
sort(q.begin(), q.end());
// 最多的数字是num,个数=count,这个是最牛X的
int num = q[0].second;
// 找出这个数字的起止位置
for (int i = 0; i < q.size(); i++)
if ((int)s[i] == num) s[i] = '0';
}
// 输出一共处理了几次
cout << res << endl;
return 0;
}