华为2014校园招聘的机试题目--字符串压缩

  1. 难得有空,玩玩今年的面试题:<p> </p><p>9月5日,华为2014校园招聘的机试题目  
  2. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。  
  3. 压缩规则:  
  4.     1、仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc"。  
  5.     2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。  
  6. 要求实现函数:   
  7.      void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);  
  8. <span style="font-family:Microsoft YaHei;">    输入pInputStr</span>:  输入字符串lInputLen:  输入字符串长度  
  9. <span style="font-family:Microsoft YaHei;">    输出 pOutputStr</span>: 输出字符串,空间已经开辟好,与输入字符串等长;  
  10. <span style="font-family:Microsoft YaHei;">注意:</span>只需要完成该函数功能算法,中间不需要有任何IO的输入输出  
  11. 示例   
  12.     输入:“cccddecc”   输出:“3c2de2c”  
  13.     输入:“adef”     输出:“adef”  
  14.     输入:“pppppppp” 输出:“8p”  
  15. </p>  
  1. /*----------------------------------------------*\  
  2.    玩玩2014年华为的机试题  
  3.    mengyafei43@gmail.com  
  4. \*----------------------------------------------*/  
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. using namespace std;  
  8.   
  9. #define MAX_LENGHT (1024)  
  10. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);  
  11.   
  12. int main(int argc, char* argv[])  
  13. {  
  14.     char src[MAX_LENGHT] = {0};//输入字符串  
  15.     char dst[MAX_LENGHT] = {0};//输出字符串  
  16.     cin>>src;  
  17.     int i=0;  
  18.     while(src[i++]){};//求字符串长度  
  19.     stringZip(src,i-1,dst);  
  20.     cout<<"Input String is:"<<src<<endl;  
  21.     cout<<"Zip String is:"<<dst<<endl;  
  22.       
  23.     return 0;  
  24. }  
  25. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)  
  26. {  
  27.       
  28.     int i=0;  
  29.     int j=0;  
  30.     char list_c[MAX_LENGHT];//list char  
  31.     int list_n[MAX_LENGHT]; //list num  
  32.     int list_idx = 0;  
  33.       
  34.     for(i=0;i<lInputLen;i++)  
  35.     {  
  36.         char cTemp = pInputStr[i];  
  37.         if(list_idx <=0)  
  38.         {     
  39.             list_c[list_idx] = cTemp;  
  40.             list_n[list_idx] = 1;  
  41.             list_idx = 1;  
  42.         }  
  43.         else  
  44.         {  
  45.             if(list_c[list_idx-1] != cTemp)  
  46.             {  
  47.                 list_c[list_idx]  = cTemp;  
  48.                 list_n[list_idx]  = 1;  
  49.                 list_idx++;  
  50.             }  
  51.             else  
  52.             {  
  53.                 list_n[list_idx-1]  += 1;  
  54.             }  
  55.         }  
  56.           
  57.     }  
  58.       
  59.     for(i=0;i<list_idx;i++)  
  60.     {  
  61.         sprintf(&pOutputStr[j],"%d%c",list_n[i],list_c[i]);  
  62.         j+=2;  
  63.     }  
  64.       
  65. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值