Codeforces:Nephren gives a riddle

time limit per test: 2 seconds memory limit per test: 256 megabytes
input: standard input output: standard output

–What are you doing at the end of the world?
–Are you busy? Will you save us?


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
Input
31 11 21 111111111111

Output
Wh.

Input
50 691 1941 1390 471 66

Output
abdef

Input
104 18253 753 5304 18294 16513 1874 5844 2554 7742 474

Output
Areyoubusy

Note

For the first two examples, refer to f0 and f1 given in the legend.

·>原题

英语奇差的我一开始并没有明白题意。看懂之后也并没有能够在训练中做出来,训练结束后立刻修改了一点小错误就过了。

首先用本人很菜的英语水平对题目意思进行翻译:奈芙莲(Nephren) 给出了一个从 f0f 无限的字符串数组 ;

其中 f0 表示{What are you doing at the end of the world? Are you busy? Will you save us?}的字符串;(大括号内的部分表示字符串,不包括大括号)

而对于 fi 定义为 {What are you doing while sending “fi-1”? Are you busy? Will you send “fi-1”?}(i>=1)

比如 f1 表示为 {What are you doing while sending “f0”? Are you busy? Will you send “f0”?}

f1 = {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?”?} (其中黑体字表示 f0 )

fi 中的字符包括字母、问号、引号和空格。

奈芙莲将询问q次,每次都会要求找寻 n = i 个的 fn 的第 k 个字符。fn 开头由1数起,如果 fn 的字符数量少于 k ,则输出 “.” (没有引号);

输入
第一行为 q (1 ≤ q ≤ 10),表示有多少个提问;
接下来 q 行输入 n 和 k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018);

输出
一行包含 q 个字符的字符串,其中第 i 个字符对应第 i 个查询;

分析:

根据题目的 fi 的递推式,除了两部分 fi-1 的之外,还有三段字符串;分别是
前段 {What are you doing while sending “};
中段 {”? Are you busy? Will you send “};
后段 {”?};

如图所示结构

fi
前段
fi-1
中段
fi-1
后段
前段
fi-2
中段
fi-2
后段
前段
fi-2
中段
fi-2
后段

很容易会想到用DFS;
通过判断 k 落在五个部分的哪个部分,如果落在 fi-1 则递归判断,如果是在其他三个部分则返回对应的字符。如果 k 超出了范围则返回 “.”;

在递归时要稍微处理一下数据。判断落在什么区间需要用到对应的 fn 的长度,因为 fn 实在是太大了,n不到100而字符串长度就已经远远超过了long long表示的64位整型的范围(n<=10000),注意到k<=1018,那超过这个长度的 fn 及其以后的 fn 可以用一个设定的无穷大(如4e18,大于k)来处理判断 k 落在对应什么部分;

即预先保存四段字符后,得到各字符串的长度后进行各 fn 长度的预处理,实际上 q 只有10组的规模下是否预处理估计差别不大;

虽然能跑,但代码写得太难看了,有很多很糟糕的地方;
仍需要继续努力。

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
typedef long long LL;
const LL INF=4e18;
using namespace std;
char f0[]={"What are you doing at the end of the world? Are you busy? Will you save us?"};
char ff[]={"What are you doing while sending \""};
char fm[]={"\"? Are you busy? Will you send \""};
char fb[]={"\"?"};
LL n,k,flen[100005],sn[11],sk[11];
int l0,lf,lm,lb,q;
void pre(){flen[0]=l0;for(int i=1;i<100005;i++){if(flen[i-1]>1e18) flen[i]=INF;elseflen[i]=lf+2*flen[i-1]+lm+lb;}
}char dfs(LL a,LL ft){      //这里我用ft来保存当前区间前面有多长的字符LL p=k-ft;                          if(a==0) return p<=l0?f0[p-1]:'.';  //需要判断n为0时k的情况if(p<=lf) return ff[p-1];             //前段else if(lf<p && p<=lf+flen[a-1])            //fi-1return dfs(a-1,ft+lf);else if(lf+flen[a-1]<p && p<=lf+flen[a-1]+lm) //中段return fm[p-flen[a-1]-lf-1];else if(lf+flen[a-1]+lm<p && p<=lf+2*flen[a-1]+lm)  //fi-1return dfs(a-1,ft+lf+flen[a-1]+lm);else if(lf+2*flen[a-1]+lm<p && p<=lf+2*flen[a-1]+lm+lb) //后段return fb[p-lf-2*flen[a-1]-lm-1];else return '.';                 //此处判断不包括n为0
}int main(){l0=strlen(f0);lf=strlen(ff);lm=strlen(fm);lb=strlen(fb);pre();scanf("%d",&q);for(int i=0;i<q;i++){scanf("%d%I64d",&sn[i],&sk[i]);}for(int i=0;i<q;i++){n=sn[i],k=sk[i];printf("%c",dfs(n,0));}puts("");return 0;
}

Codeforces 897C Nephren gives a riddle(DFS)相关推荐

  1. Codeforces 897C Nephren gives a riddle:模拟【珂学】

    题目链接:http://codeforces.com/contest/897/problem/C 题意: 给你一些字符串: A: [What are you doing at the end of t ...

  2. 【Codeforces 723D】Lakes in Berland (dfs)

    海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...

  3. Codeforces 982 C. Cut 'em all!(dfs)

    解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...

  4. CodeForces 6D Lizards and Basements 2 (dfs)

    题意:给出一串n个元素序列.a和b,只能选择编号2 ~ n-1的s数字减a,并将相邻两数字减b,要使得所有元素为负,问至少需要多少次选择,选择是怎样的. 题解:dfs 我们可以发现只有2 ~ n-1编 ...

  5. CodeForces 6D Lizards and Basements 2(DFS)

    题意:有一串数字,每一次你可以使一个数字减少a,使相邻两个数字减少b,只能操作2-n-1次 思路:直接暴力DFS一波... #include<bits/stdc++.h> using na ...

  6. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)

    一.图的基本介绍 为什么要有图 前面我们学了线性表和树 线性表局限于一个直接前驱和一个直接后继的关系 树也只能有一个直接前驱也就是父节点 当我们需要表示多对多的关系时, 这里我们就用到了图. 图的举例 ...

  7. 【 MATLAB 】离散傅里叶级数(DFS)及 IDFS 的 MATLAB 实现

    有关离散傅里叶级数(DFS)我之前也写过一些博文,例如:离散周期信号的傅里叶级数(DFS) 这里我再次给出标准公式. 分析式: 其中: 综合式: 这里我必须先声明,关于分析式和综合式前面那个系数1/N ...

  8. 部署分布式文件系统(DFS)

    部署分布式文件系统(DFS) 使用 DFS 命名空间,可以将位于不同服务器上的共享文件夹组合到一个或多个逻辑结构的命名空间.每个命名空间作为具有一系列子文件夹的单个共享文件夹显示给用户.但是,命名空间 ...

  9. Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)

    对算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)用Java实现其中的伪代码算法,案例也采用算法导论中的图. import java.util.ArrayList; import java ...

最新文章

  1. mysql数据库插入图片_向MySql数据库插入与读取图片文件
  2. 游戏在ios和android,陈情令手游ios和安卓互通吗 ios和安卓能一起玩吗
  3. 计算机文档xsl,XSL-FO 文档
  4. php 文件加载方式
  5. pythonsearch结果_python 查询Elasticsearch的小例子
  6. Vue.js的虚拟dom
  7. php循环 跳转语句,golang循环跳转语句
  8. java----动态绑定
  9. 开课吧:Dubbo的整体架构设计有哪些分层?
  10. 国内域名商.wang总量TOP14统计报告(6月9日)
  11. python基础:pip和虚拟环境
  12. mysql数据库root密码在哪个文件中_mysql数据库的root密码放在什么位置?
  13. 在iOS上使用AirPrint实现无线打印功能
  14. Struts的增删改查
  15. OBJ(3D模型)文件格式
  16. 基于数码相机拍照图像分析的植被覆盖率(FVC)计算软件人品大家自己斟酌
  17. 球球大作战JAVA小游戏
  18. 油猴插件安装以及好用的脚本推荐
  19. window.onload用法
  20. scp 命令简明介绍

热门文章

  1. 笔记本html外接显示器,笔记本电脑外接显示器怎么设置?笔记本电脑接显示器实现双屏教程...
  2. 【算法】leetcode-838 推多米诺
  3. 我优化了李笑来的MarkdownHere,附css样式代码,文章排版再也不用愁了
  4. Paypal国际版网站集成简易教程
  5. 如何使用阿里云国际版控制台使用海外云服务器-Unirech
  6. 三、自定义Abp Vnext框架代码生成模板
  7. COGS 336 Vijos 1018 NOI2003 智破连环阵
  8. XXE(外部实体注入)| PortSwigger(burpsuite官方靶场)| Part 3
  9. html怎样设置图片的圆角矩形,css怎么画圆角矩形?
  10. ps怎么设计html界面,APP展示图,教你怎么用PS制作APP的界面图片