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

First, the content

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

Second, the idea

  • First, we need to exchange the characters: the characters following larger than the preceding character. Then the color of these two characters certainly not be the same.
  • You can create a view of the same color is certainly not even a two-point edge. Then converted into a bipartite graph colors.
  • In fact, we enumerate the front from the back by the analysis, then each color conversion on the line. It can scan directly.

Third, the code

Bipartite graph coloring:

#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;
} 

greedy:

#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;
} 
Published 456 original articles · won praise 466 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_41280600/article/details/104376257