L - Por Costel and the Semipalindromes Gym - 100923L

27 篇文章 0 订阅
11 篇文章 0 订阅

Por Costel the pig, our programmer in-training, has recently returned from the Petrozaporksk training camp. There, he learned a lot of things: how to boil a cob, how to scratch his belly using his keyboard, etc… He almost remembers a programming problem too:

A semipalindrome is a word for which there exists a subword such that is a prefix of and (reverse ) is a suffix of . For example, ‘ababba’ is a semipalindrom because the subword ‘ab’ is prefix of ‘ababba’ and ‘ba’ is suffix of ‘ababba’.

Let’s consider only semipalindromes that contain letters ‘a’ and ‘b’. You have to find the -th lexicographical semipalindrome of length .

Por Costel doesn’t remember if the statement was exactly like this at Petrozaporksk, but he finds this problem interesting enough and needs your help to solve it.

Input
On the first line of the file semipal.in, there is an integer () representing the number of test cases. On the next lines there are 2 numbers, ( and K where is the number of semipalindromes of length .

Output
In the output file semipal.out, there should be lines, the -th of which should contain the answer for the -th test.

Example
Input
2
5 1
5 14
Output
aaaaa
bbabb

题意:
让你求长度为n 头尾相同 第k小子序列
解; a看为0 b=1
就是从0数到k-1的二进制

#include<bits/stdc++.h>
using namespace std;
#define ll long long
/*
ll solve(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans*=a;
        a*=a;
        b>>=1;
    }
    return ans;
}
int main()
{
  //  freopen("azerah.in","r",stdin);
    freopen("azerah.out","w",stdout);
    ll t,i,j;
    cin>>t;
    {
        while(t--)
        {
            ll n,a,s1=0,s2=0;
            scanf("%lld",&n);
            for(i=0;i<n;i++)
            {
                scanf("%lld",&a);
                if(a&1) s1++;
                else s2++;
            }
            if(!s1&&s2)
                cout<<solve(2,s2)-1<<endl;
            else if(s1&&!s2)
                cout<<solve(2,s1-1)-1<<endl;
            else if(s1&&s2)
                cout<<solve(2,s1/2+s2)-1<<endl;
        }
    }
    return 0;
}*/
int main()
{
    freopen("semipal.in","r",stdin);
    freopen("semipal.out","w",stdout);
    ll t,i,j;
    while(cin>>t)
    {
        while(t--)
        {
            ll n,k;
            scanf("%lld%lld",&n,&k);
            char s[1111];
            k--;
            for(i=n-2;i>=0;i--)
            {
                if(k%2) s[i]='b';
                else s[i]='a';
                k/=2;
            }
            s[n-1]=s[0];
            for(i=0;i<n;i++)
                printf("%c",s[i]);
            puts("");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值