질문

IMG-20260314-WA0037.jpeg
IMG-20260314-WA0039.jpeg
IMG-20260314-WA0040.jpeg
IMG-20260314-WA0041.jpeg

In c++

사고

Verifying transformation rules for 110

I’m questioning whether the example is wrong or incomplete. According to the rule, 110 can indeed transform to 000, as it satisfies the condition where neighbors differ. The example omitted 110, which is why this confusion arises. It seems like the system might have been updated from left to right, not simultaneously, leading to the discrepancy. I'll check further.

Thought for 1m 42s

답변

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

Why this works

If prev is the previous day and cur is the current day, then for each position:

  • the bit flips only when left and right neighbors are different
  • that is exactly XOR of neighbors

So:

cpp
cur[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.

이 Q&A 공유하기