diff --git a/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp b/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp index b08338e..f4d46c6 100644 --- a/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp +++ b/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp @@ -1,99 +1,30 @@ #include 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 +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 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 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; -} +} \ No newline at end of file diff --git a/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6_Test.cpp b/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6_Test.cpp new file mode 100644 index 0000000..4eb2742 --- /dev/null +++ b/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6_Test.cpp @@ -0,0 +1,63 @@ +#include +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 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; +} \ No newline at end of file