寒假特訓——搜索——H - Nephren gives a riddle
Nephren is playing a game with little leprechauns.
She gives them an infinite array of strings, f0... ∞.
f0 is "What are you doing at the end of the world? Are you busy? Will you save us?".
She wants to let more people know about it, so she defines f
For example, f1 is
"What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f
It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces.
Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output ‘.‘ (without quotes).
Can you answer her queries?
Input
The first line contains one integer q (1?≤?q?≤?10) — the number of Nephren‘s questions.
Each of the next q lines describes Nephren‘s question and contains two integers n and k (0?≤?n?≤?105,?1?≤?k?≤?1018).
Output
One line containing q characters. The i-th character in it should be the answer for the i-th query.
Examples
Input3Output
1 1
1 2
1 111111111111
Wh.Input
5Output
0 69
1 194
1 139
0 47
1 66
abdefInput
10Output
4 1825
3 75
3 530
4 1829
4 1651
3 187
4 584
4 255
4 774
2 474
Areyoubusy
Note
For the first two examples, refer to f0 and f1 given in the legend.
題目大意:
就是給你q組數據,每一組有兩個數,一個是第n句話,一個是這個第n句話中的第k個字母,求出這個字母。
給你第0句話和第一句話,讓你進行遞歸搜索。
思路:
這個是看了很多題解,比較簡單的一種
就是無論是第幾句話,都分成五個部分,第一個就是雙引號前面的,第二個是雙引號中間的,第三個是第一個雙引號後面
第二個雙引號前面的,第四個是第二個雙引號,第五個是第二個雙引號後面的。
根據k判斷它在這五個部分中的哪一個,找到後就進行定位輸出。
具體:
先寫一個長度函數,根據n句話判斷它的長度,因為當n==55,len[n]==5e18了,所以就不需要往後算,而且也算不了。
因為再往後就超出範圍了。
為什麽不用算呢?因為這個是遞歸,而且k範圍是1e18,所以不會有k>len[55]的情況出現。所以在n>=55,k都會在第一個
輸出或者第二個部分進入下一個dfs。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> using namespace std; typedef long long ll; char f0[]="What are you doing at the end of the world? Are you busy? Will you save us?"; char f1[]="What are you doing while sending \"\"? Are you busy? Will you send \"\"?"; const int maxn=1e5+10; const ll inf=1e18; ll len[maxn]; void init() { len[0]=strlen(f0); int l=strlen(f1); for(int i=1;i<=55;i++) len[i]=2*len[i-1]+l; for(int i=56;i<=100000;i++) len[i]=len[55]; } char dfs(int n,ll k) { if(n==0) { if(k<len[0]) return f0[k]; return ‘.‘; } if(k<34) return f1[k]; k-=34; if(k<len[n-1]) return dfs(n-1,k); k-=len[n-1]; if(k<32) return f1[k+34]; k-=32; if(k<len[n-1]) return dfs(n-1,k); k-=len[n-1]; if(k<2) return f1[k+66]; return ‘.‘; } int main() { int q; scanf("%d",&q); int n; ll k; init(); while(q--) { scanf("%d%I64d",&n,&k); k--;//因為數組原因 char ans=dfs(n,k); putchar(ans); } return 0; }
寒假特訓——搜索——H - Nephren gives a riddle