QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#758188 | #9558. The Devil | hos_lyric | WA | 2ms | 9788kb | C++14 | 5.1kb | 2024-11-17 16:37:16 | 2024-11-17 16:37:16 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
namespace bm {
constexpr int LIM_N0 = 130 * 130;
constexpr int LIM_N1 = 130;
int n0, n1;
bitset<LIM_N1> adj[LIM_N0];
int to[LIM_N0], fr[LIM_N1];
int zeit, used[LIM_N0];
void init(int n0_, int n1_) {
n0 = n0_; n1 = n1_;
for (int u = 0; u < n0; ++u) adj[u].reset();
}
void ae(int u, int v) {
adj[u].set(v);
}
int augment(int u) {
used[u] = zeit;
for (int v = 0; v < n1; ++v) if (adj[u][v]) {
const int w = fr[v];
if (!~fr[v] || (used[w] != zeit && augment(w))) {
to[u] = v; fr[v] = u; return 1;
}
}
return 0;
}
int run() {
memset(to, ~0, n0 * sizeof(int));
memset(fr, ~0, n1 * sizeof(int));
memset(used, ~0, n0 * sizeof(int));
int tof = 0;
for (zeit = 0; zeit < n0; ++zeit) tof += augment(zeit);
return tof;
}
} // namespace bm
constexpr int MAX_U = 130 * 130 * 130;
constexpr int A = 52;
int tr(char c) {
return isupper(c) ? (0 + (c - 'A')) : (26 + (c - 'a'));
}
char rt(int a) {
return (a < 26) ? ('A' + (a - 0)) : ('a' + (a - 26));
}
int U;
int nxt[MAX_U][A];
pair<int, int> prv[MAX_U];
int zeit, used[MAX_U];
int newNode() {
const int u = U++;
fill(nxt[u], nxt[u] + A, -1);
used[u] = 0;
return u;
}
int go(int u, int a) {
int &v = nxt[u][a];
if (!~v) {
v = newNode();
prv[v] = make_pair(u, a);
}
return v;
}
void init() {
U = 0;
zeit = 0;
newNode();
}
char buf[130 * 130];
char *readln() {
return fgets(buf, sizeof(buf), stdin);
}
int N;
vector<vector<string>> S;
int main() {
for (; readln(); ) {
N = atoi(buf);
S.assign(N, {});
for (int i = 0; i < N; ++i) {
readln();
istringstream iss(buf);
for (string s; iss >> s; S[i].push_back(s)) {}
}
// cerr<<"S = "<<S<<endl;
init();
// (depth, u)
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < N; ++i) {
const int lim = (int)S[i].size() + N + 1;
// depth -> u
vector<vector<int>> uss(lim);
uss[0].push_back(0);
for (const string &s : S[i]) {
++zeit;
int cnt = 0;
vector<vector<int>> vss(lim);
// (node, pos in s)
vector<pair<int, int>> ps;
for (int d = 0; d < lim; ++d) {
for (const int u : uss[d]) ps.emplace_back(u, 0);
vector<pair<int, int>> qs;
for (const auto &p : ps) if (p.second < (int)s.size()) {
const int u = p.first;
const int a = tr(s[p.second]);
const int v = go(u, a);
if (used[v] != zeit) {
vss[d + 1].push_back(v);
if (++cnt == N) goto done;
qs.emplace_back(v, p.second + 1);
}
}
ps.swap(qs);
}
done:{}
// cerr<<"vss = "<<vss<<endl;
uss.swap(vss);
}
for (int d = 0; d < lim; ++d) for (const int u : uss[d]) graph[i].emplace_back(d, u);
}
vector<pair<int, int>> dus;
for (int i = 0; i < N; ++i) for (const auto &du : graph[i]) dus.push_back(du);
sort(dus.begin(), dus.end());
dus.erase(unique(dus.begin(), dus.end()), dus.end());
const int dusLen = dus.size();
// greedy for dus
bm::init(dusLen, N);
for (int i = 0; i < N; ++i) for (const auto &du : graph[i]) {
const int j = lower_bound(dus.begin(), dus.end(), du) - dus.begin();
bm::ae(j, i);
}
const int res = bm::run();
if (res == N) {
for (int i = 0; i < N; ++i) {
const int j = bm::fr[i];
assert(~j);
string t;
for (int v = dus[j].second; v; ) {
const int u = prv[v].first;
const int a = prv[v].second;
t += rt(a);
v = u;
}
reverse(t.begin(), t.end());
puts(t.c_str());
}
} else {
puts("no solution");
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 9788kb
input:
5 automated teller machine active teller machine active trouble maker always telling misinformation American Teller Machinery
output:
autm atem atma atm ATM
result:
ok len=18
Test #2:
score: 0
Accepted
time: 0ms
memory: 7968kb
input:
5 Forest Conservation Committee Forum Fuming Corruption Collusion Federation Fulsome Cash Concealment Foundation Funky Crony Capitalism Facilitator Funny Cocky Cocky Funny
output:
FoCCF FCoCF FCCFo FCCF FCCoF
result:
ok len=24
Test #3:
score: 0
Accepted
time: 1ms
memory: 7612kb
input:
3 A AA AA A A A A
output:
no solution
result:
ok len=-1
Test #4:
score: 0
Accepted
time: 0ms
memory: 7580kb
input:
2 QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmertyuiop Q W E R T Y U I O P A S D F G H J K L Z X C V B N M q w e r t y u i o p a s d f g h j k l z x c v b n m j k l z x c v b n m
output:
Q QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmjklzxcvbnm
result:
ok len=63
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 7760kb
input:
10 aaa aaa aaa aaa aaa aaa aab aaa aaa aaa aaa aaa aaa aab aaa aaa aaa aaa aab aab aaa aaa aaa aaa a a a a a a ab ab a a a a a a ab ab b a a a a a a aw a a a a a az az a a a a az a a a a a
output:
no solution
result:
wrong answer Jury has the answer but participant has not