http://acm.hdu.edu.cn/showproblem.php?pid=1020
#include <iostream>
#include <cstring>
using namespace std;
void f(char *a,int len)
{
int i = 0;
int j,cnt = 1;
char temp;
while(i < len)
{
temp = a[i];
while(temp == a[i+1])
{
cnt++;
i++;
}
i++;
if (cnt != 1)
{
cout << cnt;
}
cout << temp ;
cnt = 1;
}
}
int main()
{
int n;
cin >> n;
char a[10100];
while(n--)
{
cin >> a;
f(a,strlen(a));
cout << endl;
}
return 0;
}
本文提供了一种解决HDU 1020问题的C++实现方案,该方案通过压缩重复字符来减少字符串长度。具体而言,程序读取输入的字符串并输出压缩后的形式,如果相同字符连续出现,则仅保留一个字符并记录其数量。
2022

被折叠的 条评论
为什么被折叠?



