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. MTK Android Driver :Audio音频参数

    音频参数集成(具体参数由硬件提供) 1.ring/key/mic/fmr/speech/sidetone/media/matv增益: \mediatek\custom\k9\cgen\inc\audi ...

  2. 操作系统-2-存储管理之LRU页面置换算法(LeetCode146)

    LRU缓存机制 题目:运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制. 它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...

  3. MySQL学习之路6-数据表连接方式

    内连接 关键字: inner join  on 语句:select * from <a_table> inner join <b_table> on a.id = b.id ; ...

  4. Java Array 常见报错ArrayIndexOutOfBoundsException NullPointerException

    本文介绍array报错, 数组索引越界异常: ArrayIndexOutOfBoundsException, 空指针:NullPointerException package myArray; /* ...

  5. 列表推导式和seed()的理解

    Table of Contents generated with DocToc 列表推导式和seed()的理解 对seed()的理解 列表推导式 第一种用法 第二种用法 列表推导式和seed()的理解 ...

  6. iOS线程数量监控工具

    简单却强大的线程监控工具 KKThreadMonitor :当线程过多或瞬间创建大量子线程(线程爆炸),控制台就打印出所有的线程堆栈.便于分析造成子线程过多或线程爆炸的原因. /******* 线程爆 ...

  7. AJ学IOS(16)UI之XIB自定义Cell实现团购UI

    AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...

  8. 三个步骤就能让你轻松掌握Python爬虫

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:NicePython PS:如有需要Python学习资料的小伙伴可以加 ...

  9. Product Owner交流记录1

    Abstract 最终我们选择了UWP版必应词典功能开发. 项目:“单词挑战”功能 然后我们今天中午我们和Product owner聊了聊. Content Product owner是Travis ...

  10. 机器学习新手项目之N-gram分词

    概述 对机器学习感兴趣的小伙伴,可以借助python,实现一个N-gram分词中的Unigram和Bigram分词器,来进行入门, github地址 此项目并将前向最大切词FMM和后向最大切词的结果作 ...