Run Length EncodingDescription 7 L& v% e1 @( ? S0 M. X % q, {/ J1 R1 K( L b' C; nYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below. : s/ h5 C C2 H1 z, h) }$ G/ v# H, r4 j7 @8 O! m
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. Z6 Y* w% ^3 H7 ?' V2 @$ }5 a* q/ t6 Y' y& o
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 / V) ?5 I9 J1 p$ Q8 A% y6 Hsequence, it is escaped with a 1, thus two 1 characters are output. 5 ^; E1 \; x" ~; O N
3 M& t6 C' X4 N8 d2 N
Input 6 j5 I4 C0 N8 R* e 3 d/ P' d& [3 K% n) o& y$ N( H) TThe 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. * Z. @! G6 L/ V5 X+ g
4 m8 C; u# Z' q; v$ xOutput ! i' R$ j( w Z. l, S# \% @+ y/ [$ j" l. a" W7 h% [" o
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. , f7 a: j1 L( ^# r* Y0 w J
8 A4 q! m5 w. m* A& Y 输入样例
( o/ |; U2 |2 p
AAAAAABCCCC ' {) V6 z9 ~1 Z- n4 n8 D12344 ( t, r! K' s6 z- i
" h6 Q6 L$ P7 n* ^( j! K! y8 @0 C# @" I2 O, b+ V: k* n 输出样例
8 H1 u5 N D) |% C; \8 J& D
6A1B14C$ ?4 C7 s7 ]: l1 d8 C
11123124 / C* g1 r8 f4 {* R/ [& h: f
& J" O' t0 u* R% a8 m6 C 7 C- E% K3 s V2 T% P1 XSource& B' n% |2 t1 L9 [) u$ ]4 @
0 }6 ~) [& u# B$ n) W4 w
Ulm Local 20042 ~. C' X: J3 q7 \4 R! U) d( K
4 U+ Y6 [4 H! K6 f" c s# A+ U2 lexample1:% Y& D- v m' w+ J, g
#include<stdio.h> * e3 }" ?$ g+ o- A8 I#include<string.h>6 Q8 I8 X- S! p3 E
void main() $ S! H1 t9 ?& E3 E4 k{ int i,j,k,n; 6 j- Z4 s" y t# z char a[50]; 5 O4 K5 G, n) `: k# s gets(a);0 Y$ x: V% D- }
n=strlen(a); + a/ V! A1 [5 V5 e% y7 M2 d7 G $ w5 x) p9 ^: ] for(i=0;i<n-1; ) , s0 K/ j0 t& Z if(a==a[i+1]) % [& _: |3 Q! b; s7 x @2 C# ~ { for(j=i+1;a[j]==a[j+1];j++); ; H4 E' c6 \1 m z* n printf("%d%c",j-i+1,a); 6 L& |1 H) I% @( J$ x: I i=j+1; f$ ?7 {+ L1 b" D' P, W
} / M1 Q9 O+ ?6 C3 m1 U% k else + f* q- x6 ?: {) { { if(a==1) " [% g y2 h0 `% z+ d! K { printf("11"); " w+ v0 {1 C/ \ o7 }. y! E% | i++; 6 \& o( ]% S" U$ W" O$ }: R% g } 1 s. \4 `; x% H! f else 8 G4 E) }& M. Q' C; @ { for(j=i+1;a[j]!=a[j+1];j++); % t+ _" u3 }0 }2 H/ D0 ^3 D printf("1"); ) p/ i% \7 F( Y' B: A' w y+ C if(j==n+1)& v9 u/ h( t5 V: \
j--; 5 i/ e! o- e5 u, A' p% M5 ~8 S$ O for(k=i;k<j;k++) - V" S. e. E- m printf("%c",a[k]);. J6 @$ Y- ^1 |0 @1 ?- s
printf("1");& L6 t3 [3 X1 |6 ]0 p1 H. x% j
i=j;8 A" h; S. c; d; t1 o; W
} ; ^. \/ V! M; f J } . w. [, I' G8 F6 t1 r i if(n==1) @7 m- ]7 y3 O/ A
if(a[0]=='1') 5 U+ R: e1 p: j3 A6 p printf("11");+ Z2 D) U& c. |' I! g1 x5 _$ i
else% g6 i6 o! c6 E$ U* Q2 b
printf("1%c1",a[0]); , s! o+ [( b6 r7 a2 i5 g printf("\n");. \1 K3 {. N# \5 ~3 u4 s) M2 ~, |6 r
} $ G: d; [- X9 Z }评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h> ; P) c4 x0 _0 h0 J' R' N#include<string.h> R$ K5 C! k. N8 p
void main()) i5 N. ^/ Q" T6 Z5 Q, |( X9 _1 {
{ int i,j,k,n; ' I7 ?2 n: ]) T7 C char a[50]; . I6 ?6 ~9 ^& Z2 r/ @) |- v gets(a);8 W' N( f& ^0 T& O6 x
n=strlen(a);* t# w$ V$ p2 _9 I
" P8 W% r! E5 e: |! n. z3 ^7 n- N3 f for(i=0;i<n-1; )% k$ N+ Y ^0 D* V- _# m
if(a==a[i+1])* K" U ~/ B9 t E) V% }4 y
{ for(j=i+1;a[j]==a[j+1];j++); * C8 b" o ?2 {- L( o: Z& f5 O printf("%d%c",j-i+1,a); 5 t, a$ X0 P! f- X i=j+1;! Q" E0 U' s$ |& P# g1 Q7 F
}6 \, E" M" L3 W0 ?
else . L$ z! B( S: u. T# y- r { if(a==1)7 l5 s( r2 g. s8 ?" C% k
{ printf("11"); ! J/ @8 X0 g- O( M& X* P i++;4 u* r" j5 o, N) U
}) {9 d7 J4 s, L3 G0 n9 a
else2 o; q( E7 b% l$ W. P9 _4 V- a" b
{ for(j=i+1;a[j]!=a[j+1];j++);) g9 G9 X+ l% Q' q3 S( ^+ H9 q
printf("1");) [# o/ [& n* W1 o/ e( a
if(j==n+1) 8 e! W$ I* Z/ k3 m j--; , y8 o: R: [7 n# ~1 l. q _( t" ]+ h for(k=i;k<j;k++); q2 m: F- x* E+ c
printf("%c",a[k]);+ E+ e' r2 N, `# [: j# k) A- W
printf("1");0 g3 y6 Z% d% q) s0 k+ C
i=j;, l3 I! L. e6 V7 N V8 O2 _
} 8 Q4 T8 Z. V! r } ! }( x3 K" n& D: H8 @0 B9 g7 P if(n==1)0 u6 N/ e0 g1 [% a
if(a[0]=='1') 0 v' w. s% i/ c! z( N- O! y' @ printf("11"); ; `3 _2 B! {: c# [' T+ a else ( f, k/ x( e }) U) T printf("1%c1",a[0]); - N4 Q0 {% |8 X! @ printf("\n"); 6 b) `! O$ @/ E3 R# F } example2:#include<stdio.h>& R( y7 R% O: m: {4 O0 S& ]. b- N
#include<string.h>4 \! y D: a1 K0 }( q6 c: A
void main() 9 S3 T! H; F a* S) Q{ int i,j,k,n;7 H; X! ]0 V+ G. |$ V# [$ v
char a[50]; 3 ^! p6 ]9 n% ]* l7 R% w2 W gets(a);, s; O2 f3 r) w* C
n=strlen(a);1 v1 r$ b1 v4 ^; O& U
$ ?% `5 B j$ ^% ^- U5 v6 ]
for(i=0;i<n-1; )3 c- H2 M' f" r3 K
if(a==a[i+1]) & j/ v( a. s) U$ ]: I { for(j=i+1;a[j]==a[j+1];j++); * n- h: ?! ?6 Y: g printf("%d%c",j-i+1,a); ' D0 e2 X% T" c! K0 g i=j+1;0 j( I0 o H$ e
} 9 Q; K. h4 j0 s! _1 I- n' e1 r else% U) H0 o& q7 ]& {& Z' ]
{ if(a==1) 4 J3 y3 d v0 u5 W D p { printf("11"); " F7 T! M' Y& N; l i++; M# N$ h* ? ]4 v$ N1 {' U
}. Z- q; Y" L0 V6 v0 a
else 2 E8 E+ d/ S9 B# ]# J { for(j=i+1;a[j]!=a[j+1];j++);/ d+ B2 S. H( b. p6 _+ k; N
printf("1"); 4 Z; D; R. u2 C7 o: N+ `, q if(j==n+1) # t+ m* y+ @: D) E: k j--;* q( m& A/ m; G. Q' J
for(k=i;k<j;k++)7 D: ~6 y$ T& Q2 }
printf("%c",a[k]); : D) L" h5 n" g3 \ printf("1"); & ~6 ?4 a% {! d i=j; 6 h7 N. g7 t- V }8 O* z( q) R* S& Q/ p
} " R4 p/ ~ ]/ Z; x7 Z9 G if(n==1)' j* R3 Q, [/ R/ Z) v$ Q
if(a[0]=='1')% H4 M& p. t8 V, k6 \5 c5 ]
printf("11"); / w( h- b! {7 u7 A else " u' O5 D* F. ~0 | printf("1%c1",a[0]);9 `1 e2 d& ?+ W u
printf("\n"); ; W' @/ J0 S: v- ~& u } f5 ~6 y8 s7 ^7 M/ l
example3:#include<stdio.h> 1 V8 _* H: C8 E* T#include<string.h>. H; s: y6 K; h- ?5 i
void main() ( [: j8 |' O5 I. v% Q! W6 V5 }! H{ int i,j,k,n;2 U1 O2 |0 ?' u2 ]6 l: j+ D: n" z* j
char a[50]; ! L. U9 o5 {4 q1 G! m9 Q. z) R+ u) O1 `! x gets(a); 0 S, W' a1 a: P+ e& t3 | n=strlen(a); % j. [" Z0 w5 V9 k, Z) w N( ]* o5 A# S* @2 E for(i=0;i<n-1; )3 O" W: M5 h9 {& u: J! {* A
if(a==a[i+1]) " W& _( y, _/ r/ E) I# \ { for(j=i+1;a[j]==a[j+1];j++);* W. a8 I8 K% ~1 k/ A
printf("%d%c",j-i+1,a); + k+ e* O0 D; h+ [$ B i=j+1;) v, y( t5 U- ?
} 4 q' v. @, ~. k3 f" H7 n( c% T5 e else3 n* @/ Q4 I+ S0 R, `, C( k
{ if(a==1)7 n: |- r! |0 Y8 L% o2 B. S
{ printf("11");: g# Y+ }$ }1 t, ~4 M
i++; * X5 B" ~$ s2 x! J6 N" x( f5 S } ) u' U/ Y6 E+ V# V7 X9 Z else3 ~1 `, q$ o3 y8 q
{ for(j=i+1;a[j]!=a[j+1];j++); , K; y- Y, k P printf("1");2 T5 d2 `$ C8 Z7 S
if(j==n+1)% }2 _1 T7 S: Y! O
j--; 3 Q* Y0 E& K& i" t* Z c for(k=i;k<j;k++). [# K( e; Q# B' _1 @6 S
printf("%c",a[k]); ; ]0 g' _3 p& m5 E printf("1");, Z* O4 a U: u O# l
i=j; % p/ d9 ^ P, D } 3 @2 O* ]' Q- {) R4 k9 G }, A4 ?2 ?% v% N( \7 Z& g
if(n==1)8 m& `+ ]# s+ O0 o- ?5 o7 `
if(a[0]=='1')5 d" g o0 {) F9 G
printf("11"); $ P8 `8 K1 M# m; n8 s2 r else 3 S5 W* r J4 S* l1 P5 G printf("1%c1",a[0]);4 T. i( k, r1 O8 L: D; D# \$ S; [
printf("\n"); 4 L$ H6 h/ p! X! e N4 O }4 ?$ o! b' x& t1 ~
来源:编程爱好者acm题库