题目大意:

给定一段长度为n的字符串s

你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换

需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z)

你只有两种颜色可以用来涂

问是否存在这么一种涂色方案满足题意

存在,输出YES,再用01表示两种不同的颜色,把涂色方案输出(如果有多种,输出任意一种)

不存在,输出NO

解题思路 1:

因为只有两种颜色可以用来涂

相同颜色彼此不能交换

所以同一种颜色组成的序列绝对是非严格递增的

那么就用mx记录一种颜色代表的非严格递增的序列到某个位置时的最大字符,pmx记录另一种颜色代表的到某个位置时的最大字符

只要在找第一种颜色时出现了一个比最大值小的,就把他推给第二种颜色

如果也比第二种颜色最大值小,说明至少得三种颜色才能满足,此情况直接输出NO

#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,i;
string s,ans="";
cin>>n>>s;
char mx='a',pmx='a';
for(i=;i<n;i++){
if(s[i]>=mx){
mx=max(mx,s[i]);
ans+="";
}
else{
if(pmx<=s[i]){
ans+="";
pmx=max(pmx,s[i]);
}
else{
cout<<"NO\n";
return;
}
}
}
cout<<"YES\n"<<ans;
}
int main(){
ios::sync_with_stdio();
cin.tie();cout.tie();
solve(); return ;
}

解题思路 2:

※该思路可直接用于题 E2 hard 版本

可以引入一个 r 数组,开26个空间代表26种字母

这个数组 r[i] 的值代表 第 i 个字母在前面涂的颜色最大编号是多少,0表示没出现过

遍历这个字符串,每遍历到一个字母时,从当前字母后一个位置开始往后再遍历这个数组,即找比当前字母要大的所有字母中编号最大的那个字母的编号

根据解题思路1,如果一个字符比一种颜色最大值要小,那么就把它推给下一种颜色

如果这个过程所有颜色都按照从1开始递增的顺序来,那么,这个字符的颜色编号会比前面的比他大的字符中编号最大的那个编号还要大

就可以得出结论,每次找比当前字母大的所有字母中涂色方案最大的编号+1,即为当前字母涂色方案

只要涂色方案出现大于2种,直接输出NO

#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,i,j,d,r[]={};
string s;
cin>>n>>s;
string ans;
for(i=;i<n;i++){
d=;
for(j=s[i]-'a'+;j<;j++)//从当前字母后一个位置开始往后找
d=max(d,r[j]+);
r[s[i]-'a']=d;
if(d>){
cout<<"NO";
return;
}
ans+=((d-)?"":"");
}
cout<<"YES\n"<<ans;
}
int main(){
ios::sync_with_stdio();
cin.tie();cout.tie();
solve(); return ;
}

解题思路 3:

※该思路可直接用于题 E2 hard 版本

因为相同颜色彼此不能交换,所以必定是已经有顺序的

那么就可以得到这一种想法,编号从1开始,从头到尾遍历出一条非严格递增的标记为颜色0,如果标记出来的数量和小于n(即还有一些字符没有被标记),那么就加一种颜色,重新再来一遍,如果还是没有全部涂起来,说明颜色至少要3种,直接返回NO

#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,i,done=,cur=;
char mx;
string s;
cin>>n>>s;
vector<int> ans(n,-);
while(done<n){
mx='a';
if(cur>){
cout<<"NO";
return;
}
for(i=;i<n;i++){
if(ans[i]==-&&s[i]>=mx){
mx=s[i];
done++;
ans[i]=cur;
}
}
cur++;
}
cout<<"YES\n";
for(i=;i<n;i++)
cout<<ans[i];
}
int main(){
ios::sync_with_stdio();
cin.tie();cout.tie();
solve(); return ;
}

Codeforces 1296E1 - String Coloring (easy version)的更多相关文章

  1. E1. String Coloring (easy version)(贪心)

    E1. String Coloring (easy version) time limit per test 1 second memory limit per test 256 megabytes ...

  2. Codeforces 1296E2. String Coloring (hard version)

    这道题和HDU1257一模一样,一开始窝都用贪心直接解,没法理解为什么求一个最长下降序列,直到看了巨巨的题解,先给出一个定理,Dilworth's theorem,离散学不好,补题两行泪,该定理是说, ...

  3. E2. String Coloring (hard version)(贪心)

    E2. String Coloring (hard version) time limit per test 1 second memory limit per test 256 megabytes ...

  4. codeforces Equalizing by Division (easy version)

    output standard output The only difference between easy and hard versions is the number of elements ...

  5. Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)

    <题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...

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

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

  7. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  8. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  9. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

随机推荐

  1. An attempt was made to call the method com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; but it does not exist. Its class, com.google.gson.GsonBuilder, is available from the foll

    SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/G:/sharp/repo ...

  2. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project sharp-common: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin

    [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.sharp:sharp-common > ...

  3. mongodb- 备份和导入备份

    一.使用 mongodump 命令备份数据 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表 -o 文件存放路径 参数说明: -h 指明数据库宿主机 ...

  4. vux 中 this.$vux.loading undefined 的问题

    时间:2018-04-03 摘要:this.$vux.loading 报 undefined 今天在使用 事件触发 vux 的 loading  组件时,发现无法触发成功,显示 undefined 然 ...

  5. XV6源代码阅读-同步机制

    Exercise1 源代码阅读 锁部分:spinlock.h/spinlock.c以及相关其他文件代码 // Mutual exclusion lock. struct spinlock { uint ...

  6. python实现进程的三种方式及其区别

    在python中有三种方式用于实现进程 多进程中, 每个进程中所有数据( 包括全局变量) 都各有拥有⼀份, 互不影响 1.fork()方法 ret = os.fork() if ret == 0: # ...

  7. 009、MySQL取当前时间Unix时间戳,取今天Unix时间戳

    #取Unix时间戳 SELECT unix_timestamp( ) ; #取今天时间戳 SELECT unix_timestamp( curdate( ) ); 显示如下: 不忘初心,如果您认为这篇 ...

  8. wincc的服务器-客户机模式具体做法(全抄-未测试)

    一.原来的工作方式:在同一工作组中4台计算机其windows名分别为A.B.C.D且都已安装好wincc5.0+sp2,原来在每台计算机上运行的均是单用户,4台计算机上实际运行的是一个相同的项目,最先 ...

  9. 九、响应式发:rem和less(适配移动端)

    一.响应式开发 响应式开发优先适配移动端又兼容到pc端 官网:https://less.bootcss.com/usage/ 教程:https://www.w3cschool.cn/less/ rem ...

  10. SQLAlchemy建立数据库模型之间的关系

    一对多关系 多对一关系 多对多关系 一对一关系 一对多关系(一个作者,多篇文章) ## 一对多关系,单作者-多文章,外键不可少 ## 外键(ForeignKey)总在多的那边定义,关系(relatio ...