diff --git a/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp b/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp index f4d46c6..4a5c4ef 100644 --- a/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp +++ b/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6.cpp @@ -1,30 +1,50 @@ #include using namespace std; +typedef pair PII; +const int N = 110; +int a[N]; +int st[10]; +vector q; +int res; +int n; -/* -测试用例: -5 -12321 +int main() { +#ifndef ONLINE_JUDGE + freopen("6.in", "r", stdin); +#endif -*/ -int cnt = 1; + while (cin >> n && n) { + // 各种清空 + memset(a, 0, sizeof a); + memset(st, 0, sizeof st); + q.clear(); + res = 0; -// 递归写法 -void dfs(string s) { - int p; - for (p = s.size() - 1; p >= 1; p--) - if (s[0] != s[p]) break; + for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组 + for (int i = 1; i <= n; i++) { // 遍历每个位置 + if (!st[a[i]]) { // 如果此数字没有被处理过 + st[a[i]] = 1; // 标识已处理 + for (int j = n; j; j--) // 从后向前找到它的终止位置 + if (a[i] == a[j]) { + q.push_back({i, j}); // 记录有效区间 + break; + } + } + } - string t; - for (int i = 1; i <= p; i++) t += s[i]; - if (t.size()) cnt++, dfs(t); -} + // 从左到右枚举,所以,不再需再进行按左端点排序,现在就是按左端点排序完的 + // Q:求一组区间的相交区间个数?如果存在一个相交区间,就多加1个1 + res = q.size(); -int main() { - int n; - string s; - cin >> n >> s; - dfs(s); - cout << cnt << endl; + // 如果存在区间相交,则res++ + for (int i = 0; i < q.size(); i++) + for (int j = i + 1; j < q.size(); j++) + if (q[i].second > q[j].first && q[j].second > q[i].second) res++; + + for (int i = 1; i <= n; i++) cout << a[i] << " "; + cout << endl; + cout << res << endl; + cout << 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 deleted file mode 100644 index 887f098..0000000 --- a/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao15STEMA202401/6_Test.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -using namespace std; -typedef pair PII; -const int N = 110; -int a[N]; -int st[10]; -vector q; -int res; -int n; - -int main() { -#ifndef ONLINE_JUDGE - freopen("6.in", "r", stdin); -#endif - - while (cin >> n && n) { - // 各种清空 - memset(a, 0, sizeof a); - memset(st, 0, sizeof st); - q.clear(); - res = 0; - - for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组 - for (int i = 1; i <= n; i++) { // 遍历每个位置 - if (!st[a[i]]) { // 如果此数字没有被处理过 - st[a[i]] = 1; // 标识已处理 - for (int j = n; j; j--) // 从后向前找到它的终止位置 - if (a[i] == a[j]) { - q.push_back({i, j}); // 记录有效区间 - break; - } - } - } - - // 从左到右枚举,所以,不再需再进行按左端点排序,现在就是按左端点排序完的 - // Q:求一组区间的相交区间个数?如果存在一个相交区间,就多加1个1 - res = q.size(); - - // 如果存在区间相交,则res++ - for (int i = 0; i < q.size(); i++) - for (int j = i + 1; j < q.size(); j++) - if (q[i].second > q[j].first && q[j].second > q[i].second) res++; - - cout << res << endl; - } - return 0; -} \ No newline at end of file