QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#610877#8058. Binary vs Ternaryyimg#WA 1ms3592kbC++202.5kb2024-10-04 17:53:592024-10-04 17:53:59

Judging History

你现在查看的是最新测评结果

  • [2024-10-04 17:53:59]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3592kb
  • [2024-10-04 17:53:59]
  • 提交

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;
}

Details

Tip: Click on the bar to expand more detailed information

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)