• D. Binary String To Subsequences


    D. Binary String To Subsequences

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given a binary string ss consisting of nn 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 tt independent test cases.

    Input

    The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

    The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of ss. The second line of the test case contains nn characters '0' and '1' — the string ss.

    It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

    Output

    For each test case, print the answer: in the first line print one integer kk (1≤k≤n1≤k≤n) — the minimum number of subsequences you can divide the string ss to. In the second line print nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k), where aiai is the number of subsequence the ii-th character of ss belongs to.

    If there are several answers, you can print any.

    Example

    input

    Copy

    4
    4
    0011
    6
    111111
    5
    10101
    8
    01010000
    

    output

    Copy

    2
    1 2 2 1 
    6
    1 2 3 4 5 6 
    1
    1 1 1 1 1 
    4
    1 1 1 1 1 2 3 4 

    =========================================================================

    贪心的策略当然是能加就加进原本就有的,不能加的时候再重新开辟一个

    这里用vector进行模拟,末尾存当前串的编号即可

    1. # include
    2. # include
    3. # include
    4. using namespace std;
    5. int ans[200000+10];
    6. int main ()
    7. {
    8. //从前往后扫
    9. //1的话就加进去0,把加进去的0结尾变成1结尾
    10. //0的话就加1,把加进去的1结尾变成0结尾
    11. //也就是贪心+模拟,能加就加即可,并不影响后来的
    12. int t;
    13. cin>>t;
    14. while(t--)
    15. {
    16. int n;
    17. cin>>n;
    18. vector<int>v[2];
    19. string s;
    20. cin>>s;
    21. s=" "+s;
    22. int now=s[1]-'0';
    23. int cnt=1;
    24. ans[1]=cnt;
    25. v[now].push_back(ans[1]);
    26. for(int i=2; i<=n; i++)
    27. {
    28. if(s[i]=='0')
    29. {
    30. if(v[1].empty())
    31. {
    32. cnt++;
    33. ans[i]=cnt;
    34. v[0].push_back(ans[i]);
    35. }
    36. else
    37. {
    38. int now=v[1].back();
    39. v[1].pop_back();
    40. ans[i]=now;
    41. v[0].push_back(ans[i]);
    42. }
    43. }
    44. else
    45. {
    46. if(v[0].empty())
    47. {
    48. cnt++;
    49. ans[i]=cnt;
    50. v[1].push_back(ans[i]);
    51. }
    52. else
    53. {
    54. int now=v[0].back();
    55. v[0].pop_back();
    56. ans[i]=now;
    57. v[1].push_back(ans[i]);
    58. }
    59. }
    60. }
    61. cout<
    62. for(int i=1; i<=n; i++)
    63. {
    64. cout<" ";
    65. }
    66. cout<
    67. }
    68. return 0;
    69. }

  • 相关阅读:
    洗衣洗鞋柜洗衣洗鞋小程序
    人工智能、深度学习、机器学习书目推荐
    神经网络可以用来分类吗,神经网络相关问题
    Linux内核的配置和编译(2)——Linux内核下载与配置
    计算机专业毕业设计项目推荐13-超酷炫轻食平台(Go/Java+Vue+Mysql)
    深入理解计算机系统,汇编的流程控制
    py并发编程实践-demo
    我的创作纪念日
    【云原生】k8s 中的 hostNetwork 和 NetworkPolicy(网络策略)讲解与实战操作
    华为数通方向HCIP-DataCom H12-821题库(单选题:221-240)
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126244869