-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaditya_verma_10.cpp
More file actions
64 lines (52 loc) · 1.12 KB
/
aditya_verma_10.cpp
File metadata and controls
64 lines (52 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//kth symbol in grammar--leetcode
#include <bits/stdc++.h>
using namespace std;
/* ///naive method
void push_for_one(vector<int>& v){
v.push_back(1);
v.push_back(0);
}
void push_for_zero(vector<int>& v){
v.push_back(0);
v.push_back(1);
}
void update_grammar(vector<int>& v){
if(v.size()==0) return;
int temp=v.at(v.size()-1);
v.pop_back();
update_grammar(v);
if(temp==0) push_for_zero(v);
else if(temp==1) push_for_one(v);
return;
}
void generate_grammar(vector<int>& v,int n){
if(n==0){
v.push_back(0);
return;
}
--n;
generate_grammar(v,n);
update_grammar(v);
}
int search_grammar(int n,int k){
if(n==0) return 0;
vector<int> grammar;
generate_grammar(grammar,n);
return grammar.at(k-1);
}
*/
//efficient method
int kthGrammar(int n, int k) {
if(n==1 && k==1) return 0;
int mid=pow(2,n-1);
//cout<<mid;
if(k<=mid) return kthGrammar(n-1,k);
//else return !(kthGrammar(n-1,k-mid));
else return !kthGrammar(n-1,k-mid);
}
int main() {
int n=2,k=2;
//cin>>n>>k;
cout<<kthGrammar(n,k)<<"\n";
return 0;
}