CodeForces - 1296E1 String Coloring (easy version) 贪心

一、内容

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.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 s  )After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such anoperation 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 n(1≤n≤200) — the length of sThe second line of the input contains the string consisting of exactly nlowercase 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 ncharacters, the i-th character should be '0' if the ith character is colored the first color and '1' otherwise).

Input

9
abacbecfd

Output

YES
001010101

二、思路

  • 首先我们需要交换的字符:后面的字符大于前面的字符。 那么这2个字符的颜色肯定不能相同。
  • 可以建立一个图, 颜色肯定不相同的2个点连一条边。 那么就转化为二分图颜色。
  • 其实通过分析我们是从后面枚举前面,那么每次将颜色变换就行了。 直接一次扫描就可以。

三、代码

二分图染色:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 205, M = 8e4 + 5;
struct E {
	int v, next;
} e[M];
int n, len, h[N], c[N];
char s[N];
void add(int u, int  v) {
	e[++len].v = v; e[len].next = h[u]; h[u] = len;
}
bool dfs(int u, int color) {
	c[u] = color;
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (c[v] == -1) {
			if (!dfs(v, color ^ 1)) return false;
		}else if (c[v] == color) return false;
	}
	return true;
}
int main() {
	scanf("%d%s", &n, s + 1);
	memset(c, -1, sizeof(c));
	for (int i = n; i > 0; i--) {
		for (int j = i - 1; j > 0; j--) {
			if (s[i] < s[j]) {
				add(i, j); add(j, i);		
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		if (c[i] == -1) {
			if (!dfs(i, 0)) {printf("NO\n");return 0;}
		}
	}
	printf("YES\n");
	for (int i = 1; i <= n; i++) printf("%d", c[i]);
	return 0;
} 

贪心:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 205;
int n, c[N];
char s[N];
int main() {
	scanf("%d%s", &n, s + 1);
	memset(c, -1, sizeof(c));
	for (int i = n; i > 0; i--) {
		if (c[i] == -1) c[i] = 0;
		for (int j = i - 1; j > 0; j--) {
			if (s[i] < s[j]) {
				if (c[j] == -1) c[j] = c[i] ^ 1;
				else if (c[j] != c[i] ^ 1) {printf("NO"); return 0;}  //代表颜色相同 
			}
		}
	}
	printf("YES\n");
	for (int i = 1; i <= n; i++) printf("%d", c[i]);
	return 0;
} 
发布了456 篇原创文章 · 获赞 466 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104376257
今日推荐