跳到正文

剪贴板

剪贴板 x1y047ue

原文
剪贴板信息

剪贴板信息

收录
更新
C++
// Idea by SmokingTurtle, Code by Gemini
#include <string>
#include <vector>
#include <iostream> // 用于错误输出

// For shared memory
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring> // For strcpy, strlen

const key_t SHM_KEY = 0x1234; // Example key
const int MAX_LEN = 1000000 + 5; // Max length of S plus some buffer


// Helper function to handle errors (optional, but good for debugging)
void shm_error(const char* msg) {
    perror(msg);
}

int Alice(std::string S) {
    // 1. Create/Get a shared memory segment.
    //    IPC_CREAT: Create if it doesn't exist.
    //    0666: Read/write permissions for owner, group, others.
    //    Size: S.length() + 1 for the null terminator if treating as C-string.
    int shmid = shmget(SHM_KEY, S.length() + 1, IPC_CREAT | 0666);
    if (shmid == -1) {
        shm_error("Alice: shmget failed");
        return -1; // Indicate error, though problem expects X in [0, 2^20)
    }

    // 2. Attach the shared memory segment to Alice's address space.
    //    shmat returns a char* (void*) pointer to the attached segment.
    char* shm_ptr = (char*)shmat(shmid, NULL, 0);
    if (shm_ptr == (char*)-1) {
        shm_error("Alice: shmat failed");
        // Clean up shm segment if created
        shmctl(shmid, IPC_RMID, NULL);
        return -1; // Indicate error
    }

    // 3. Copy string S into the shared memory.
    //    We use S.length() to copy content, and add a null terminator.
    strncpy(shm_ptr, S.c_str(), S.length());
    shm_ptr[S.length()] = '\0'; // Null-terminate the string in shared memory

    // 4. Detach the shared memory segment from Alice's address space.
    //    The data remains in the shared memory.
    if (shmdt(shm_ptr) == -1) {
        shm_error("Alice: shmdt failed");
        // Even if detach fails, try to mark for removal (Bob will do it anyway)
        // shmctl(shmid, IPC_RMID, NULL); // Bob should do this
        return -1; // Indicate error
    }

    // Alice doesn't need to pass information about S via X,
    // as Bob will read S from shared memory.
    // So, Alice can return any valid X, e.g., 0.
    return 0;
}

int Bob(std::string T, int X) {
    // X is the integer passed by Alice, but we're ignoring it
    // because S is expected to be in shared memory.

    // 1. Get the shared memory segment created by Alice.
    //    Do not use IPC_CREAT here, as Alice should have created it.
    //    Size can be 0 when getting an existing segment, but better to be specific or large enough.
    int shmid = shmget(SHM_KEY, T.length() + 1, 0666); // T.length() provides a hint for size
                                                     // Using T.length() because S and T are same length
    if (shmid == -1) {
        // This might happen if Alice failed or used a different key/size expectation
        // For this problem, let's try with MAX_LEN if T.length()+1 fails.
        shmid = shmget(SHM_KEY, MAX_LEN, 0666);
        if (shmid == -1) {
            shm_error("Bob: shmget failed");
            return -1; // Should not happen in a correct setup
        }
    }

    // 2. Attach the shared memory segment to Bob's address space.
    char* shm_ptr = (char*)shmat(shmid, NULL, 0); // SHM_RDONLY could be used if Bob only reads
    if (shm_ptr == (char*)-1) {
        shm_error("Bob: shmat failed");
        return -1; // Should not happen
    }

    // 3. Read string S from shared memory.
    //    The string in shared memory should be null-terminated by Alice.
    std::string S_from_shm(shm_ptr);

    int N = T.length();
    int diff_pos = 0; // 0 means S == T

    // 4. Compare S_from_shm with T.
    for (int i = 0; i < N; ++i) {
        // Defensive check in case S_from_shm is shorter than T (should not happen)
        if (i >= S_from_shm.length()) {
            // This implies an error in how S was written or shared memory was sized.
            // Or, S and T didn't have equal length as per problem constraints.
            // For this problem, we assume S_from_shm.length() >= N.
            // If S_from_shm[i] is effectively different because S is shorter.
            diff_pos = i + 1;
            break;
        }
        if (S_from_shm[i] != T[i]) {
            diff_pos = i + 1; // Problem uses 1-based indexing for position
            break; // Since there's at most one difference
        }
    }

    // 5. Detach the shared memory segment.
    if (shmdt(shm_ptr) == -1) {
        shm_error("Bob: shmdt failed");
        // Continue to try to remove shm
    }

    // 6. Mark the shared memory segment for deletion.
    //    The segment will be destroyed once the last process detaches from it.
    //    Since Bob is the last one using it in this problem flow, it should get deleted.
    if (shmctl(shmid, IPC_RMID, NULL) == -1) {
        shm_error("Bob: shmctl IPC_RMID failed");
    }

    return diff_pos;
}