| Run Length EncodingDescription * N" R, k" m( B& c 
 9 j3 d7 X% q/ W6 v+ H7 ?Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. ) [! o) B2 Q( E% E: j6 W) M
 2 ^5 T3 q8 X) D* R$ f0 F) a) z
 Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.
 / T, {" V4 b( c9 I
 D: K) g! j* F( \0 _( o* W* AAny sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the
 . `+ W/ z: P  E' M1 Fsequence, it is escaped with a 1, thus two 1 characters are output.
 6 w7 t9 k' `4 }' `, a' J5 e, o* V0 X* B$ Y. K  E4 Y7 @8 A% R( {
 Input
 1 x7 M9 q: a! D2 J% o: @$ H# A9 C8 _9 Y
 The input consists of letters (both upper- and lower-case), digits, spaces, and punctuation. Every line is terminated with a newline character and no other characters appear in the input. 7 q9 P) ~6 g& Q$ t: \" E6 E* b
 
 + U6 O6 P9 I# mOutput * Q: P' Y; t  o- P
 b3 r; r3 h" u6 L6 K
 Each line in the input is encoded separately as described above. The newline at the end of each line is not encoded, but is passed directly to the output.
 9 C8 N& u9 j- A/ X* D: I, A# }6 |0 Q5 R! \
 输入样例
 " ~, C5 J0 k( ]! S2 d* `# x0 m$ P! j- ^6 d" \+ wAAAAAABCCCC8 I4 b. ^2 _& v( z. t
 12344" m& ~8 r7 u; S% G! Y- b
 
 ( j; ?4 d! W8 o0 Z  j- Q, G9 v6 M
 输出样例
 + L0 ^( U, F2 i$ a  O/ @6A1B14C
 & t2 U' `9 r0 S9 ^111231240 |) z# D" y' o! N
 
 ) x$ _6 n" q* q* r# \
 . ]% s$ P) P* |9 L8 N9 mSource
 : r' P/ l& z8 X" o/ `) {' L6 q$ D, r% X0 _! h+ w
 Ulm Local 2004) U  Z' f" m* X
 $ A6 X! h+ U6 _8 J
 example1:
 4 ^4 _! v/ r4 l" M8 J% i! N9 z#include<stdio.h>
 + m2 ~: i+ L, R* p#include<string.h>' p- m3 s1 m2 y2 `6 c1 h9 N! m7 {
 void main()
 0 y; Z' ^: R" G7 c5 n{  int i,j,k,n;: e- a2 F( p- g: L
 char a[50];
 # r5 i  Z( n  D9 [   gets(a);
 3 r: x, j, D- F   n=strlen(a);
 8 k1 k' n5 F: \  r) f6 v# I9 a% }) ?- v9 o
 for(i=0;i<n-1; )
 9 W0 x: z6 @, v1 N4 r5 [      if(a==a[i+1])/ v- k- R! e5 k3 k) \
 {  for(j=i+1;a[j]==a[j+1];j++);2 c! S! r4 ]8 E* v0 G9 _2 ^
 printf("%d%c",j-i+1,a);6 B$ o/ n, m# {& X1 r8 y
 i=j+1;6 v, M5 @+ Z% V/ l
 }
 $ e3 {: n4 x7 A* ]5 c      else3 ~( X2 U; y& ~4 C: t- B  E' \
 {  if(a==1)
 0 R/ ^- x" {2 s6 ~1 Z            { printf("11");
 & W* C. k$ H* {; T1 n2 |( G3 ~, @                 i++;
 - ~0 J* j% f( }3 ~8 Z4 X& ~            }1 e  D& I5 t' y, m
 else
 T- z% X! P' u  W2 ]          {  for(j=i+1;a[j]!=a[j+1];j++);  a: D6 b; p1 b+ X' y
 printf("1");( z, p6 y) b( _
 if(j==n+1)/ D0 f+ I6 ]: N/ s* `. R; E
 j--;
 " Q6 {* B2 E9 K& x7 [( X& J              for(k=i;k<j;k++)- q% g  |1 I$ Z1 T! s) `
 printf("%c",a[k]);% f$ d1 m/ S) `# a4 u
 printf("1");9 d. E& d# }% k, j3 w: N. g6 y
 i=j;0 k2 b, D8 E6 K2 C: Z
 }2 V. K- {9 z& `. k' z. Q
 }6 Z3 h3 d+ ~+ |( a, p/ Z
 if(n==1)
 1 n7 L7 k' C8 g7 ]          if(a[0]=='1')
 ( Y6 q9 l+ N+ A2 F. W              printf("11");/ _/ @' Y9 j. x  B1 p
 else
 % g4 k  N+ k* Q+ y; Z% I3 k              printf("1%c1",a[0]);
 - K6 t- g1 x& m: l# I' Z* P3 ]: I   printf("\n");
 + T/ Q, d: O( T+ |) E9 { }, a% s1 m; J, r9 l
 评论人: Colby  发布时间: 2010-3-2 12:04:06 #include<stdio.h>0 V6 m( r) C, S" e. t. r3 a
 #include<string.h>& N" g) d2 A5 Q3 D
 void main()& d9 w* c! X1 }! \7 E
 {  int i,j,k,n;: y  d" `! J( n% \9 T6 @
 char a[50];- ~% a- f/ n7 |0 p2 W! s, S" _5 C
 gets(a);
 ; Q" U' E( a* ]3 m( f; o   n=strlen(a);& y. u0 E9 }& o" u! i$ b0 `
 0 l' M& q. c* @  ^
 for(i=0;i<n-1; )7 }  f. N& Z& U# Q5 u* h
 if(a==a[i+1])! G7 \& w9 Q5 U3 L, c1 N, P
 {  for(j=i+1;a[j]==a[j+1];j++);3 u& L$ n8 x, {8 G
 printf("%d%c",j-i+1,a);
 ; D$ q/ S- N# t, l, o2 Y          i=j+1;  M* p* w6 K0 ?1 A2 i. k( {7 |
 }
 ) r* [" L) I" Q5 d3 T  L      else
 ! ~4 ]6 S9 U* k# B      {  if(a==1)( p/ Y; i" J" ~: K! ]% i+ `
 { printf("11");( Y, E/ |7 {2 h5 y: ?4 b0 N
 i++;" w3 d4 {, ~; q  {; ^* {% B$ L) ?* `
 }( p) h  \- L! ]& p! B. D
 else
 . R# v) U( O6 A1 i  I- e          {  for(j=i+1;a[j]!=a[j+1];j++);( R6 G! \- l- y2 \9 I& a3 d# s* r
 printf("1");
 3 }$ d7 R/ t- k* r9 ^, R5 y! ]              if(j==n+1)
 / v7 \* C! u* r% |% s3 f                  j--;% Q3 B3 O% U6 D1 W
 for(k=i;k<j;k++)
 9 O& E8 _# k. r/ Q$ [                  printf("%c",a[k]);
 6 E8 {  _) ~4 ~% ~: R8 ~3 W8 K# X              printf("1");
 / \# Y, r  a5 Y0 u3 `! w% `              i=j;$ H# P6 {% D1 o
 }
 % J- v6 E6 A0 S      }/ U% h& U" v/ M8 U( f
 if(n==1)
 $ I7 p/ W- o" L1 {; x2 x% m% o          if(a[0]=='1'). R0 N5 v( S( j3 s4 t. W
 printf("11");
 - \* w% d& e* M; h7 f! M6 c          else
 3 y% F0 |0 n3 r9 x9 b3 Z              printf("1%c1",a[0]);
 1 ^0 `# ?5 D" x5 v5 b& G   printf("\n");% H/ Q7 ]) C+ O, n8 T. r
 }    example2:#include<stdio.h>- u' o) \: q/ b6 n5 j8 T
 #include<string.h>
 0 g6 p4 g' p. N2 d) y8 evoid main()
 ( H! j+ y6 M1 A5 [8 l9 K% c+ C{  int i,j,k,n;
 ( i0 e; e. Z* O+ d2 K   char a[50];
 % a) l9 V' ~1 `: f   gets(a);
 n* q/ k: Y$ w. P" k& s- S   n=strlen(a);
 ! v' d& P; Z# N0 R. F. O, l& D; K( m
 for(i=0;i<n-1; )5 N! T& W7 W4 h, h% {
 if(a==a[i+1])! n3 `' g/ D0 T6 l7 W( U
 {  for(j=i+1;a[j]==a[j+1];j++);
 0 r( V4 z7 g/ ^+ _7 y          printf("%d%c",j-i+1,a);& K) b% _) l/ V4 `
 i=j+1;# s- L6 h5 }0 Y
 }0 Y/ t6 ~, ^; e
 else
 ! R" y* i' I. ]: W9 y: ~8 z8 g      {  if(a==1)! b1 |% J7 C* f6 c8 q
 { printf("11");
 / O9 h; T: F0 M/ r( W  u- a  z                 i++;' k. H" U. `8 g+ Z( g
 }2 Z# q$ m; l  G3 u6 u3 q- \- G7 J
 else
 4 Z! s/ U. p% {3 m. b" d          {  for(j=i+1;a[j]!=a[j+1];j++);
 6 Z: s5 U; j) H* J6 m; r              printf("1");2 g! a  `; a/ |9 A8 g
 if(j==n+1)
 r  w6 e( j, l$ v: U                  j--;/ y& R3 o- z: l2 Y
 for(k=i;k<j;k++)6 J: n4 k1 t0 |
 printf("%c",a[k]);
 ( O" Y5 f1 s9 T: e              printf("1");
 2 x, o1 u. v1 y! ]* j              i=j;
 3 C2 V- c& u$ @- p          }) M+ r  k, p. |. i4 l' j, Y
 }
 1 a5 N& \' w! y/ p+ P# c      if(n==1)
 8 v) V- H2 q& N% v2 y          if(a[0]=='1')5 a" M' M/ c$ X3 {, z& o
 printf("11");% o) |1 ?& ~: E& n" W
 else
 6 K2 ~* I+ o+ F( B% ~4 O              printf("1%c1",a[0]);8 X, f, v$ G1 z2 R0 C1 ]
 printf("\n");$ v1 w* d% R! S- b3 b
 }
 6 z- i& `5 C, V+ t/ p1 R8 |4 ]4 e* o   example3:#include<stdio.h>( L& {4 z  l4 E, d" j
 #include<string.h>! n3 `9 `- r1 [5 l
 void main()2 C) s& u) Q! t9 K, d; C8 w$ u
 {  int i,j,k,n;3 @/ Q0 E9 z2 L
 char a[50];
 % o1 C& o# u) p) d* j   gets(a);
 2 U5 r8 C1 n" }. s( u   n=strlen(a);! _' t1 k( V5 `2 Y. [% ~5 l) S
 4 c+ O% \9 ?1 [+ F
 for(i=0;i<n-1; )* _( P% B4 A% p1 u1 @' T' t& `; F: W
 if(a==a[i+1])1 f% P* ~6 B! I# a& M" H9 ~
 {  for(j=i+1;a[j]==a[j+1];j++);
 - V' t0 z# r/ ]! }, q9 l          printf("%d%c",j-i+1,a);3 P0 U. c3 ?; t
 i=j+1;" ], u2 h# U: ]4 t
 }& e( T% {% O+ N6 W- Y- c5 X
 else4 F, C: b' u5 j! U' T' U
 {  if(a==1)3 z! m' K. E( ]9 `: Y0 g2 @% D
 { printf("11");
 5 S; u1 V) q2 O                 i++;/ Z% X9 ~- w1 K; Z4 d
 }; a4 \  N: z; \8 T0 A: U* k, t9 O. z
 else& |, a$ R' H" U3 m9 X; Q
 {  for(j=i+1;a[j]!=a[j+1];j++);
 . T( q2 }& \5 t" `              printf("1");+ @# N! f/ }; e, I
 if(j==n+1)
 0 ~: ~$ U1 j7 Q/ Q  \, b: U# j! A2 i4 ?                  j--;
 # K& {* Y  E1 j              for(k=i;k<j;k++)  T+ M7 K7 O( J5 J% ?, v, w
 printf("%c",a[k]);
 ) A! H" i9 k' v- R. V              printf("1");# B. t: X/ B+ H# V8 f7 s: P  E! X
 i=j;* W& Z% e& e; p% a
 }
 2 N) T9 m7 v. P, D      }
 " y" l; H: X2 x! v' Z5 g      if(n==1)
 / d/ F# W, V6 [! H6 d          if(a[0]=='1')' V/ S  U! _- `( H
 printf("11");
 2 T2 J) d1 Z4 H7 W  l% W9 Z8 O  o          else
 , l, l/ q8 {/ o8 g3 f; P, _              printf("1%c1",a[0]);& l: A/ A! V0 r% v( {* x
 printf("\n");) ?/ R+ q5 D0 J5 E" i; A, ]
 }- E0 W0 L. Z- c" l
 来源:编程爱好者acm题库
 |