Files
python/TangDou/LuoGuBook/P3612.cpp

38 lines
884 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
//原始字符串
string s;
//预求的位置值
LL x;
//在n个长度的字符串中,查找位置x的字符
char dfs(LL n, LL x) {
//如果回到原始串,那么直接返回指定位置的字符
if (n == s.size()) return s[x - 1];//之所以x-1,是因为字符串下标是从0开始的
//字符串的中间点
LL mid = n >> 1;
//如果在后半部分(转化为前半部分的位置)
if (x > mid)
return dfs(mid, (x == mid + 1 ? mid : x - mid - 1));
//如果在前半部分
else
return dfs(mid, x);
}
int main() {
cin >> s >> x;
//原始长度
LL n = s.size();
//不断乘2,翻倍,找出能够容纳x这个数字的最短长度
while (n < x) n <<= 1;
//递归
cout << dfs(n, x);
}