华为(C++实现字符串压缩程序)

本文介绍了一个简单的字符串压缩算法实现过程,使用C++编程语言。该算法遍历输入字符串,统计连续字符的数量,并将这些信息编码为更短的形式输出。文章通过具体示例展示了如何减少存储空间并提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string>
using namespace std;
void string_zip(string input,string &output)
{
	if(input.empty())
		return ;
	char last='\0';
	char current;
	int count=0;
	for(int i=0;i<input.size();++i)
	{
		current=input[i];
		if(current==last||last=='\0')
		{
			count++;
			last=current;
		}
		else
		{
			if(count>1)
			{
				output.push_back(count+'0');
			}
				output.push_back(last);
			count=1;
			last=current;
		}
	}
	if(count>1)
		output.push_back(count+'0');
	output.push_back(last);
}
int main()
{
	string input("aabbbbccd");
	string output;
	string_zip(input,output);
	cout<<output<<endl;
	system("pause");
	return  0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值