Codeforces 897C Nephren gives a riddle(dfs)

来源:互联网 发布:youtube无网络连接ios 编辑:程序博客网 时间:2024/04/28 19:05

题目Codeforces 897C


题意:给定构造字串 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"?" 输入两个数字n和k(0 ≤ n ≤ 1e5, 1 ≤ k ≤ 1e18),问fn的第k个字符是什么,若长度不足则输出“.”。


思路:递归解决。先预处理1e18内fn的长度len[n],若k>len[n]则直接输出“.”。然后分别对每一段长度比较进行解决。注意当n≥x,即字串的长度大于1e18时,因为k最大为1e18,故所查字符必定在fn-1当中,直接调用即可。

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <cmath>#include <vector>#include <iostream>#include <stack>#include <set>#include <map>#include <string> using namespace std;char s0[]="What are you doing at the end of the world? Are you busy? Will you save us?";char s1[]="What are you doing while sending \"";char s2[]="\"? Are you busy? Will you send \"";char s3[]="\"?";int q,x;long long len[1000];void init(void){x=0;len[0]=strlen(s0);while(len[x++]<=1e18){len[x]=strlen(s1)+len[x-1]+strlen(s2)+len[x-1]+strlen(s3);}}char solve(int n,long long k){if(n<x&&k>len[n])    return '.';if(n==0)        return s0[k-1];if(k<=strlen(s1))    return s1[k-1];k-=strlen(s1);if(n>=x||k<=len[n-1])    return solve(n-1,k);k-=len[n-1];if(k<=strlen(s2))    return s2[k-1];k-=strlen(s2);if(k<=len[n-1])    return solve(n-1,k);k-=len[n-1];return s3[k-1];}int main(){int n;long long k;scanf("%d",&q);init();while(q--){scanf("%d%lld",&n,&k);printf("%c",solve(n,k));}putchar('\n');return 0;}


原创粉丝点击