#include <bits/stdc++.h>
#include <unistd.h> // pipe, fork, read, write, close, _exit (POSIX)
#include <sys/wait.h> // waitpid
#include <dirent.h> // For opendir, readdir, closedir
#include <fcntl.h> // For fcntl (though not directly used in example below)
int Alice(std::string S);
int Bob(std::string T, int X);
// 辅助函数:向管道安全地写入数据
bool write_to_pipe(int fd, const void* data, size_t size) {
size_t total_written = 0;
const char* buffer = static_cast<const char*>(data);
while (total_written < size) {
ssize_t written = write(fd, buffer + total_written, size - total_written);
if (written < 0) {
// perror("write to pipe"); // 子进程中不建议直接输出到 stderr 干扰评测机
return false;
}
// write 返回 0 通常意味着 count 参数为 0,或者对于某些类型的文件描述符是EOF,
// 但对于管道,如果 count > 0,返回 0 是很罕见的,通常表示错误。
// 然而,如果写入 0 字节是成功的,下面的检查会错误地将其视为失败。
// 对于阻塞管道和 size > 0,write 不应返回 0 除非发生错误(此时应返回 -1)。
// 因此,主要依赖 written < 0 来捕获错误。
// if (written == 0 && (size - total_written) > 0) { // 严格来说,如果 size > 0,write 不应返回 0
// return false;
// }
total_written += written;
}
return true;
}
// 辅助函数:从管道安全地读取数据
bool read_from_pipe(int fd, void* data, size_t size) {
size_t total_read = 0;
char* buffer = static_cast<char*>(data);
while (total_read < size) {
ssize_t bytes = read(fd, buffer + total_read, size - total_read);
if (bytes < 0) {
// perror("read from pipe");
return false;
}
if (bytes == 0) { // EOF (管道另一端已关闭且所有数据已读取)
return false; // 在读取完期望的字节数之前遇到了EOF
}
total_read += bytes;
}
return true;
}
int main() {
std::string s_str, t_str;
if (!(std::cin >> s_str >> t_str)) {
std::cout << "Failed" << std::endl;
return 0;
}
if (s_str.length() != t_str.length()) {
std::cout << "Failed" << std::endl;
return 0;
}
if (s_str.empty()) {
std::cout << "Failed" << std::endl;
return 0;
}
int n = s_str.length();
if (n > 1000000) {
std::cout << "Failed" << std::endl;
return 0;
}
for(char ch : s_str) if(ch != '0' && ch != '1') {
std::cout << "Failed" << std::endl;
return 0;
}
for(char ch : t_str) if(ch != '0' && ch != '1') {
std::cout << "Failed" << std::endl;
return 0;
}
// 检查输入是否满足“至多一个位置不同”的约束 (这更多是测试数据的有效性检查)
int diff_count = 0;
for (int i = 0; i < n; ++i) {
if (s_str[i] != t_str[i]) {
diff_count++;
}
}
if (diff_count > 1) {
std::cout << "Failed" << std::endl;
return 0;
}
int alice_val_x = -1;
int pipe_A_to_P[2]; // 管道:Alice -> Parent
if (pipe(pipe_A_to_P) == -1) {
perror("Grader Error creating pipe for Alice"); // 这个错误更可能是评分器本身的问题
std::cout << "Failed" << std::endl; // 但按题目要求,交互错误也输出WA
return 0;
}
pid_t pid_A = fork();
if (pid_A == -1) {
perror("Grader Error fork for Alice failed");
std::cout << "Failed" << std::endl;
close(pipe_A_to_P[0]);
close(pipe_A_to_P[1]);
return 0;
}
if (pid_A == 0) { // Alice 子进程
close(pipe_A_to_P[0]); // Alice 只写入
int result_x = Alice(s_str); // 调用用户的 Alice 函数
if (!write_to_pipe(pipe_A_to_P[1], &result_x, sizeof(result_x))) {
// 写入管道错误
close(pipe_A_to_P[1]);
_exit(EXIT_FAILURE); // 向父进程指示错误 (使用 _exit 避免刷新 stdio 缓冲区)
}
close(pipe_A_to_P[1]);
_exit(EXIT_SUCCESS);
} else { // 父进程
close(pipe_A_to_P[1]); // 父进程只从 Alice 读取
int status_A;
waitpid(pid_A, &status_A, 0); // 等待 Alice 进程结束
bool alice_comm_ok = true;
if (WIFEXITED(status_A)) { // Alice 进程正常退出
if (WEXITSTATUS(status_A) != EXIT_SUCCESS) { // Alice 返回了非0退出码
alice_comm_ok = false;
} else { // Alice 返回0,尝试读取数据
if (!read_from_pipe(pipe_A_to_P[0], &alice_val_x, sizeof(alice_val_x))) {
alice_comm_ok = false; // 从管道读取数据失败
}
}
} else { // Alice 进程异常终止 (例如,崩溃)
alice_comm_ok = false;
}
close(pipe_A_to_P[0]); // 关闭读端
if (!alice_comm_ok) {
std::cout << "Failed" << std::endl;
return 0;
}
// 验证 Alice 的返回值 X
if (alice_val_x < 0 || alice_val_x >= (1 << 20)) {
std::cout << "WrongAnswer" << std::endl;
return 0;
}
}
// Alice 执行完毕且返回值X有效,现在运行 Bob 进程
int bob_val_p = -1;
int pipe_B_to_P[2]; // 管道:Bob -> Parent
if (pipe(pipe_B_to_P) == -1) {
perror("Grader Error creating pipe for Bob");
std::cout << "Failed" << std::endl;
return 0;
}
pid_t pid_B = fork();
if (pid_B == -1) {
perror("Grader Error fork for Bob failed");
std::cout << "Failed" << std::endl;
close(pipe_B_to_P[0]);
close(pipe_B_to_P[1]);
return 0;
}
if (pid_B == 0) { // Bob 子进程
close(pipe_B_to_P[0]); // Bob 只写入
int result_p = Bob(t_str, alice_val_x); // 调用用户的 Bob 函数
if (!write_to_pipe(pipe_B_to_P[1], &result_p, sizeof(result_p))) {
close(pipe_B_to_P[1]);
_exit(EXIT_FAILURE);
}
close(pipe_B_to_P[1]);
_exit(EXIT_SUCCESS);
} else { // 父进程
close(pipe_B_to_P[1]); // 父进程只从 Bob 读取
int status_B;
waitpid(pid_B, &status_B, 0); // 等待 Bob 进程结束
bool bob_comm_ok = true;
if (WIFEXITED(status_B)) {
if (WEXITSTATUS(status_B) != EXIT_SUCCESS) {
bob_comm_ok = false;
} else {
if (!read_from_pipe(pipe_B_to_P[0], &bob_val_p, sizeof(bob_val_p))) {
bob_comm_ok = false;
}
}
} else {
bob_comm_ok = false;
}
close(pipe_B_to_P[0]);
if (!bob_comm_ok) {
std::cout << "Failed" << std::endl;
return 0;
}
// 验证 Bob 的返回值 P
if (bob_val_p < 0 || bob_val_p > n) {
std::cout << "WrongAnswer" << std::endl;
return 0;
}
// 最终检查
if ((s_str == t_str && bob_val_p != 0) || (s_str != t_str && s_str[bob_val_p - 1] == t_str[bob_val_p - 1])) {
std::cout << "WrongAnswer" << std::endl;
return 0;
}
// 如果所有检查都通过
std::cout << "Accepted" << std::endl;
}
return 0;
}剪贴板
剪贴板 4da1no0o
剪贴板信息
剪贴板信息
- 收录
- 更新