标签:coloring Coloring string color version input Copy E1 dp
链接:https://codeforces.ml/contest/1296/problem/E1
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.
Input
The 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.
Output
If 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).
Examples
input
Copy
9 abacbecfd
output
Copy
YES 001010101
input
Copy
8 aaabbcbb
output
Copy
YES 01011011
input
Copy
7 abcdedc
output
Copy
NO
input
Copy
5 abcde
output
Copy
YES 00000
代码:
#include <bits/stdc++.h>
using namespace std;
long long n,k,t,s0=0,s1=0;
char a[1001];
long long dp[10001];
map<long long,long long>m;
int main()
{
cin>>n;
cin>>a;
int flag=1;
dp[0]=0;
for(int i=1;i<n;i++)
{
dp[i]=-1;
for(int j=0;j<i;j++)
{
if(a[j]>a[i])
{
if(dp[i]==dp[j])
{
flag=0;
break;
}
else
{
dp[i]=!dp[j];
}
}
}
if(flag==0)
break;
if(dp[i]==-1)
dp[i]=0;
}
if(flag==0)
cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
cout<<dp[i];
}
}
龍木 发布了215 篇原创文章 · 获赞 22 · 访问量 2万+ 私信 关注
标签:coloring,Coloring,string,color,version,input,Copy,E1,dp 来源: https://blog.csdn.net/Luoriliming/article/details/104558739
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。