CF1328B K-th Beautiful String
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的更多相关文章
- Codeforces Round #604 (Div. 2) A. Beautiful String
链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...
- hiho一下:Beautiful String
hiho一下:Beautiful String 记不清这是 hiho一下第几周的题目了,题目不难,不过对于练习编程,训练思维很有帮助.况且当时笔者处于学习算法的早期, 所以也希望刚接触算法的同学能多去 ...
- 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 ...
- hihocoder 1061.Beautiful String
题目链接:http://hihocoder.com/problemset/problem/1061 题目意思:给出一个不超过10MB长度的字符串,判断是否里面含有一个beautiful strings ...
- Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...
- HackerRank beautiful string
问题 https://vjudge.net/problem/HackerRank-beautiful-string 给一个字符串S,可以任意取走S中的两个字符从而得到另外一个字符串P,求有多少种不同的 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- MongoDB查询mgov2的聚合方法
1.多条表数据累计相加. respCount := struct { Rebatescore int64 //变量命名必须要和查询的参数一样.}{} o := bson.M{"$match& ...
- C语言 文件操作(八)
1.删除文件或目录 int remove(char * filename); [参数]filename为要删除的文件名,可以为一目录.如果参数filename 为一文件,则调用unlink()处理:若 ...
- 多级菜单初写(dict使用)
#!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyumap = { "中国":{ "北京":{ &qu ...
- AJ学IOS 之小知识iOS启动动画_Launch Screen的运用
AJ 分享,必须精品 看下效果吧 例如新浪微博的软件开启时候 就是这个 用Launch image实现 这个不难,就是在Images.xcassets 增加一个LaunchImage文件(右键 new ...
- 【简单了解系列】从基础的使用来深挖HashMap
HashMap定义 说的专业一点,HashMap是常用的用于存储key-value键值对数据的一个集合,底层是基于对Map的接口实现.每一个键值对又叫Entry,这些Entry分散的存储在一个由数组和 ...
- 一篇文章快速搞懂Redis的慢查询分析
什么是慢查询? 慢查询,顾名思义就是比较慢的查询,但是究竟是哪里慢呢?首先,我们了解一下Redis命令执行的整个过程: 发送命令 命令排队 命令执行 返回结果 在慢查询的定义中,统计比较慢的时间段指的 ...
- 2019CCPC-江西省赛(重现赛)- 感谢南昌大学
A题: 题意: 给你两棵树,然后用一条边将这两棵树连接起来,然后计算 每两点之间的距离,然后求和,问这个和的最小值. 思路:根据重心的性质,树上的所有点到重心的距离最短,因此我们找到两棵树的重心,然后 ...
- 装机摸鱼日志--ubuntu16.04安装网易云音乐客户端
之前装的网易云音乐不指定啥原因不能用了,所以打算重新装一个,但是进官网只有deepin15和ubuntu18.04版本的安装包.然后我装了一下18.04的安装包,但是没有成功.甚至因为更换glibc差 ...
- 【认证与授权】Spring Security的授权流程
上一篇我们简单的分析了一下认证流程,通过程序的启动加载了各类的配置信息.接下来我们一起来看一下授权流程,争取完成和前面简单的web基于sessin的认证方式一致.由于在授权过程中,我们预先会给用于设置 ...
- StringBuilder、StringBuffer分析比较
StringBuilder.StringBuffer源码分析 StringBuilder源码分析 类结构 public final class StringBuilder extends Abstra ...