2021团体程序设计天梯赛 L3-2 还原文件
思路:
dfs+回溯,不加回溯只有26分
Tip:
直接拿纸条往上怼
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
struct mmp {
int i;
int at;
vector<int> v;
} member[105];
int number[maxn];
int n, p;
bool flag = false;
bool cmp1(mmp a, mmp b) {
return a.v.size() > b.v.size();
}
bool cmp2(mmp a, mmp b) {
return a.at < b.at;
}
void dfs(int now) {
if (now == n && !flag) {
flag = true;
sort(member + 1, member + 1 + p, cmp2);
bool first = true;
for (int i = 1; i <= p; i++) {
if (first) {
first = false;
cout << member[i].i;
} else
cout << " " << member[i].i;
}
return;
}
for (int i = 1; i <= p; i++) {
bool flag1 = true;
for (int j = 0; j < member[i].v.size(); j++) {
if (member[i].v[j] != number[now + j]) {
flag1 = false;
break;
}
}
if (flag1) {
member[i].at = now;
dfs(now + member[i].v.size() - 1);
if (flag)
return;
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> number[i];
}
cin >> p;
for (int i = 1; i <= p; i++) {
int t;
cin >> t;
member[i].i = i;
for (int j = 1; j <= t; j++) {
int tmp;
cin >> tmp;
member[i].v.push_back(tmp);
}
}
sort(member + 1, member + 1 + p, cmp1);
dfs(1);
return 0;
}