You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
884 B

2 years ago
#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);
}