CF1328B K-th Beautiful String,然而CF今天却上不去了,这是洛谷的链接

题意

一个长度为\(n\)的字符串,有2个\(\texttt{b}\)和\(n-2\)个\(\texttt{a}\)

按字典序排序后,问你第\(k\)个是啥

\(n\leq 10^5\)


由于我们相信CF从来不卡常,所以这实际是个\(O(n)\)找规律

看题目里的例子:

aaabb
aabab
aabba
abaab
ababa
abbaa
baaab
baaba
babaa
bbaaa

可以发现,如果只看前面一个\(\texttt{b}\)的运动轨迹,是从后向前以为一位移动的

而对于靠后的一个\(\texttt{b}\),当前面那个\(\texttt{b}\)确定后,它也是从最后一位向前移动,直到移动到前面那个\(\texttt{b}\)的后一位

然后,它又回到最后,前面那个\(\texttt{b}\)再往前走一位

所以我们可以从后到前枚举第一个\(\texttt{b}\)的位置,假设当前位置是\(i\),那么这一种情况就有\(n-i\)个字符串

讨论如下:

  • \(k>n-i\),那么,\(k\rightarrow k-(n-i)\),就是说这\(n-i\)个字符串里面没有要找的,再向前考虑
  • 其余情况,也就是我们要找的第\(k\)个字符串就在这\(n-i\)个,那么第二个\(\texttt{b}\)就是在第\(n-k+1\)为,输出就行

可以结合上面的例子理解

放上代码

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define reg register
#define EN std::puts("")
#define LL long long
inline LL read(){
LL x=0,y=1;
char c=std::getchar();
while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
return y?x:-x;
}
LL n,k;
int main(){int T=read();while(T--){
n=read();k=read();
for(reg int i=n-1;i;i--){
if(k>n-i) k-=n-i;
else{
for(reg int j=1;j<i;j++) std::putchar('a');
std::putchar('b');
for(reg int j=i+1;j<n-k+1;j++) std::putchar('a');
std::putchar('b');
for(reg int j=n-k+2;j<=n;j++) std::putchar('a');
break;
}
}
EN;
}
return 0;
}

CF1328B K-th Beautiful String的更多相关文章

  1. Codeforces Round #604 (Div. 2) A. Beautiful String

    链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...

  2. hiho一下:Beautiful String

    hiho一下:Beautiful String 记不清这是 hiho一下第几周的题目了,题目不难,不过对于练习编程,训练思维很有帮助.况且当时笔者处于学习算法的早期, 所以也希望刚接触算法的同学能多去 ...

  3. K - Count the string kmp_Next数组应用

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  4. hihocoder 1061.Beautiful String

    题目链接:http://hihocoder.com/problemset/problem/1061 题目意思:给出一个不超过10MB长度的字符串,判断是否里面含有一个beautiful strings ...

  5. Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...

  6. HackerRank beautiful string

    问题 https://vjudge.net/problem/HackerRank-beautiful-string 给一个字符串S,可以任意取走S中的两个字符从而得到另外一个字符串P,求有多少种不同的 ...

  7. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  8. Leetcode: Rearrange String k Distance Apart

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  9. [Swift]LeetCode358. 按距离为k隔离重排字符串 $ Rearrange String k Distance Apart

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

随机推荐

  1. Typora+PicGo+GitHub实现md自带图床效果

    1 GitHub创建作为图床的仓库 1.1 在GitHub中创建一个仓库 注意仓库要是public的,不然上传的图片还是无法使用的.如果不知道怎么创建仓库,可以百度一下. 1.2 在GitHub生成一 ...

  2. OS-DOS/CMD/Windows/各类软件快捷键等使用总结

    一.快捷键 很多软件的快捷键使用相通,在不确定的情况下,先试试其他软件的快捷键的使用方法 Windows电脑快捷键 HP惠普笔记本 win+E 打开文件管器 win+D 显示桌面 win+L 锁计算机 ...

  3. 小程序wepy2 模拟vant PasswordInput, NumberKeyboard 密码输入框控件

    vant weapp小程序端控件目前是没有PasswordInput,NumberKeyboard的.实现效果: 数字键盘组件代码(keyboard.wpy): <template> &l ...

  4. tf.nn.sigmoid_cross_entropy_with_logits 分类

    tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,,labels=None,logits=None,name=None) logits和la ...

  5. Linux知识再回顾

    Linux再回顾 下面是自己之前centos7的笔记总结第二篇,第一篇是19年就写过了一些,记住Linux中一切皆文件. 这里提下,使用xshell+xftp来使用云服务器是很不错的,强烈建议小伙伴这 ...

  6. vue的组件缓存(返回页面不刷新)

    每次使用返回是页面总是会刷新 导致了一些体验上的不愉快 现在 发现vue中的一个很方便的方法还可以用来优化性能就是: keep-alive缓存组件 <router-view v-if=" ...

  7. 【乱码问题】IDEA控制台使用了GBK字符集

    什么Tomcat乱码设置IDEA的初始编码,瞎搞 终于在这个帖子看到了真相 https://blog.csdn.net/weixin_42617398/article/details/81806438 ...

  8. Jenkins 批量创建任务的三种方法

    最近,要搭建多套测试环境,需要把 Jenkins 中 dev 视图下的所有任务批量复制到 sit 等视图下. 说明 Jenkins 任务名称规则为:[测试环境标识]-[工程名称],如:dev-daod ...

  9. vue中的ref属性

    1.什么是ref? ref是用于快速定位到dom结构,vue中一般不去操作dom结构,他是数据驱动的.jQuery会疯狂操作DOM {{msg}} mounted(){ let h = this.$r ...

  10. Linux学习笔记(五)搜索命令

    搜索命令 whereis which locate find whereis 英文原意:locate the binary,source,and manual page files for a com ...