hdu 1020_Encoding(水题)

题目链接:clink here~~

题目大意是给出一连串单词,输出连续且相同字母的次数,次数为1的不输出次数。

#include <cstdio>
#include <cstdlib> 
#include <vector>
#include <iostream>
using namespace std;
struct str{
    char a;
    int counts;
}Str[10000];
int main()
{
     int n;
     char a;
     int start = 0;
     int j;
     bool exit;
     bool begin;
    
     scanf("%d",&n);
     getchar();
     while(n --)
     {         
         for(int i = 0; i < 10000; i++)
         {
             Str[i].counts = 0;
            // Str[i].a = ' ';
         }
         start = 0;
         while(scanf("%c",&a))
         {
             exit = false;
             begin = false;
             if(a == '\n')break;
             if(start == 0)
             {
                 Str[start].a = a;
                 Str[start].counts = 1; 
                 start++;
                 begin = true;
             }             
             else  
             {     
                if(a == Str[start - 1].a)
                 {
                     Str[start - 1].counts ++;
                     exit = true;//printf("Str[%d].counts:%d\n",j,Str[j].counts);
                 }
             }
             if(!begin && !exit)
             {
                 Str[start].a = a;
                 Str[start].counts = 1; 
                 start ++;
             }
         }
        // printf("start:%d\n",start);
         for(j = 0; j < start; j ++)
            if(Str[j].counts > 1)
                 printf("%d%c",Str[j].counts,Str[j].a);
             else
                 printf("%c",Str[j].a);
         printf("\n");

     }
}

如果有更简洁的代码,欢迎贴出交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值