In c++
Tạo vào: 14 tháng 3, 2026
Trả lời bằng GPT-5.4 Thinking bởi Chat01
Tạo vào: 14 tháng 3, 2026
Trả lời bằng GPT-5.4 Thinking bởi Chat01
In c++
Use this C++ solution. It runs in O(n).
cpp#include <string> using namespace std; string circularCircuit(string input1) { int n = (int)input1.size(); // For n = 1 or 2, both neighbors are effectively the same LED, // so no bit changes. Previous day must be the same string. if (n <= 2) return input1; string answer = ""; int countValid = 0; for (int first = 0; first <= 1; first++) { for (int second = 0; second <= 1; second++) { string prev(n, '0'); prev[0] = char('0' + first); prev[1] = char('0' + second); // From: // current[i] = prev[i-1] XOR prev[i] XOR prev[i+1] // so: // prev[i+1] = current[i] XOR prev[i] XOR prev[i-1] for (int i = 1; i <= n - 2; i++) { int nextBit = (input1[i] - '0') ^ (prev[i] - '0') ^ (prev[i - 1] - '0'); prev[i + 1] = char('0' + nextBit); } // Check circular conditions for i = 0 and i = n-1 int checkFirst = (prev[n - 1] - '0') ^ (prev[0] - '0') ^ (prev[1] - '0'); int checkLast = (prev[n - 2] - '0') ^ (prev[n - 1] - '0') ^ (prev[0] - '0'); if (checkFirst == (input1[0] - '0') && checkLast == (input1[n - 1] - '0')) { countValid++; if (countValid == 1) answer = prev; else return "Multiple scenario"; // change to "Multiple Scenario" if portal expects that exact case } } } if (countValid == 0) return "No Chance"; return answer; }
If prev is the previous day and cur is the current day, then for each position:
So:
cppcur[i] = prev[i] XOR prev[left] XOR prev[right]
If we guess the first two bits of the previous string, every other bit becomes fixed.
There are only 4 possible guesses for the first two bits, so we try all 4 and count how many valid previous strings exist:
0 valid → "No Chance"1 valid → return that string>1 valid → "Multiple scenario"For the screenshot template, paste only the function body code above.
One small note: the sample in the image has inconsistent capitalization for "Multiple scenario" vs "Multiple Scenario". If the judge is strict, use the exact version its sample/output checker expects.