码迷,mamicode.com
首页 > 其他好文 > 详细

String Coloring (hard version)

时间:2020-02-05 20:01:25      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:状态压缩   with   force   bre   ace   codeforce   https   contest   return   

E2. String Coloring (hard version)

首先我们要明确一点,最多只会出现26种颜色,因为当下字母s[i]如果在后面s[>i]出现过,那么在 i 这个位置的最佳颜色选择即为先前确定的颜色。所以我们可以使用状态压缩来记录状态。

// Created by CAD on 2020/2/5.
#include <bits/stdc++.h>
using namespace std;

int a[200005];
int x[27];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;cin>>n;
    string s;cin>>s;
    int maxn=0;
    for(int i=n-1;i>=0;--i){
        int flag=0;
        for(int j=s[i]-'a';j>=1;--j)
            flag|=x[j];
        for(int k=0;k<=25;++k)
            if((1<<k)&flag) continue;
            else {
                x[s[i]-'a'+1]|=1<<k;
                a[i]=k;
                maxn=max(maxn,k);
                break;
            }
    }
    cout<<maxn+1<<endl<<a[0]+1;
    for(int i=1;i<n;++i)
        cout<<" "<<a[i]+1;
    cout<<endl;
    return 0;
}

String Coloring (hard version)

标签:状态压缩   with   force   bre   ace   codeforce   https   contest   return   

原文地址:https://www.cnblogs.com/CADCADCAD/p/12266127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!