codeforces896A Nephren gives a riddle (dfs)

题目链接:http://codeforces.com/problemset/problem/896/A

题意:给你一个数字n,代表f(n),然后给定数字k,在f(n)中查找第k个字符,如果k > f(n)的长度,返回‘.’;

思路:在f(n)中找到f(n-1)处,递归查找即可

#include <bits/stdc++.h>
#define LL long long
using namespace std;

char sa[] = "What are you doing at the end of the world? Are you busy? Will you save us?";
char sb[] = "What are you doing while sending \"";
char sc[] = "\"? Are you busy? Will you send \"";
char sd[] = "\"?";

int q,n,lena,lenb,lenc,lend;
LL k,x,len[110];

void init(){
    lena = strlen(sa);
    lenb = strlen(sb);
    lenc = strlen(sc);
    lend = strlen(sd);
    len[0] = lena;
    x = 0;
    while(len[x] <= 1e18){
        x ++; len[x] = lenb + len[x-1] + lenc + len[x-1] + lend;
    }
}

char dfs(int n, LL k){
    if(n <= x && k > len[n]) return '.';
    else{
        if(n == 0) return sa[k-1];
        else{
            if(k <= lenb) return sb[k-1];
            k -= lenb;
            if(n > x || k <= len[n-1]) return dfs(n-1,k);
            k -= len[n-1];
            if(k <= lenc) return sc[k - 1];
            k -= lenc;
            if(n > x || k <= len[n-1]) return dfs(n-1,k);
            k -= len[n-1];
            return sd[k - 1];
        }
    }
}

int main(){
    init();
    scanf("%d",&q);
    while(q --){
        scanf("%d%I64d",&n,&k);
        printf("%c",dfs(n,k));
    }
    printf("\n");
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值