原题

This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.
You are given a string ss consisting of nn lowercase Latin letters.
You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).
After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.
The goal is to make the string sorted, i.e. all characters should be in alphabetical order.
Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.InputThe first line of the input contains one integer nn (1≤n≤2001≤n≤200) — the length of ss.
The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.OutputIf it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print “NO” (without quotes) in the first line.
Otherwise, print “YES” in the first line and any correct coloring in the second line (the coloring is the string consisting of nn characters, the ii-th character should be ‘0’ if the ii-th character is colored the first color and ‘1’ otherwise).

题意简介

要求你用 0 1串表示一段字符串,如果相邻两位不同,那么这两位代表的字符可以进行交换
问是否存在一串01串进行有限次交换使得该字符串满足字典序。

思考

首先,我们需要思考01串交换的性质,熟悉01串的朋友可能一眼就看出来此题中的01串满足一个性质
0可以在连续为1的子串中游动,当遇到0时停止
举个例子:
001111->0<-1110
箭头指向的0满足这一条件向左可以运动到第3位,向右可以遇到倒数第二位;
同理1也如此
那么通过一系列变换后我们发现原子串等价于
一串连续的0+一串连续的1时,并且0和1的原下标均为递增

再进一步

我们需要考虑什么情况下,一串连续的0+一串连续的1,通过变换可以满足图中的条件?
答案很显然:
当0和1代表的子串不下降时;
那么算法很显然:
找出两段不下降的子数列

code:

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
char p[200010];
char s[200010];
int vis[200010];
int main(){int len;cin>>len;cin>>p+1;int so = 0;for(int i=1;i<=len;i++){if(p[i]>=so){so = p[i] ;s[i] = '0';vis[i] = 1;}}so = 0;for(int i=1;i<=len;i++){if(!vis[i] && p[i]>=so){so = p[i];s[i] = '1';vis[i] = 1;}if(!vis[i] && p[i]<so){cout<<"NO"<<endl;return 0;}}cout<<"YES"<<endl;for(int i=1;i<=len;i++){cout<<s[i];}
}

CF1296E1——String Coloring (easy version)相关推荐

  1. Codeforces Round #617 (Div. 3) E2. String Coloring (hard version) 思维 + dp + Dilworth定理

    传送门 文章目录 题意: 思路: 题意: 让你给一个串染色,不同颜色且相邻的一对字符可以互换位置,用最少的颜色,使交换后这个字符串字典序最小. 思路: 考虑将字符串分成若干个非递减的子序列,由于其非递 ...

  2. E1. Rubik‘s Cube Coloring (easy version) 贪心,满二叉树(1300)

    题意 : 给定一个层数为k的满二叉树,结点编号为标准的层序遍历的编号 魔方有六个面,如图,每个面一个颜色 树上的结点的颜色也是这六个颜色之一,但是两个相邻结点的颜色必须是 魔方中,颜色相邻的两种颜色 ...

  3. D1. Kirk and a Binary String (easy version)

    题目链接:http://codeforces.com/contest/1204/problem/D1 D1. Kirk and a Binary String (easy version) time ...

  4. CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)

    题目链接:点击查看 题目大意:交互题猜密码,设原密码为 xxx,猜的密码为 yyy,如果没猜到,密码会自适应变成 zzz,满足 x⊕z=yx \oplus z=yx⊕z=y ,最多猜 nnn 次 题目 ...

  5. Codeforces Round #579 (Div. 3) F1. Complete the Projects (easy version) 排序 + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 比较直观的想法就是对于bi≥0b_i\ge0bi​≥0的项目,我们将aia_iai​从小到大排序,让后依次加bib_ibi​,如果有取不到的,显然就无 ...

  6. 1282B1. K for the Price of One (Easy Version)

    B1. K for the Price of One (Easy Version):题目 两种情况,前面取一或者前面不取 #include <bits/stdc++.h> using na ...

  7. 1420C1. Pokémon Army (easy version)

    C1. Pokémon Army (easy version):题目 题意:选择其中一部分,按照+-依此计算,求总和 思路:找到局部最大值,然后减去局部最小值,依此找. #include <bi ...

  8. Codeforces Round #617 (Div. 3) String Coloring(E1.E2)

    (easy version): 题目链接:http://codeforces.com/contest/1296/problem/E1 题目一句话就是说,两种颜色不同的字符可以相互换位, 问,对这字符串 ...

  9. 智乃的数字积木(easy version)<每日一题>

    题目: 题目链接: 登录-专业IT笔试面试备考平台_牛客网 题目思路(easy version): 封装一个能够在相同颜色交换位置 并将字符串转换成数字 的自定义函数 再每次改变颜色后 只需修改颜色值 ...

  10. Tokitsukaze and Good 01-String (easy version)

    Tokitsukaze and Good 01-String (easy version) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题目描述 This is the easy ...

最新文章

  1. 我来做百科(第二十天) D
  2. 分区视图(转自小春BOOK)
  3. *36.操作系统中磁盘的调度算法
  4. 判断两个时间在15分钟内_为什么敷面膜的使用时间要15—20分钟,这个时间怎么算出来的?...
  5. postgresql 备份恢复(一)
  6. POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
  7. 黑马程序员——GUI篇
  8. 测试的主要评测方法(3)
  9. 还不知道如何使用 IDEA ?教你三招快速掌握 IDEA
  10. 地震matlab频域分析,基于matlab的地震数据的分析.doc
  11. win10安装IIS及操作使用
  12. 如何自学成为程序员?
  13. 查找文件命令find和文件内容查找命令grep
  14. U8字符串(u8前缀)的作用
  15. 一个demo理解什么是MVP
  16. 殿影酒店即将开业,推出电影和酒店的跨界创新模式
  17. 【机器学习网络】神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)
  18. 华为云数据库mysql云灾备方案_华为云MySQL云灾备解决方案发布,放心的数据库都有异地保护...
  19. 【金融量化】期货中的对手价、市价、排队价、最新价分别表示什么价位
  20. qpsk相点 matlab,QPSK误码率和星座图MATLAB仿真

热门文章

  1. ml-agents与tensorflow结合的先关操作文档
  2. 程序猿头头(js数组reverse,sort,concat,slice, splice)
  3. Excel2003和Excel2007对下拉选择和下拉级联选择的操作以及java程序的调用
  4. python背景图添加_Python实例讲解 -- tkinter canvas (设置背景图片及文字)
  5. Vue.js——前端模块化雏形和CommonJS——2020.12.9
  6. 机器学习第五章之决策树模型
  7. 机器学习基础-Lagrange duality
  8. NYOJ 找球号(二)(哈希表)
  9. 激光跟踪传感器的工作原理
  10. excel公式编辑器_Excel如何用函数公式制作随机抽奖小程序