//4_7_9: Run Length Encoding 字符串编码程序 POJ1782 ZOJ2240
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int i,cnt,start;
char str[10000];
while(gets(str))
{
i = 0;
start = 0; //=1表示非连续串已经开始了
while(str[i])
{
cnt = 1;
while(str[i + cnt] && str[i] == str[i + cnt] && cnt < 9) cnt ++;
if(cnt != 1)
{
if(start)
{
start = 0;
printf("1");
}
printf("%d%c",cnt,str[i]);
}
else //cnt == 1
{
if(!start)
{
start = 1;
printf("1");
}
if(str[i] == '1') printf("11");
else printf("%c",str[i]);
}
i += cnt;
}
if(start) printf("1");
printf("\n");
}
return 0;
}
/*测试结果:通过POJ1782 ZOJ2240检测
AAAAAAAAAA 12442334454454544555555
9A1A14 1112124121232415124154512465
bbbbdcccccddeadf12435324
4b1d15c2d1eadf1124353241
请按任意键继续. . .
*/
POJ1782 ZOJ2240 Run Length Encoding
最新推荐文章于 2019-09-26 21:45:55 发布