标签:int Semipalindromes Gym Por cin else problem include Costel
链接:
https://vjudge.net/problem/Gym-100923L
题意:
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.
思路:
考虑首字母等于末字母,中间按字典序处理即可,字典序处理可用二进制的思想.
aaa = 000, aab = 001, aba == 010, 就是挨个进位,注意,第k小在字典序的二进制中是k-1, 因为aaa = 000,从0开始.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
map<string, double> Sc;
const int MAXN = 1e3+10;
char s[100];
LL n, k;
//aaaa, aaba, abaa, abba, baba,
int main()
{
freopen("semipal.in", "r", stdin);
freopen("semipal.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n >> k;
if (k > 1LL<<(n-2))
{
k -= 1LL<<(n-2);
k--;
s[0] = 'b';
s[n-1] = 'b';
for (int i = n-2;i >= 1;i--)
{
if (k%2 == 0)
s[i] = 'a';
else
s[i] = 'b';
k /= 2;
}
}
else
{
s[0] = 'a';
s[n-1] = 'a';
k--;
for (int i = n-2;i >= 1;i--)
{
if (k%2 == 0)
s[i] = 'a';
else
s[i] = 'b';
k /= 2;
}
}
s[n] = 0;
cout << s << endl;
}
return 0;
}
标签:int,Semipalindromes,Gym,Por,cin,else,problem,include,Costel 来源: https://www.cnblogs.com/YDDDD/p/11370986.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。