• AcWing3544. 寻找变化前的01序列


    题目描述

    给你一个 01 序列,HDLC 协议处理的话,如果出现连续的 5 个 1 会补 1 个 0。

    例如 1111110,会变成 11111010。

    现在给你一个经过 HDLC 处理后的 01 序列,你需要找到 HDLC 处理之前的 01 序列。

    例如给你 11111010,你需要输出 1111110。

    输入格式

    第一行一个整数 N,表示共有 N 组测试数据。

    每组数据占一行,包含一个 01 序列。

    输出格式

    每组数据输出一行结果,为处理之前的 01 序列。

    数据范围

    (1≤N≤100),
    01 序列长度不超过 100

    输入样例:

    2
    11111010
    1111100
    

    输出样例:

    1111110
    111110
    

    题解

    枚举即可、时间复杂度(O(n))、每次判断当前字符出现个数、若出现次数不到5、即直接打印输出、到了五次即对接下来的cnt统计和输出做特判。


    代码

    #include <bits/stdc++.h>
    
    using namespace std;
    
    string str;
    
    int main(){
        int T;
        scanf("%d",&T);
        
        while(T--){
            int cnt = 0;
            cin >> str;
            string res = "";
            int n = str.length();
            
            for(int i = 0;i < n;++i){
                if(str[i] == '1'){
                    res += "1";
                    cnt++;
                }else{
                    res += "0";
                    cnt = 0;
                }
                if(cnt == 5){
                    cnt = 0;
                    i++;    // 跳过这次输出、并将cnt归零 
                }
            }
            cout << res << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    ping-tool
    yum 安装 5.6
    音视频编辑
    图表
    VC2013设置输出文件目录
    hdu 4679 Terrorist’s destroy 树形DP
    poj 3580 SuperMemo splay tree(重口味)
    hdu 1890 Robotic Sort splaytree+懒惰标记
    bzoj 1588 [HNOI2002]营业额统计 splay tree
    bzoj 1503 [NOI2004]郁闷的出纳员 splay tree
  • 原文地址:https://www.cnblogs.com/xiaofrank/p/14877867.html
Copyright © 2020-2023  润新知