Shanghai Monthly Competition Group B 2023 January Competition: Unlimited Extension

topic

Original title link: aaa is so difficult


train of thought

Obviously, for a position p , we have a mapping relationship (because strings form a recursive relationship )

for example:

s=abcd, find 6

abcd dabc

d is in the last position of the first half string corresponding to the second half string

a is in the second position of the first half string corresponding to the second half string

We found that 6 is actually the first position of the first half string, so output s[1]='a'.

We determine the initial position by continuously narrowing the range , which belongs to the divide and conquer algorithm.


the code

#include <bits/stdc++.h>
#define int long long
using namespace std;
string s;
int m,k,siz,p,mid;
signed main()
{
  cin>>s>>m;
  k = m;
  siz = s.size();
  p = siz;
  while(p <= m) p *= 2;//是先找到第一个>m的2的次幂 p
  while(p > siz)//当p<=siz时结束
  {
    mid = p / 2;
    if(mid < k) k = k - mid - 1;
    if(k == 0) k = mid;
    p /= 2;
  }
  cout<<s[k - 1];
  return 0;
}

Guess you like

Origin blog.csdn.net/weq2011/article/details/128908888