main
黄海 1 year ago
parent 2434c2742d
commit 79420e8e0f

@ -1,99 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
/*
5
2 3 4 5 2
4
1a[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
12321
*/
int cnt = 1;
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];
// 递归写法
void dfs(string s) {
int p;
for (p = s.size() - 1; p >= 1; p--)
if (s[0] != s[p]) break;
vector<Node> a;
string t;
for (int i = 1; i <= p; i++) t += s[i];
if (t.size()) cnt++, dfs(t);
}
int main() {
int n; // n个长度的字符串
string s; // 字符串
int 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;
dfs(s);
cout << cnt << endl;
return 0;
}
}

@ -0,0 +1,63 @@
#include <bits/stdc++.h>
using namespace std;
/*
14
3 2 2 3 2 2 3 2 2 2 3 3 3 3
*/
const int N = 110;
int n;
int a[N];
int res; // 执行次数
int st[10]; // 哪些数字使用过了
int b[10];
// 结构体
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;
}
};
// 找出未处理的数字中符合条件的那个
Node find() {
memset(b, 0, sizeof b);
vector<Node> q;
for (int i = 1; i <= n; i++)
if (!st[a[i]]) b[a[i]]++; // 个数
for (int i = 1; i <= 9; i++) {
int start, end;
for (int j = 1; j <= n; j++)
if (a[j] == i) {
start = j;
break;
}
for (int j = n; j >= 1; j--)
if (a[j] == i) {
end = j;
break;
}
q.push_back({i, b[i], start, end});
}
sort(q.begin(), q.end());
return q[0];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组
for (res = 0;; res++) {
// 找出没有记录过的数字,并且,它的数量最多,如果存在数量一样多的多个数字,就比较谁的区间大,大的优先,如果无法比较谁的区间大,就数小的优先
Node x = find();
if (x.count == 0) break;
}
cout << res << endl;
return 0;
}
Loading…
Cancel
Save