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 
#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;
}


你可能感兴趣的:(搜索,CodeForces)