zoukankan      html  css  js  c++  java
  • 字符串的压缩【百度】

    题目如下:

    通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
    压缩规则:
    1、仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc"。
    2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。


    要求实现函数:
    void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
    输入pInputStr:  输入字符串lInputLen:  输入字符串长度
    输出 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
    注意:只需要完成该函数功能算法,中间不需要有任何IO的输入输出。


    示例输入输出:
    输入:“cccddecc”   输出:“3c2de2c”
    输入:“adef”     输出:“adef”
    输入:“pppppppp” 输出:“8p”    

     1 void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){
     2     long temp=0,d_p=0,repeatCharNum=0,i=0;
     3     char *a=new char[10],* temp2;
     4     bool endingRepeat=false ;
     5     for (;i<lInputLen-1;i++)
     6     {
     7         pOutputStr[d_p++]=pInputStr[i];
     8         if(pInputStr[i]==pInputStr[i+1]){
     9             if(i==lInputLen-2){
    10                 endingRepeat=true;
    11             }
    12             d_p--;
    13             repeatCharNum=2;
    14             temp=i+1;
    15             while(temp<lInputLen-1){
    16                 temp++;
    17                 if(pInputStr[temp]==pInputStr[i]){
    18                     if(temp==lInputLen-1){
    19                         endingRepeat=true;
    20                     }
    21                     repeatCharNum++;
    22                 }
    23                 else
    24                 {
    25                     break;
    26                 }
    27             }
    28             itoa(repeatCharNum,a,10);
    29             temp2=&a[0];
    30             while(*temp2){
    31                 pOutputStr[d_p++]=*temp2;
    32                 temp2++;
    33             }
    34             pOutputStr[d_p++]=pInputStr[i];
    35             if(temp!=i) i=temp-1;
    36         }
    37     }
    38     if (!endingRepeat)
    39     {
    40         pOutputStr[d_p++]=pInputStr[i];
    41     }
    42     pOutputStr[d_p++]=0;
    43 }
  • 相关阅读:
    HDU 3401 Trade
    POJ 1151 Atlantis
    HDU 3415 Max Sum of MaxKsubsequence
    HDU 4234 Moving Points
    HDU 4258 Covered Walkway
    HDU 4391 Paint The Wall
    HDU 1199 Color the Ball
    HDU 4374 One hundred layer
    HDU 3507 Print Article
    GCC特性之__init修饰解析 kasalyn的专栏 博客频道 CSDN.NET
  • 原文地址:https://www.cnblogs.com/havePassed/p/3560043.html
Copyright © 2011-2022 走看看