链接:

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;
}

最新文章

  1. 显示python已安装模块及路径,添加修改模块搜索路径
  2. 你真的了解UIApplication吗?
  3. [团队项目]Github生成燃尽图的方式
  4. Material Design Lite,简洁惊艳的前端工具箱 之 布局组件。
  5. Jquery操作Cookie取值错误的解决方法
  6. FatMouse' Trade
  7. 【Shell脚本】运行shell脚本文件的几种方法与区别
  8. STM8单片机启动流程彻底探究--基于IAR开发环境
  9. Nginx教程(一) Nginx入门教程
  10. PAT乙级-1057. 数零壹(20)
  11. Node.js系列文章:利用console输出日志文件
  12. 【翻译】在Ext JS应用程序中使用自定义图标
  13. redis 初步认识三(设置登录密码)
  14. 个人git链接和git学习心得总结
  15. Nutch的nutch-default.xml和regex-urlfilter.txt的中文解释
  16. c# winform 自动升级
  17. 参照示例搭建一个Quertz + Topshelf的一个作业调度服务(基础)
  18. ndarray
  19. Cocos2d-x 3.0final 终结者系列教程10-画图节点Node中的Action
  20. Vue.js学习(常用指令)

热门文章

  1. centos7.5 升级kernel内核版本
  2. js获取当天时间,7天前后时间,时间格式化
  3. LeetCode.1013-分割数组为三个和相同的部分
  4. Maven - 配置setting.xml
  5. 洛谷 P3834 卢卡斯定理 题解
  6. Python和mysql的连接
  7. Ajax提交数据后,清空form表单
  8. sql server 符号函数sign
  9. 【三】Django模版的使用
  10. ALV打印不显示打印界面的问题