<span>题解 CF1399D 【Binary String To Subsequences】</span>

题目链接:http://codeforces.com/contest/1399/problem/D
题目描述:

You are given a binary string s consisting of n zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤2⋅\(10^4\)) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅ \(10^5\)) — the length of s. The second line of the test case contains n characters '0' and '1' — the string s.
It is guaranteed that the sum of n does not exceed 2⋅ \(10^5\) (∑n≤2⋅\(10^5\)).

Output

For each test case, print the answer: in the first line print one integer k (1≤k≤n) — the minimum number of subsequences you can divide the string s to. In the second line print n integers \(a_1\),\(a_2\),…,\(a_n\) (1≤\(a_i\)≤k), where \(a_i\) is the number of subsequence the i-th character of s belongs to.
If there are several answers, you can print any.

最开始的朴素做法(这种做***TLE):
用一个数组c来存储每个子序列最后的数字,在遍历数组a的同时使\(a_i\)遍历数组c,使其接上满足题意的子序列。如果没有找到,\(a_i\)单独作为一个子序列。用\(b_i\)来记录\(a_i\)是属于第几个子序列的,因为是保序的,只需令\(b_i\)=j+1就行了。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        char x;
        cin>>n;
        vector<int> a(n+5),b(n+5),c;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i]=x-'0';
        }
        
        c.push_back(a[1]);
        b[1]=1;
        for(int i=2;i<=n;i++)
        {
            int len=c.size();
            bool flag=true;
            for(int j=0;j<len;j++)
            {
                if(a[i]+c[j]==1)
                {
                    c[j]=a[i];
                    b[i]=j+1;
                    flag=false;
                }
                if(flag==false)
                break;
            }
            if(flag==true)
            {
                c.push_back(a[i]);
                b[i]=len+1;
            }
        }
        cout<<c.size()<<endl;
        //cout<<"n=="<<n<<endl;
        for(int i=1;i<=n;i++)
           cout<<b[i]<<" ";
        cout<<endl;
        c.clear();
        //cout<<"c.size()=="<<c.size()<<endl;
    }
    return 0;
}

正确做法:上面的朴素做***TLE的根本原因在于对于每一个\(a_i\)都要遍历一遍数组c,这样时间复杂度就上去了,而实际上我们只需要对每个\(a_i\)判断当前已经存在的子序列中是否有以1结尾的子序列或以0结尾的子序列即可。而不需要每次遍历。因此我们用两个数组分别来存取以0或1结尾的子序列就可以了。这时为了确定\(b_i\)的值,我们需要对两个数组中所存储的值进行修改,应该存储的是每个子序列的编号,而不再是0或1了。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
string s;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n >> s;
        vector<int> b(n + 5), c0, c1;
        for (int i = 0;i < n;i++)
        {
            int newpos;
            if (s[i] == '0')
            {
                newpos = c0.size() + c1.size();
                if (c1.empty())
                {
                    c0.push_back(newpos);
                }
                else
                {
                    newpos = c1.back();
                    c1.pop_back();
                    c0.push_back(newpos);
                }
            }
            else
            {
                newpos = c0.size() + c1.size();
                if (c0.empty())
                {
                    c1.push_back(newpos);
                }
                else
                {
                    newpos = c0.back();
                    c0.pop_back();
                    c1.push_back(newpos);
                }
            }
            b[i] = newpos + 1;
        }
        cout << c0.size() + c1.size() << endl;
        for (int i = 0;i < n;i++)
            cout << b[i] << " ";
        cout << endl;
    }
    return 0;
}
全部评论

相关推荐

10-27 02:29
已编辑
门头沟学院 嵌入式工程师
牛客72783561...:简历不是这么写的,你这两个项目只说了用到了什么技术,却没说取得了什么成果,在我看来这就是你自己做的一个demo,没有价值。你为什么不写你电赛国二的那个项目?
点赞 评论 收藏
分享
面试官全程关摄像头1.自我介绍一下2.React和Vue哪个更熟悉一点3.你在之前那段实习经历中有没有什么技术性的突破(我只是实习了44天工作28天,我把我能说的都说了)4.你封装的哪个表单组件支不支持动态传值5.自己在实习阶段Vue3项目封装过hook吗6.hook有什么作用7.Vue2和Vue3的响应式区别(我说一个是proxy是拦截所有的底层操作,Object.defineProperty本身就是一个底层操作,有些东西拦截不了,比如数组的一些操作还有等等,面试官就说实在要拦截能不能拦截????我心想肯定不行呀,他的底层机制就不允许吧)8.pinia和vuex的区别(这个回答不出来是我太久没用了)9.pinia和zustand的区别,怎么选(直接给我干懵了)(我说react能用pinia吗&nbsp;&nbsp;他说要用的话也可以)10.渲染一万条数据,怎么解决页面卡顿问题(我说分页、监听滚轮动态加载,纯数据展示好像还可以用canvas画)(估计是没说虚拟表单,感觉不满意)11.type和interface的区别12.ts的泛型有哪些作用(我就说了一个结构相同但是类型不同的时候可以用,比如请求响应的接口,每次的data不同,这里能用一个泛型,他问我还有什么)13.你项目用的是React,如果让你再写一遍你会选择什么14.pnpm、npm、yarn的区别15.dependencies和devdependencies的区别总而言之太久没面试了,上一段实习的面试js问了很多。结果这次js一点没问,网络方面也没考,表现得很一般,但是知道自己的问题了&nbsp;&nbsp;好好准备,等待明天的影石360和周四的腾讯了&nbsp;&nbsp;加油!!!
解zj:大三的第一段面试居然是这样的结局
查看15道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务