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.

100 lines
2.4 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
算法:
1、开a[N]数组,记录目标密码锁的密码,本例就是 a[]={ 2 , 3 , 4 , 5 , 2}
2、模拟我们从{0,0,0,0,0}出发,看看是不是到达了目标状态
3、如果还没有到达目标状态我们来研究一下怎么能尽快的向目标逼近
1出现次数最多的数字起始终止
2如果有个次数一样多的数字那么起始终止范围大的在前
3如果不存在范围包含关系那么号小的在前(其实是随意的)
{2,2,2,2,2} res=1
3 2 2 3 2 2 3 2 2 2 3 3 3 3
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的
for (int i = q[0].st; i <= q[0].ed; i++) s[i] = q[0].c;
}
// 输出一共处理了几次
cout << res << endl;
return 0;
}