HDU1020 Encoding

本文介绍了一个简单的字符串压缩算法实现过程,使用C++编程语言通过循环遍历字符串来计数重复字符并输出。此算法适用于处理大量重复字符的场景。

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

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1020
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif #include < iostream >
None.gif#include
< string >
None.gif
using namespace std;
None.gif
None.gif
void doProcess( string & str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
intcount,i,j;
InBlock.gif
for(i=0;i<str.length();)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcount
=1;
InBlock.gif
for(j=i+1;j<str.length();++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(str[i]==str[j])
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//记录相同的字符个数
InBlock.gif
count++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
if(count>1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<count<<str[i];
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<str[i];
ExpandedSubBlockEnd.gif}

InBlock.gifi
=j;//更新起始位置
ExpandedSubBlockEnd.gif
}

InBlock.gifcout
<<endl;
ExpandedBlockEnd.gif}

None.gif
int main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
intcaseNum,i;
InBlock.gif
stringstrTmp;
InBlock.gif
while(cin>>caseNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=0;i<caseNum;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcin
>>strTmp;
InBlock.gifdoProcess(strTmp);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return0;
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值