c++
創建於:2026年3月15日
使用 GPT-5.4 Thinking 回答 Chat01
創建於:2026年3月15日
使用 GPT-5.4 Thinking 回答 Chat01
c++
Use this C++ function:
cpp#include <string> using namespace std; string circularCircuit(string input1) { string s = input1; int n = (int)s.size(); // For 1 or 2 LEDs, both neighbors are effectively the same LED, // so no change happens. if (n <= 2) return s; int cnt = 0; string ans = ""; // Try all possibilities for the first two bits of the previous day for (char b0 = '0'; b0 <= '1'; ++b0) { for (char b1 = '0'; b1 <= '1'; ++b1) { string prev(n, '0'); prev[0] = b0; prev[1] = b1; // current[i] = prev[i-1] XOR prev[i] XOR prev[i+1] // => prev[i+1] = current[i] XOR prev[i] XOR prev[i-1] for (int i = 1; i <= n - 2; ++i) { int bit = (s[i] - '0') ^ (prev[i] - '0') ^ (prev[i - 1] - '0'); prev[i + 1] = char('0' + bit); } // Check circular conditions bool ok1 = (((prev[n - 1] - '0') ^ (prev[0] - '0') ^ (prev[1] - '0')) == (s[0] - '0')); bool ok2 = (((prev[n - 2] - '0') ^ (prev[n - 1] - '0') ^ (prev[0] - '0')) == (s[n - 1] - '0')); if (ok1 && ok2) { cnt++; if (cnt == 1) { ans = prev; } else { return "Multiple scenario"; } } } } if (cnt == 0) return "No Chance"; return ans; }
The key relation is:
cppcurrent[i] = previous[left] XOR previous[i] XOR previous[right]
So we guess the first two bits of the previous string, reconstruct the rest, and then verify the circular ends.
If your judge expects a different capitalization like "Multiple Scenario", change that return string exactly.