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 ...
随机推荐
- MySQL REPLACE INTO 的使用
前段时间写游戏合服工具时出现过一个问题,源DB和目标DB角色表中主键全部都不相同,从源DB取出玩家数据再使用 replace into 写入目标DB中,结果总有几条数据插入时会导致目标DB中原有的角色 ...
- "选择图片"组件:<pickimage> —— 快应用组件库H-UI
 <import name="pickimage" src="../Common/ui/h-ui/media/c_pickimage"></ ...
- canvas 实现光线沿不规则路径运动
canvas 实现光线沿不规则路径运动 此文章为原创,请勿转载 1.svg实现 2.canvas实现 3.坑点 svg让动画沿着不规则路径运动 查阅svg文档后发现,svg动画运动有两种实现方式,且都 ...
- CVE-2020-1938:Apache-Tomcat-Ajp漏洞-复现
0x00 漏洞简介 Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行. Apache Tomcat服务器存在文件包含 ...
- 360众测考试 Drupal 漏洞 CVE-2018-7600 远程代码执行-复现
0x00 前言 昨天360众测遇到的一个题 今天自己搭环境复现一下,希望对大家有帮助 0x01 漏洞简介 Drupal是一个开源内容管理系统(CMS),全球超过100万个网站(包括政府,电子零售,企业 ...
- Trie(字典树、前缀树)
目录 什么是Trie? 创建一棵Trie 向Trie中添加元素 Trie的查询操作 对比二分搜索树和Trie的性能 leetcode上的问题 什么是Trie? Trie是一个多叉树,Trie专门为 ...
- A - Oil Deposits DFS
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- 基于canvas的画板
最近重新在看Html5&CSS3的知识,看到canvas的时候,想到了以前在学校学计算机图形学时做过的画图实验,于是想,可以基于html5和css3来做一款画板,经过1天的努力,完成了画板的一 ...
- Gatling脚本编写技巧篇(二)
脚本示例: import io.gatling.core.Predef._ import io.gatling.http.Predef._ import scala.concurrent.durati ...
- linux内核第一宏 container_of
内核第一宏 list_entry()有着内核第一宏的美称,它被设计用来通过结构体成员的指针来返回结构体的指针.现在就让我们通过一步步的分析,来揭开它的神秘面纱,感受内核第一宏设计的精妙之处. 整理分析 ...