题目链接: https://hihocoder.com/contest/offers53/problem/2
解题思路: 根据每代字符串的生成规则,可以看出,第i代的字符串的第k个位置的值,必然有i-1代字符串第k/2个位置产生,于是递归。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 char dfs(long long k) 5 { 6 if (k == 1LL) 7 { 8 return 'h'; 9 } 10 if (k == 2LL) 11 { 12 return 'i'; 13 } 14 char tmp = dfs((k+1)/2); 15 long long flags = k&1; 16 if (tmp =='h') 17 { 18 if (flags) 19 { 20 return 'h'; 21 } 22 else 23 { 24 return 'i'; 25 } 26 } 27 else if (tmp == 'i') 28 { 29 if (flags) return 'i'; 30 else return 'o'; 31 } 32 else if (tmp == 'o') 33 { 34 if (flags) return 'o'; 35 else return 'h'; 36 } 37 } 38 39 int main() 40 { 41 // for (int i = 1; i <= 20; ++i) 42 // printf("%c", dfs(i)); 43 // printf(" "); 44 int Case; 45 scanf("%d", &Case); 46 while (Case--) 47 { 48 long long k; 49 scanf("%lld", &k); 50 printf("%c ", dfs(k)); 51 } 52 return 0; 53 }