| Run Length EncodingDescription : R: U. j: P* C- {8 T0 G6 @
 ' _4 r- @3 [, I9 nYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below. : E+ k3 D) m8 U; L- p% N$ V/ W# f2 e1 T
 3 ]5 W5 `) S* Z) A$ K( l7 n
 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. 6 a4 n9 |4 A# O; e& e
 + Q! a$ }) F: Q8 y( h; X# G
 Any 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
 0 t9 ~( P5 R8 z! ~; nsequence, it is escaped with a 1, thus two 1 characters are output. ; W+ Z, s" c' l0 z2 |# ]
 
 0 F' D( X9 L% }; ?* D5 _Input & n8 |8 p( g" ]) B
 + L; z6 ~6 `2 |1 I8 B. r
 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.
 1 X3 M& b* A1 c8 v9 j- {- H; [1 g$ W! K- h) p
 Output ( {. ^2 h$ J: c7 Z. `% ?* V1 ~
 
 , P- N$ i, _/ F  E0 _3 [5 W6 FEach 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.
 , R, B8 I+ O2 W7 w1 `4 `# L3 ^2 X
 ! `- H" h3 K7 V1 s( b! E1 B2 x输入样例
 % ~, b+ R& q% E/ K+ IAAAAAABCCCC1 A3 M& _6 H& a6 V
 12344
 # n# P3 i8 B5 V) P8 `/ f1 t
 - K4 s8 l4 ~' \( F% ?3 }& W* M0 c) N
 & w% @  W( ~# o$ J输出样例
 % R0 X# X  X, X1 m- f6A1B14C
 1 [' m: l" p; c6 E$ d: O111231240 d4 [& j/ _+ i- S4 i; D; U) S& T
 
 . R8 p' J2 Q/ H
 8 l3 d' d; Y; X! p  A5 W; K' MSource$ Z: ^3 K) C$ [6 N( a! g
 
 7 f/ v1 m# j- ZUlm Local 2004
 ' V6 Z' p6 ]  E" T, |" x1 o1 ]
 5 A! s/ y: e" y9 m; |example1:+ O/ G% `# U& _7 L
 #include<stdio.h>9 Z1 s1 T2 a! `( ~
 #include<string.h>5 M( b; \1 t' o5 `1 D  n
 void main()5 M# a& x) H0 o- f% |
 {  int i,j,k,n;
 ( D8 I) z0 c, k, W# o   char a[50];0 ]; x, |' _; z
 gets(a);
 . U4 F7 {* k, a. Q& H0 E   n=strlen(a);
 ) \: h$ ~. {* i( d8 W; U. d- N- I' x$ `
 for(i=0;i<n-1; )
 + _+ F$ [! g( L      if(a==a[i+1])+ D- _7 x  O) T& f0 L- n: q
 {  for(j=i+1;a[j]==a[j+1];j++);0 {4 P5 o9 j4 D, l: q
 printf("%d%c",j-i+1,a);
 3 s8 t" w  G0 g          i=j+1;
 + r+ S1 h4 ~6 E4 q      }! x: n5 V, w* S. T- q3 d1 r1 E
 else
 3 t; Y8 {( h6 f, Q4 q: y9 Q      {  if(a==1)
 9 d2 Y7 I$ F( {            { printf("11");
 3 W1 w6 v; j* e  |+ `                 i++;- y5 T! i  _/ D: v! x& N* ?, Q
 }
 0 p' D. P& q/ U( _          else
 4 i( @1 v- J0 j2 V  E! f) c          {  for(j=i+1;a[j]!=a[j+1];j++);
 8 _% n" e, ~. U) P8 @4 }0 l              printf("1");- n1 J+ S1 }, c- O$ J5 e
 if(j==n+1)
 ' w4 M- h3 V0 z: P: O                  j--;
 # P4 J. j& w# f. p- `              for(k=i;k<j;k++)
 ' q& e6 A4 k1 ^/ |" v8 }                  printf("%c",a[k]);
 5 ]3 H6 P1 P5 t3 B- A% N! k              printf("1");9 r- m6 U: L) B: j9 `- E0 ?
 i=j;
 ' }$ J+ d& a8 K( R1 Q" w          }
 ; E6 l8 \% j2 k* F- r7 W' h' V2 Y      }" G7 e$ R' V: ^9 u7 b6 L
 if(n==1)# I" g5 }/ {2 f5 b* P* _
 if(a[0]=='1')
 ( Q0 I% e" P7 t5 j5 l              printf("11");0 o* u. D* a  t+ G! W+ U
 else
 : K8 T8 D! T; j: b$ ^' T; x              printf("1%c1",a[0]);, d7 q6 Q2 N$ j$ F) ~7 N
 printf("\n");
 5 U6 u/ ?# u+ s& r$ h+ e1 @1 L }4 i3 x+ x+ z# r- V, s2 o1 c4 b" G
 评论人: Colby  发布时间: 2010-3-2 12:04:06 #include<stdio.h>
 4 z2 O, [# U# H- X7 T0 H1 O#include<string.h>  @# Q2 T4 s9 k
 void main()# x  X; m# x$ V8 F; m5 {
 {  int i,j,k,n;: ~% [0 ?4 u( `4 l! D
 char a[50];  @5 S  S! s( S+ b% a  v0 ]7 [5 q
 gets(a);
 # b7 J* Q6 ~0 x! D; L   n=strlen(a);& l5 d: a+ Y3 A) `; W
 
 7 \$ r6 }4 Y- n9 O( L" E' V   for(i=0;i<n-1; )" Q4 h- T1 n# }5 i1 e
 if(a==a[i+1])
 " T8 p$ _1 Z% N2 o      {  for(j=i+1;a[j]==a[j+1];j++);5 G6 N- Z6 n5 k. Y) {  k! \, |
 printf("%d%c",j-i+1,a);
 ! P" y' S% v/ ]" R          i=j+1;! ]- D: }3 n- h9 _9 ?
 }) s0 Y5 M4 _- K
 else
 + n& `0 B0 o9 i      {  if(a==1)
 * q% l7 f% I5 |- `% \            { printf("11");
 2 x6 _2 `0 |: G8 y- C% W                 i++;
 " e+ R+ Q7 A+ g" @' U            }) d, o% f6 }& l1 ]9 ]
 else
 _6 q9 `) F! q. k          {  for(j=i+1;a[j]!=a[j+1];j++);' Y5 F; I) ~! f- J0 b
 printf("1");2 D- I' Y7 \+ z  O* y0 d0 e
 if(j==n+1)
 . K5 W8 ^3 }; ]5 ~                  j--;' v$ O  v9 r5 n& a( R2 H/ a
 for(k=i;k<j;k++)+ K4 ^$ x3 `/ ^2 A' D
 printf("%c",a[k]);
 * Z6 w- [7 S) e6 X  `9 a! [7 N              printf("1");! M# A. _6 ^: g  K; u1 M# L
 i=j;0 \/ D2 o8 P  D1 I0 |" K% O3 b+ @
 }" w4 Z( w4 i& W: I) f1 N) }: y. t) d
 }. ^, l3 f8 ~0 f( E8 d* A  f7 h( K
 if(n==1)
 ! d" r! f9 m" `- L! R1 e& J          if(a[0]=='1')$ p' b" Y, {' \% F7 t
 printf("11");
 " P, f3 n4 R* X) @. ]5 F          else/ w% H- v( E3 t1 E0 `; W) [& o
 printf("1%c1",a[0]);9 {: P( j" W' |2 a7 X: A
 printf("\n");
 * n' [! l1 E+ @& Y' e0 g% D+ l }    example2:#include<stdio.h>
 ) l) ]( I+ B8 C. j#include<string.h>
 ' {7 F9 L$ Y+ j  f$ Avoid main()
 ! k) X7 f' K- H! s. K* T; l{  int i,j,k,n;
 O" @: X" J# ^) E   char a[50];! w0 b/ n3 o5 d7 R" n
 gets(a);6 j+ \, }$ |% A% u9 O
 n=strlen(a);
 6 r6 L7 Q4 k, t2 q4 ^# b* y0 i, O" p" J4 Z/ p
 for(i=0;i<n-1; )
 7 h# d8 {8 f. W      if(a==a[i+1])
 ^7 z$ K) |2 Y/ ]8 p% S/ Y      {  for(j=i+1;a[j]==a[j+1];j++);
 * q- `  K& M3 Y          printf("%d%c",j-i+1,a);% R5 p- S: Y  W# X  R; D+ q/ V7 w
 i=j+1;
 - j/ i* C6 m: H, t/ L      }
 # \; n/ x& ?6 _8 `4 q1 m; a      else
 7 W, @* c, _+ B) z      {  if(a==1)1 [" C7 [$ T' E  C+ z1 [0 k. C
 { printf("11");' f  B5 g" }6 t3 P6 Z0 v
 i++;
 " }  A# a# i7 n5 d5 V            }0 Q! n! B- K4 Z: m) H+ b4 B
 else/ i/ v$ H3 ?% K3 U4 g$ h
 {  for(j=i+1;a[j]!=a[j+1];j++);
 " {1 `7 e; L# C" V; {0 E  p              printf("1");
 8 v* m8 B& h* q              if(j==n+1)" S+ C3 H0 s- r9 Q3 ?4 Z5 R
 j--;' p# i7 N0 V6 s# U( j1 L
 for(k=i;k<j;k++)5 ]+ c' D: e# }2 V* C
 printf("%c",a[k]);1 q+ o& f* O9 u) w
 printf("1");
 6 m% @, c, x) s# y2 M# @) `              i=j;! j: N6 y. S2 }2 P
 }) a& Y, z+ A3 T( }: m+ u. u
 }& b+ g- Q0 {7 o, Z) I
 if(n==1)5 K: p: C# E. D, b. ~. I, C0 V4 {+ w
 if(a[0]=='1')
 2 ]9 W0 f5 g& N, f7 C7 M! n( l; b              printf("11");/ O- N/ V: g, x
 else" _1 g  [9 Z$ I% \( m* e3 O7 r; s8 e
 printf("1%c1",a[0]);6 m0 [2 C6 h- _
 printf("\n");
 $ Q: N  M4 E- d$ d, F# Q1 ^ }$ P& t1 i$ w/ K8 x& F/ v4 Q
 example3:#include<stdio.h>5 `* _/ k& |0 b/ M* ]0 x+ s- {2 @% }
 #include<string.h>
 $ L1 Z3 z- F# Gvoid main()4 C( V7 _: J' p+ X: |3 B
 {  int i,j,k,n;: C+ t$ ~) e4 O3 U( V! f' F- T
 char a[50];
 + Y1 i; R5 X+ @* t4 E7 B   gets(a);
 ! Q9 }, B# }$ I) z( x   n=strlen(a);& ]3 |' s' S* T0 _& Z4 D  k6 A
 & K' [& N; t& w
 for(i=0;i<n-1; )
 6 o) W0 q. d4 p: _      if(a==a[i+1])
 3 A; I" }7 _, }, ~' g8 I3 F* }4 S* @      {  for(j=i+1;a[j]==a[j+1];j++);/ t8 m8 Y+ l( L
 printf("%d%c",j-i+1,a);6 c1 R3 l$ N1 k, U; |: i
 i=j+1;- v4 b/ V+ X) `+ l% X! u
 }
 # F, z6 f: z& y# Z% t9 Z: a3 M      else2 |0 W$ g( t* t; ?% s  g
 {  if(a==1)) I' X) P$ J8 ~3 g9 W
 { printf("11");
 6 A  T. ?7 ~5 W' L                 i++;' k* H2 U' }- a/ p2 P2 Y( J' h6 ]2 r; S1 i
 }0 v3 r% H+ I6 T- [7 {& l% q
 else' m& Y+ @% {# o
 {  for(j=i+1;a[j]!=a[j+1];j++);$ }+ E5 F7 E+ A! H
 printf("1");  r% L. S+ _# }& ^9 Y  h
 if(j==n+1)
 ' v( K- a1 u9 P+ [- b                  j--;7 A) O. z) L" V
 for(k=i;k<j;k++)
 - ?* I  q* w! P4 U; n                  printf("%c",a[k]);/ S9 [. b- Z1 V+ Q; H
 printf("1");3 v" c+ X4 b* n8 \, \  O
 i=j;
 - ?5 m) q  F& M, U& [          }
 6 r( o2 J* ]8 {6 L" a      }" v4 s3 t. x" o1 S( [
 if(n==1)
 - s/ r' }1 u& j5 X: m/ o2 P* P          if(a[0]=='1'); S- P9 i5 b# d( w; o+ N: p9 {
 printf("11");
 7 X) W9 `8 Y+ O( k$ O2 o          else
 7 c' A- b: b- ]              printf("1%c1",a[0]);
 7 h! z+ c" Q, D9 v3 P# u4 u   printf("\n");
 V% `" V2 `! Q- c }
 " v; Y( k  R6 D& G9 j! N9 X) Y8 y% l" F      来源:编程爱好者acm题库
 |