寒假特训——搜索——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 fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1.
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 f1.
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; }
标签:riddle,What,busy,end,双引号,doing,Will,Nephren,gives 来源: https://www.cnblogs.com/EchoZQN/p/10354669.html