QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#610877 | #8058. Binary vs Ternary | yimg# | WA | 1ms | 3592kb | C++20 | 2.5kb | 2024-10-04 17:53:59 | 2024-10-04 17:53:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// Helper function to convert a ternary substring into binary
string ternaryToBinary(string ternary) {
int decimal_value = 0;
int power = 1;
// Convert ternary to decimal
for (int i = ternary.size() - 1; i >= 0; --i) {
decimal_value += (ternary[i] - '0') * power;
power *= 3;
}
// Convert decimal to binary
string binary = "";
while (decimal_value > 0) {
binary = (decimal_value % 2 == 0 ? '0' : '1') + binary;
decimal_value /= 2;
}
return binary.empty() ? "0" : binary;
}
int main() {
int T;
cin >> T;
while (T--) {
string A, B;
cin >> A >> B;
// If lengths are different, output -1
if (A.size() != B.size()) {
cout << "-1\n";
continue;
}
// Initialize a list of operations
vector<pair<int, int>> operations;
// Try transforming A to B within the limits of the problem
bool possible = true;
for (int i = 0; i < A.size(); ++i) {
if (A[i] != B[i]) {
// Try to find a substring in A that can be transformed to help match B
for (int l = i; l < A.size(); ++l) {
for (int r = l; r < A.size(); ++r) {
string ternary = A.substr(l, r - l + 1);
string binary = ternaryToBinary(ternary);
// Replace the substring in A with the binary equivalent
string newA = A.substr(0, l) + binary + A.substr(r + 1);
if (newA == B) {
operations.push_back({l + 1, r + 1});
A = newA;
break;
}
}
if (A == B) break;
}
if (A != B) {
possible = false;
break;
}
}
}
// Output the result
if (possible && operations.size() <= 512 && A == B) {
cout << operations.size() << '\n';
for (auto &op : operations) {
cout << op.first << ' ' << op.second << '\n';
}
} else {
cout << "-1\n";
}
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3592kb
input:
3 1 111 110110 1101010 1111 111111
output:
-1 -1 -1
result:
wrong answer Pans=-1, Jans!=-1 (test case 2)