Codeforces Round #629 (Div. 3) B. K-th Beautiful String(字符串构造/规律)

题目链接


在这里插入图片描述
1.题目大意:给出一个只包含两个’ b b b‘且剩余字符全为’ a a a'的字符串,求构成的所有字符串中第 k k k小的字符串

2.对于第 k k k小,我们难以下手,但是反过来想,求第 k k k大不是很好求吗,那么先初始化所有的子串,找出求第 k k k大的规律,再倒着求第 k k k小即可。规律显然是第一个’ b b b‘的位置,由 1 − n − 1 1-n-1 1n1,当第一个’ b b b‘在下标 1 1 1时,第二个’ b b b‘由大到小有四种方法。得出只要确定第 1 1 1个’ b b b‘的位置i就能得出第二个’ b b b'有 n − i n-i ni种位置,累积的就是第 k k k大,先初始化保存到数组中,最后倒着查询第 k k k小,就能得出两个 b b b的位置,就能构造出字符串了

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x7fffffff;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=3e5+10;

int f[maxn];
int n,t,k;

void init(){
    for(int i=1;i<=n;i++){
        f[i]=n-i;
    }
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>k;
        init();
        int ans=0,p=n-1;
        while(ans+f[p]<k){
            ans+=f[p--];
        }
        int x=k-ans;
        int y=n+1-x;
        for(int i=1;i<=n;i++){
            if(i==p || i==y) cout<<'b';
            else cout<<'a';
        }
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值