• 【0006】数据的压缩、解压缩——时间优先、空间优先


    时间优先的方式实现数据的压缩、解压缩

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // "aaaaabbbhaihualovefangfangooooooooo";    --> 5a3bhaihualovefangfang9o
    char * timefastzip(char *str)
    {
        int length = strlen(str);                                // 获取待压缩字符串的长度
        char *newstr = (char *)calloc(length, sizeof(char));    // 给压缩的字符串开辟内存
    
        char *pstr = str;
        char *pnewstr = newstr;            // 保存分配内存的首地址
    
        while (*pstr != '')
        {
            int charlength = 0;
            char * p = pstr;
            while (*p == *(p + 1))
            {
                charlength++;                        // 统计重复字母次数
                p++;
            }
    
            if (charlength == 0)
                *pnewstr++ = *pstr++;
            else
            {
                *pnewstr = charlength + 1 + '0';    // 重复字母个数转化为数值型字符
                *(pnewstr + 1) = *pstr;
                pnewstr += 2;
                pstr += charlength + 1;
            }
        }
    
        _recalloc(newstr, strlen(newstr), sizeof(char));        // 释放压缩的空间
    
        return newstr;
    }
    
    char * timefastunzip(char *str)
    {
        char *newstr = (char *)calloc(10000, sizeof(char));
    
        char *pstr = str;
        char *pnewstr = newstr;
    
        while (*pstr != '')
        {
            char *p = pstr;
            if (*p >= '0'&&*p <= '9')
            {
                int length = *p - '0';
                for (int i = 0; i < length; i++)
                {
                    *pnewstr++ = *(p + 1);
                }
                pstr += 2;
            }
            else
            {
                *pnewstr++ = *pstr++;
            }
        }
    
        _recalloc(newstr, strlen(newstr), sizeof(char));
    
        return newstr;
    }
    
    void main006()    
    {
        char str[1024] = "aaaaabbbhaihualovefangfangooooooooo";    // 5a3bhaihualovefangfang9o
    
        printf("%s 
    ", str);
        printf("%s 
    ", timefastzip(str));
        printf("%s 
    ", timefastunzip(str));
    
        system("pause");
    }
    时间优先

    空间优先的方式实现数据的压缩、解压缩

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // 双指针法操作字符串
    // "aaaaabbbhaihualovefangfangooooooooo";    --> 5a3bhaihualovefangfang9o
    char * spacezipa(char *str)        // 压缩字符串中的a
    {
        char *p1 = str;
        char *p2 = str;
    
        while (*p1 = *p2)
        {
            if (*p1 != 'a')
            {
                p1++;
                p2++;
            }
            else
            {
                p2++;
            }
        }
    
        return str;
    }
    char * spacezipX(char *str)        // abhaihualovefangfango
    {
        char *p1 = str;
        char *p2 = str;
    
        while (*p1 = *p2)
        {
            int length = 1;
            while (*p1 == *p2)
            {
                p2++;
            }
            p1++;
        }
    
        return str;
    }
    char * spacezip(char *str)        // 5a3bhaihualovefangfang9o
    {
        char *p1 = str;
        char *p2 = str;
    
        while (*p1 = *p2)
        {
            int length = 1;
            char *p3 = p2 + 1;
            while (*p1 == *p3)
            {
                p3++;
                length++;
            }
    
            if (length > 1)
            {
                *p1 = length + '0';
                *(p1 + 1) = *(p3 - 1);
                p1 += 2;
                p2 += length;
            }
            else
            {
                p1++;
                p2++;
            }
        }
    
        return str;
    }
    
    char * spaceunzip(char *str)
    {
        char *p1 = str;
        char *p2 = str;
    
        while (*p1 = *p2)
        {
            int length = 0;
            if (*p1 >= '0'&&*p1 <= '9')
            {
                length = *p1 - '0';
            }
    
            if (length == 0)
            {
                p1++;
                p2++;
            }
            else
            {
                char ch = *(p1 + 1);
                for (char *p = p1 + strlen(p1); p > p1 + 1; p--)        // 5a  扣除两个字符
                {
                    *(p + length - 2) = *p;
                }
                
                for (int i = 0; i < length; i++)
                {
                    *(p1 + i) = ch;
                }
    
                p1 += length;
                p2 = p1;
            }
        }
    
        return str;
    }
    
    void main007()    
    {
        char str[1024] = "aaaaabbbhaihualovefangfangooooooooo";    // 5a3bhaihualovefangfang9o
    
        printf("%s 
    ", str);
        printf("%s 
    ", spacezip(str));
        printf("%s 
    ", spaceunzip(str));
    
        system("pause");
    }
    空间优先
  • 相关阅读:
    [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]
    [BZOJ4842]Delight for a Cat[费用流]
    [HNOI2018]转盘[结论+线段树]
    [LOJ#6066]. 「2017 山东一轮集训 Day3」第二题[二分+括号序列+hash]
    [CF963E]Circles of Waiting[高斯消元网格图优化+期望]
    [CF966F]May Holidays[分块+虚树]
    【JZOJ5088】【GDOI2017第四轮模拟day2】最小边权和 排序+动态规划
    【JZOJ5086】【GDOI2017第四轮模拟day1】数列 折半搜索
    GDOI2017第四轮day1总结
    【51nod1563】坐标轴上的最大团 贪心
  • 原文地址:https://www.cnblogs.com/ant-colonies/p/13434023.html
Copyright © 2020-2023  润新知