Skip to content

779. K-th Symbol in Grammar #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

779. K-th Symbol in Grammar #46

wants to merge 2 commits into from

Conversation

potrue
Copy link
Owner

@potrue potrue commented Aug 14, 2025

int kthGrammar(int n, int k) {
if (k == 1) return 0;
int k_parity = k & 1;
if (k_parity == 0) k_parity = 2;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この行が読みやすさに寄与しているか微妙だなと思い、取り除いてk_parity が 0 か 1 で良い気がしました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そう思います。ちょっと1-indexedにとらわれすぎました。

if (k_parity == 0) k_parity = 2;
int k_prev = k + 1 >> 1;
int prev = kthGrammar(n - 1, k_prev);
if (prev == 0 && k_parity == 1 || prev == 1 && k_parity == 2) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k_parity == 2 の時には prev とは違う文字になるの方が伝わりやすいと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

反転するという捉え方が書いているときになかったのでこんな感じになってしまいました。
ただその捉え方をするなら結局while文などで書いたほうがいい気もしますね。

if (k == 1) return 0;
int k_parity = k & 1;
if (k_parity == 0) k_parity = 2;
int k_prev = k + 1 >> 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+>> のどちらの優先度が高いか、分かりづらいように思いました。

int k_prev = (k + 1) >> 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants