QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#246247 | #7686. The Phantom Menace | salvator_noster | WA | 330ms | 269440kb | C++20 | 4.0kb | 2023-11-10 17:49:33 | 2023-11-10 17:49:34 |
Judging History
你现在查看的是最新测评结果
- [2024-10-08 14:11:03]
- hack成功,自动添加数据
- (/hack/941)
- [2024-10-08 10:05:28]
- hack成功,自动添加数据
- (/hack/940)
- [2024-10-07 19:51:15]
- hack成功,自动添加数据
- (/hack/938)
- [2024-10-07 19:28:01]
- hack成功,自动添加数据
- (/hack/937)
- [2024-10-07 17:16:32]
- hack成功,自动添加数据
- (/hack/936)
- [2024-10-07 16:53:09]
- hack成功,自动添加数据
- (/hack/935)
- [2024-10-07 16:22:17]
- hack成功,自动添加数据
- (/hack/934)
- [2023-11-10 17:49:33]
- 提交
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
typedef vector <int> vi;
const int MAX_N = 1000000 + 5;
const int Base1 = 233;
const int Base2 = 567;
const int Mod1 = 1258489069;
const int Mod2 = 1453570957;
void init();
int N, M, pw1[MAX_N], pw2[MAX_N];
char str[MAX_N];
string s[MAX_N], t[MAX_N];
int ids[MAX_N], idt[MAX_N];
struct Hash{
vector <int> ha1, ha2;
void build(const char *s) {
ha1.resize(M + 1);
ha2.resize(M + 1);
ha1[0] = ha2[0] = 0;
for (int i = 1; i <= M; i ++) {
ha1[i] = (1ll * ha1[i - 1] * Base1 + s[i]) % Mod1;
ha2[i] = (1ll * ha2[i - 1] * Base2 + s[i]) % Mod2;
}
}
pii get(int l, int r) {
int res1 = (ha1[r] - 1ll * ha1[l - 1] * pw1[r - l + 1] + 1ll * Mod1 * Mod1) % Mod1;
int res2 = (ha2[r] - 1ll * ha2[l - 1] * pw2[r - l + 1] + 1ll * Mod2 * Mod2) % Mod2;
return {res1, res2};
}
}has[MAX_N], hat[MAX_N];
vector <pii> edge[MAX_N << 2];
int ecur[MAX_N], lastnode;
vi ans;
inline bool cmp_s(int i, int j) {return s[i] < s[j];}
inline bool cmp_t(int i, int j) {return t[i] < t[j];}
void dfs(int u) {
lastnode = u;
for (int &i = ecur[u]; i < edge[u].size(); ) {
if (edge[u][i].second) {
ans.push_back(edge[u][i].second);
edge[u][i].second = 0;
dfs(edge[u][i ++].first);
}else i ++;
}
}
bool solve_graph(int tot) {
ans.clear(); lastnode = 0;
for (int i = 1; i <= tot; i ++) ecur[i] = 0;
dfs(1);
if (lastnode != 1) return false;
for (int i = 1; i <= tot; i ++)
if (ecur[i] != edge[i].size()) return false;
vi anss(0), anst(0);
for (auto x : ans)
if (x <= N) anss.push_back(x);
else anst.push_back(x - N);
assert(anss.size() == N && anst.size() == N);
for (int i = 0; i < N; i ++) printf("%d%c", anss[i], "\n "[i + 1 < N]);
for (int i = 0; i < N; i ++) printf("%d%c", anst[i], "\n "[i + 1 < N]);
return true;
}
void solve() {
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i ++) {
ids[i] = i;
idt[i] = i;
scanf("%s", str + 1);
has[i].build(str);
s[i].resize(M);
for (int j = 0; j < M; j ++) s[i][j] = str[j];
}
for (int i = 1; i <= N; i ++) {
scanf("%s", str + 1);
hat[i].build(str);
t[i].resize(M);
for (int j = 0; j < M; j ++) t[i][j] = str[j];
}
sort(ids + 1, ids + N + 1, cmp_s);
sort(idt + 1, idt + N + 1, cmp_t);
char flag = 1;
for (int i = 1; i <= N; i ++)
if (s[ids[i]] != t[idt[i]]) {
flag = 0; break;
}
if (flag) {
for (int i = 1; i <= N; i ++) printf("%d%c", ids[i], "\n "[i < N]);
for (int i = 1; i <= N; i ++) printf("%d%c", idt[i], "\n "[i < N]);
return ;
}
for (int i = 1; i < M; i ++) {
int tot = 0;
map <pii, int> idxs, idxt;
for (int j = 1; j <= N * 4; j ++) edge[j].clear();
for (int j = 1; j <= N; j ++) {
pii sj1 = has[j].get(1, i), sj2 = has[j].get(i + 1, M);
pii tj1 = hat[j].get(1, M - i), tj2 = hat[j].get(M - i + 1, M);
#define allocate(idx, x) (idx.count(x) ? idx[x] : (idx[x] = ++ tot))
int u1 = allocate(idxs, sj1), v1 = allocate(idxt, sj2);
int u2 = allocate(idxt, tj1), v2 = allocate(idxs, tj2);
#undef allocate
edge[u1].emplace_back(v1, j);
edge[u2].emplace_back(v2, j + N);
}
if (solve_graph(tot)) {
return ;
}
}
puts("-1");
}
int main() {
init();
int T;
scanf("%d", &T);
while (T --) solve();
return 0;
}
void init() {
pw2[0] = 1; pw1[0] = 1;
for (int i = 1; i < MAX_N; i ++) {
pw1[i] = 1ll * pw1[i - 1] * Base1 % Mod1;
pw2[i] = 1ll * pw2[i - 1] * Base2 % Mod2;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 47ms
memory: 267952kb
input:
2 3 3 abc ghi def bcd efg hia 1 3 abc def
output:
1 3 2 1 2 3 -1
result:
ok 2 cases (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 330ms
memory: 269440kb
input:
1000000 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 ...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
wrong answer not cyclic isomorphism (test case 2)