华为机试题---字符串压缩

/*
题目描述(40分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"


要求实现函数: 
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);


【输入】 pInputStr:  输入字符串
         lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;


【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出


示例 
输入:“cccddecc”   输出:“3c2de2c”
输入:“adef”     输出:“adef”
输入:“pppppppp” 输出:“8p”
*/
include <stdio.h>  
#include<stdlib.h>
#include<windows.h>
void stringZip(const char *p_str, long len, char *p_outstr)  
{
	int count=0;
	for(int i=0;i<len;i++)
	{
		if(p_str[i]==p_str[i+1])
		{
			count++;
		}
		else
		{
			count++;
			if(count>1)
			{
				*p_outstr++ = count+'0';//把数字1转换成字符‘1’
				*p_outstr++ = p_str[i];
			}
			else
			{
				*p_outstr++ = p_str[i];
			}
			count=0;
		}
	}
	*p_outstr++='\0';
}
  
void main()  
{  
    char *str = "cccddecc";  
    int len = strlen(str);  
    char * outstr = (char*)malloc(len*sizeof(char));   
    stringZip(str,len,outstr);  
    printf("%s",outstr);  
    free(outstr);  
    outstr = NULL;  
    system("pause");
}




解析:这个题目的要求其实就是一个点,计算相邻的重复字符的个数,就是函数中的count,其中要注意的是输出时,要把数字1转换成字符‘1’;
*p_outstr++ = count+'0';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jcompress 是一款基于哈夫曼编码和最小堆的无损压缩、解压缩小程序,支持任何格式的文件的压缩与解压缩。Jcompress 的源代码位于 Utility 的 repository 分类下的 Jcompress 目录,后续会在 Utility 下面增加其他一些实用的小程序,比如基于 socket 的文件断点下载小程序等等。Jcompress代码实现1. 最小堆代码实现最小堆排序算法基本上是按照严蔚敏版的算法来实现的,其具体功能这里不再赘述,仅列出代码,读者可以参考课本自行分析之。首先是heap_min_adjust,也就是调整堆,代码如下所示:int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end){       /**       ** check error for argument       */       if(huffman_node_array==NULL || data_start<0 || data_end<0 || data_end<data_start){           printf("heap_min_adjust: argument error\n");           exit(0);       }          if(data_end==data_start) return 1;       /**       ** the top of heap-min indicated by index data_start is the only element       ** which need to be adjusted to make a min-heap       */       HuffmanNode * current_data_tobe_adjust=huffman_node_array[data_start];       long current_indexof_data=data_start;          for(long cur=2*data_start;cur<=data_end;cur=2*cur){           if(curdata_8bit_count)>((huffman_node_array[cur 1])->data_8bit_count)){                   cur =1;               }           }           if((current_data_tobe_adjust->data_8bit_count)data_8bit_count))               break;           huffman_node_array[current_indexof_data]=huffman_node_array[cur];           current_indexof_data=cur;       }       huffman_node_array[current_indexof_data]=current_data_tobe_adjust;       /**       ** return 1 means everything is ok       */       return 1;   }接下来是heap_min_construct()的代码,此函数是通过不断的调用上面的调整堆的函数来达到堆排序的目的。int heap_min_construct(HuffmanNode **huffman_node_array, long array_size){       /** valid data start from index 1 not 0 */          /**       ** check error for argument       */        if(huffman_node_array==NULL || array_size256){           printf("heap_min_construct: argument error\n");           exit(0);        }           for(long HeapRoot=array_size/2; HeapRoot>=1;HeapRoot--){           heap_min_adjust(huffman_node_array,HeapRoot,array_size);        }        return 1;   }最后是heap_min_get2min()函数。int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size){          /**       ** check argument       **/        if(huffman_node_array==NULL){           printf("heap_min_get2min: argument error\n");           exit(0);        }          *min_first=huffman_node_array[1];       huffman_node_array[1]=huffman_node_array[*heap_size];       (*heap_size)-=1;       /** after we get the min data, we should again adjust the heap to make a min-heap */       heap_min_adjust(huffman_node_array,1,*heap_size);          *min_second=huffman_node_array[1];       huffman_node_array[1]=huffman_node_array[*heap_size];       (*heap_size)-=1;          /** the same as above*/       if(*heap_size>0){           heap_min_adjust(huffman_node_array,1,*heap_size);       }          return 1;   } 标签:Jcompress

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值