PAT1078 ----- switch with string

There are many text compression methods, here we only consider the simplest kind of: a discrete segments with the same characters and the character number of this fragment containing the character represented. For example,  ccccc to use  5c to represent. If the character is not repeated, it is output. For example,  aba it continues to be compressed  aba.

Decompression method is, in turn, the shaped like  5c such a representation is restored  ccccc.

Your request that the need of compression or decompression, for a given string for processing. Here we simply assume that the original string is non-empty string entirely of English letters and spaces.

Input formats:

The first line of a given input character, if  C it means the following strings being compressed; if  D it means that the following character string to be decompressed. The second line gives need not be compressed or decompressed string of 1000 characters, with a carriage return at the end. The number of repeated characters in the title to ensure that the range of integers, and the output file is not less than 1MB.

Output formats:

Compression or decompression request string, and outputs the result on a line.

Sample Input 1:

C
TTTTThhiiiis isssss a   tesssst CAaaa as

Output Sample 1:

5T2h4is i5s a3 te4st CA3a as

Sample Input 2:

D
5T2h4is i5s a3 te4st CA3a as10Z

Output Sample 2:

TTTTThhiiiis isssss a   tesssst CAaaa asZZZZZZZZZZ
 
. 1 #include <stdio.h>
 2 #include < String .h>
 . 3 #include <the iostream>
 . 4 #include <math.h>
 . 5 #include <ctype.h> 
 . 6 #include < the malloc .h>
 . 7 #include < the iomanip>
 . 8 #include <stdlib.h>
 . 9  the using  namespace STD;
 10  
. 11  void the compress ()
 12 is  {
 13 is      char pervious = getchar (), Current;    // a character and a post character compared before, not the same as the output 
14      int COUNT = . 1 ;
 15      the while(Current = getchar ())            // a clever way to the last time newline 
16      {
 . 17          IF (== pervious Current)
 18 is          {
 . 19              COUNT ++ ;
 20 is          } the else 
21 is          {
 22 is              IF (COUNT> . 1 )
 23 is              {
 24                  << COUT COUNT;
 25              }
 26 is              the putchar (pervious);     // output a single character 
27              pervious = Current;
 28              COUNT = . 1;
 29          }
 30          IF (Current == ' \ n- ' )
 31 is          {
 32              BREAK ;
 33 is          }
 34 is      }
 35  }
 36  void depress ()
 37 [  {
 38 is      char C;
 39      int COUNT = 0 ;
 40      the while ((C = getchar ( ))! = ' \ n- ' )
 41 is      {
 42 is          iF (isdigit (C))     // determines whether 0-9 
43          {
44             count=count*10+c-'0';
45         }else
46         {
47             if(count==0)
48             {
49                 count=1;
50             }
51             for(int i=0;i<count;i++)
52             {
53                 cout<<c;
54             //    count=0;
55             }
56             = count 0 ;            // note count condition placement 
57 is          }
 58      }
 59  }
 60  int main ()
 61 is  {
 62 is      char C;
 63 is      cin >> C;
 64      getchar ();                   // with cin if there is a \ n later is not absorbed, you have to go with the absorbent getchar () 
65      Switch (C)
 66      {
 67          Case  ' C ' :
 68                  the compress ();
 69                  BREAK ;
 70          Case  'D':
71                 depress();
72                 break; 
73     }
74     return 0;
75 }

 

Guess you like

Origin www.cnblogs.com/BananaMan/p/11333234.html