QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#514865 | #5161. Last Guess | Tobo | RE | 0ms | 0kb | C++20 | 3.5kb | 2024-08-11 12:47:06 | 2024-08-11 12:47:06 |
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int g, l;
cin >> g >> l;
g--;
vector<string> a(g), res(g);
for (int i = 0; i < g; i++)
cin >> a[i] >> res[i];
string ans = string(l, char('a' - 1));
for (int i = 0; i < g; i++)
for (int j = 0; j < l; j++)
if (res[i][j] == 'G')
ans[j] = a[i][j];
vector<vector<int>> ban(l, vector<int>(26));
vector<int> req(26);
vector<int> lim(26);
for (int i = 0; i < g; i++)
{
for (int j = 0; j < l; j++)
if (res[i][j] != 'G')
ban[j][a[i][j] - 'a'] = 1;
for (int j = 0; j < l; j++)
if (res[i][j] == 'B')
lim[a[i][j] - 'a'] = 1;
vector<int> cnt(26);
for (int j = 0; j < l; j++)
if (res[i][j] != 'B')
cnt[a[i][j] - 'a']++;
for (int c = 0; c < 26; c++)
req[c] = max(req[c], cnt[c]);
}
int s = 0, t = 26 + l + 1;
vector<int> head(t + 1, -1), nxt, to, cap;
auto add = [&](int x, int y, int z)
{
to.push_back(y);
cap.push_back(z);
nxt.push_back(head[x]);
head[x] = to.size() - 1;
to.push_back(x);
cap.push_back(0);
nxt.push_back(head[y]);
head[y] = to.size() - 1;
};
for (int j = 0; j < l; j++)
if (ans[j] != ' ')
req[ans[j] - 'a']--;
for (int c = 0; c < 26; c++)
add(s, c + 1, req[c]);
int sum = accumulate(req.begin(), req.end(), 0);
for (int i = 0; i < l; i++)
{
int id = 27 + i;
for (int c = 0; c < 26; c++)
if (!ban[i][c] && ans[i] == char('a' - 1))
add(c + 1, id, 1);
add(id, t, 1);
}
vector<int> arc(t + 1), dis(t + 1);
auto bfs = [&]() -> bool
{
dis.assign(t + 1, -1);
dis[s] = 0;
queue<int> que;
que.push(s);
while (!que.empty())
{
int cur = que.front();
que.pop();
arc[cur] = head[cur];
for (int p = head[cur]; ~p; p = nxt[p])
{
if (cap[p] && dis[to[p]] == -1)
{
dis[to[p]] = dis[cur] + 1;
que.push(to[p]);
}
}
}
return dis[t] != -1;
};
auto dfs = [&](auto &dfs, int cur, int flow) -> int
{
if (cur == t)
return flow;
int used = 0;
for (int &p = arc[cur]; ~p; p = nxt[p])
{
if (dis[to[p]] == dis[cur] + 1 && cap[p])
{
int k = dfs(dfs, to[p], min(flow - used, cap[p]));
used += k;
cap[p] -= k, cap[p ^ 1] += k;
if (27 <= to[p] && to[p] < t && cur >= 1 && cur <= 26 && k)
ans[to[p] - 27] = char('a' + cur - 1);
if (used == flow)
break;
}
}
return used;
};
while (bfs())
sum -= dfs(dfs, s, 1e9);
assert(sum == 0);
for (int i = 0; i < l; i++)
{
if (ans[i] == char('a' - 1))
{
for (int c = 0; c < 26; c++)
if (!ban[i][c] && !lim[c])
ans[i] = char('a' + c);
}
}
cout << ans << '\n';
}
详细
Test #1:
score: 0
Runtime Error
input:
4 5 reply YYGBB refer BBBGG puppy YYGBB