QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#722859 | #2518. Puzzle Game | ucup-team5062# | WA | 1ms | 3612kb | C++17 | 3.1kb | 2024-11-07 20:26:13 | 2024-11-07 20:26:13 |
Judging History
answer
#include <array>
#include <cmath>
#include <string>
#include <vector>
#include <cstdint>
#include <iostream>
#include <algorithm>
using namespace std;
uint64_t seed = 1234567891234567891;
uint64_t xorshift64() {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 7;
return seed;
}
int rand_int(int l, int r) {
return l + int(xorshift64() % (r - l));
}
const int INF = 1012345678;
const vector<string> code_recov = { "AA", "AB", "BB", "AC", "BC", "CC", "AD", "BD", "CD", "DD" };
int get_code(char c1, char c2) {
if (c1 > c2) {
swap(c1, c2);
}
int x = c1 - 'A', y = c2 - 'A';
return y * (y + 1) / 2 + x;
}
string subsolve(int N, const string& S, const string& T) {
int M = T.size();
array<int, 4> d1 = {0};
array<int, 4> d2 = {0};
for (int i = 0; i < N; i++) {
d1[S[i] - 'A']++;
}
for (int i = 0; i < M; i++) {
d2[T[i] - 'A']++;
}
string alpha;
for (int i = 0; i < 4; i++) {
if (d1[i] < d2[i]) {
return string();
}
alpha += string(d1[i] - d2[i], 'A' + i);
}
array<int, 10> c1 = {0};
array<int, 10> c2 = {0};
for (int i = 0; i < N - 1; i++) {
c1[get_code(S[i], S[i + 1])]++;
}
for (int i = 0; i < M - 1; i++) {
c2[get_code(T[i], T[i + 1])]++;
}
const int MAX_LOOPS = 500000;
auto get_score = [&](const vector<int>& seq) -> int {
array<int, 10> c = c2;
for (int i = 0; i < N - M; i++) {
if (c[seq[i]] == 0) {
return INF;
}
c[seq[i]]--;
c[get_code(alpha[i], code_recov[seq[i]][0])]++;
c[get_code(alpha[i], code_recov[seq[i]][1])]++;
}
int score = 0;
for (int i = 0; i < 10; i++) {
score += abs(c[i] - c1[i]);
}
return score;
};
vector<int> curseq(N - M);
for (int i = 0; i < N - M; i++) {
curseq[i] = get_code(T[0], i != 0 ? alpha[i - 1] : T[1]);
}
int curscore = get_score(curseq);
if (N > M) {
int loops = 0;
while (loops < MAX_LOOPS && curscore > 0) {
loops++;
vector<int> nxtseq = curseq;
nxtseq[rand_int(0, N - M)] = rand_int(0, 10);
int nxtscore = get_score(nxtseq);
if (nxtscore != INF) {
curscore = nxtscore;
curseq = nxtseq;
}
}
}
if (curscore != 0) {
return string();
}
string ans = T;
for (int i = 0; i < N - M; i++) {
for (int j = 0; j < int(ans.size()) - 1; j++) {
if (get_code(ans[j], ans[j + 1]) == curseq[i]) {
ans.insert(ans.begin() + j + 1, alpha[i]);
break;
}
}
}
return ans;
};
string increase(string S, char head, char tail) {
if (S[0] != head) S = head + S;
if (S.back() != tail) S = S + tail;
return S;
}
string solve(int N, int M, const string& S, const string& T) {
string res1 = subsolve(N, S, increase(T, S[0], S.back()));
if (!res1.empty()) {
return res1;
}
if (S[0] != S[1]) {
string res2 = subsolve(N, S, increase(T, S[1], S.back()));
if (!res2.empty()) {
return res2;
}
}
return string();
}
int main() {
int Q;
cin >> Q;
for (int id = 1; id <= Q; id++) {
int N, M; string S, T;
cin >> N >> M >> S >> T;
string ans = solve(N, M, S, T);
if (!ans.empty()) {
cout << ans << endl;
} else {
cout << "-1" << endl;
}
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3612kb
input:
3 7 3 ABADCAB CBB 11 7 ABACCDBADAC AADCDAC 3 2 ABA CB
output:
ADCABAB ABABDACCDAC -1
result:
wrong answer Wrong Answer: participant outputs an incorrect string. Judge's output: NO Participant's output: -1 (test case 3)