Run Length EncodingDescription ; Y+ \+ T R4 b
; z" D6 ^7 d/ [& zYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
9 @5 K. [/ r' x: \) f5 M z6 J% r# h
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. 5 N; t3 w0 j% |9 I( @
' p, ^7 F' N2 h: h8 w% b* K; J
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 ( D/ `3 M% T9 n( ?7 e& O# V
sequence, it is escaped with a 1, thus two 1 characters are output.
: X; C+ s3 d) `
$ n9 U& m8 V' n8 i2 q% [Input
) f) O+ o5 ^+ L, L
- p9 v( M5 i- e: M; L1 j" `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.
! g- ?8 o8 \) _3 f' {( I) A# [7 I2 F* w% F5 i5 \7 B4 u+ W, ^
Output
% b4 t. e r& ?) @) D
9 c; \& F9 A' O" U/ d' wEach 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. 7 i! t: f- ?0 v: _) @+ f0 q
( j/ ~, Z: S/ q& T6 y1 V9 |# ?
输入样例
9 o; s( f: B* U& {; [- SAAAAAABCCCC
* s' I8 _9 t8 c3 u1 u" O& @: y12344
- b) O6 d5 M$ M
( m& J- Q, B9 o: h9 K9 r3 w/ S( j* y* K8 h
输出样例 % T7 z- ]9 h# X. S7 L
6A1B14C+ p2 I9 E# B8 u! a! R
11123124" ^% r. Y% [" Z, ^3 X, @' u
% L5 Q- _8 K6 `* \1 Z6 M" J& }% X
# @0 x, |; z: w8 z; D$ z7 L% T0 QSource8 X4 Y5 z2 d* h
' [' r* N. F: S$ O0 P( w/ R5 RUlm Local 20046 @3 y- k5 x+ B8 B
) Z. A$ q8 Y( I1 f9 _1 L
example1:
8 i8 d7 n$ m/ e. }#include<stdio.h>. `8 B" ~$ P% \% D
#include<string.h>
u( B4 y/ i" C4 M9 u( q* ~1 l/ Xvoid main()
- ~6 f# W; z) `1 ^{ int i,j,k,n;
, D4 P0 x% W0 ?4 z char a[50];
% V( f5 [6 ~* ^/ ` gets(a);
6 X, {) r1 [8 Y, y, O7 O n=strlen(a);
1 k9 U$ y* v: ^% s! w4 q. a$ N4 r/ D
for(i=0;i<n-1; ): \3 C% d# W. }# R I8 i9 ^
if(a==a[i+1])
% l6 u1 a3 J( ^# \) r4 Z { for(j=i+1;a[j]==a[j+1];j++);/ f& A2 s! m3 j, \
printf("%d%c",j-i+1,a);7 }: E s, A- U* g% B! K, c
i=j+1;
3 ~- w. k! ?, j7 d }
" W- b1 x* C4 n& u# j7 o6 F) H else" E" ~" w% B4 M4 C5 Y$ o1 U
{ if(a==1). I: {# Z6 C! X6 u2 Y: x4 _# \
{ printf("11");7 S. ~% |' M8 c# y1 J. Z' |0 _
i++;5 h( h( n- Q5 s) l2 ~: C3 e
}
9 ~6 F% W6 M% `, i) H, [ else
$ }- C( \% @, l" D { for(j=i+1;a[j]!=a[j+1];j++);! @: P* R3 N/ P3 ~3 ^+ f. e
printf("1");
( E8 ^: X: x+ @- S/ m# M if(j==n+1)% M$ A+ N6 u: V+ ?2 ]
j--;
5 o$ C. y! R: ^ p for(k=i;k<j;k++)
# ^ t ^3 h2 ^1 q5 g- b: M1 R printf("%c",a[k]); n; o4 M8 S8 ~5 B$ {) A! p
printf("1");8 k$ x6 B' h, v1 Z4 v8 z
i=j;
6 n& r2 u" d! W* y }3 i& q r9 [, ^
}
- V3 G7 Q' ^7 H+ u! `" K, b u2 Z if(n==1)
: d' v/ @9 @' S7 d4 W if(a[0]=='1')
3 h! s& Q' g+ e( o( D printf("11");
( m# [) ^$ s, q$ E% r% O else5 E' `2 U9 {( h5 ?1 k7 e
printf("1%c1",a[0]);
' [& C6 f* n% J- d+ q; d5 l: e. m4 l printf("\n");% w0 V8 Q9 G$ y- m0 F% \1 J3 U
}
( F4 M! ]7 G3 S4 \评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h># G W& w f% X$ s+ L$ q) y, B
#include<string.h>) z& e0 t6 d. j4 x, [
void main()
. f; R. w. N* M5 c{ int i,j,k,n;
5 F# c2 G8 m/ Q9 W" P9 y char a[50];, z5 R0 w. u) R2 s& ]6 `0 T) w+ N
gets(a);
+ N6 k$ [4 y$ V. c n=strlen(a);; A6 Y5 W( M* S7 o! t
) H& d/ Z6 w3 R for(i=0;i<n-1; )2 t$ A3 b) |% Q' g9 _0 z# p' b
if(a==a[i+1]). m" o$ X" }3 g) u# x
{ for(j=i+1;a[j]==a[j+1];j++);# m% X' {" @. L
printf("%d%c",j-i+1,a);" w9 f" \' y9 r
i=j+1;. b9 u4 `2 {1 O6 G+ _! c
}
y# j5 J% F6 H3 L0 _9 ] else1 ]& u0 b5 O" b4 z
{ if(a==1)
- G/ q. R- m( M" _. p) }' ^ { printf("11");; E7 s7 R5 v/ X
i++;( [" Y6 k% N, i. B& j! G4 g+ Y
}
C7 p. J( f1 Y" @4 N0 C" s- q else& ~& P( p/ @' x0 y% n
{ for(j=i+1;a[j]!=a[j+1];j++);0 [1 M! i4 {7 w* g' a; U' j- s/ i
printf("1");% T- b; n9 }* l- |4 W1 Y
if(j==n+1)2 c3 E6 t) k& D$ N S s
j--;( ?' `! M2 {! @8 K7 b9 f* O
for(k=i;k<j;k++)5 C3 }; m. ]# c3 H
printf("%c",a[k]);, e, [+ M$ F7 d- }; q
printf("1");
L" j1 a. O K) `5 y. y- i i=j;$ t6 N8 m' w! b2 l# r( }4 L
}+ F7 I, g5 B6 F
}2 k- {$ N: c& H; H: C y9 }. N
if(n==1)& C4 X, s( q+ g
if(a[0]=='1')
4 C8 ^$ T; I2 C% n% n" D/ M printf("11");
. i6 [" P1 p! @2 @ else4 |+ ^4 e4 p7 ~# h: \
printf("1%c1",a[0]);
/ K1 m3 F7 r5 T: A6 } @ printf("\n");
8 F9 N, F& i* Y5 z$ u } example2:#include<stdio.h>
' b( I: S! A& W, o1 F% `* }#include<string.h>. r6 ? s7 B2 ], G
void main()& _# r, ^2 k3 }' L
{ int i,j,k,n;
) y5 @% Z; D% S# A9 [ char a[50];
& u* x$ P5 s/ l gets(a);$ s z2 O) {7 P; @7 J! l! p
n=strlen(a);, k# Z. _: s1 U% t) [! E% q- d
9 B3 k# X! X' Q/ t a. X
for(i=0;i<n-1; )' u! [- Z w* f3 y5 a
if(a==a[i+1])) z- M0 B2 [: x/ L5 O
{ for(j=i+1;a[j]==a[j+1];j++);, b/ }7 g6 W; C* h
printf("%d%c",j-i+1,a);: j7 m4 e! ?& y7 R
i=j+1;
9 W7 [4 d1 Z& H0 P6 a' k8 h; d }
3 G) m: \$ A/ q1 T1 h else' L0 B- e# R% S2 y P2 l2 }& O9 I: Z
{ if(a==1)
% n( u' X; R+ x4 c# { { printf("11");
( x. |4 J( |1 e3 Z' j4 Q i++;) _. f9 l7 Q% I) \4 H
}& _3 L7 t2 \' E7 h
else
- O2 p; c5 U4 t6 [: q: b! M { for(j=i+1;a[j]!=a[j+1];j++);: x) N& y% N* j5 c
printf("1");+ ]4 X* D: t/ b% v+ D& m
if(j==n+1)
/ N1 h! }4 a; u: ^, |) f( S- V' I$ q j--;
# \/ o, o8 _6 ^; K! } for(k=i;k<j;k++)
% a% N! Y0 v. _ `# W printf("%c",a[k]);( E( z" X8 x# m% L7 t* c
printf("1");2 z" N( j: Z6 p" i$ }
i=j;
+ O \0 [7 |! g* l/ ~ }, [+ i q, {# U9 q3 H: k0 @% R! {
}
- H b% }4 u$ ` if(n==1)
4 d v3 j" M" a! ^& X1 W0 a+ F# Q/ `1 o) P if(a[0]=='1')
' t0 [- Y; x* }3 J. R( g# h printf("11");
# i, I* T% K( Q6 ?- M; j( P else9 V3 C* u: u0 h! O' l- h* F
printf("1%c1",a[0]);
& H5 @7 ^ v( T0 R' t; Q printf("\n");
: |7 y" j7 q" N0 I1 Z) l }+ Y5 G; c+ H" ~- q* F) P" p
example3:#include<stdio.h>& }/ `! Q' h, S8 E
#include<string.h>
6 U, k$ E9 ~2 Avoid main()
! ]$ ?$ ~/ Z5 \2 f6 t- N+ f8 H+ V{ int i,j,k,n;
' h2 ~5 b: U7 t+ B. V char a[50];
3 k$ ?3 T1 _7 C0 p gets(a);! ?8 t/ i- j+ X( g, ?- g8 N
n=strlen(a);( ~7 _( l6 I! a9 r7 p
9 }4 ?6 y. e: h, v for(i=0;i<n-1; )" P! K* o8 C' B2 g
if(a==a[i+1])
5 O" G1 n1 C/ Q$ w5 T { for(j=i+1;a[j]==a[j+1];j++);
9 v. R8 R' `( ~& N/ x$ H printf("%d%c",j-i+1,a);9 e7 K% _, m$ z+ v( W" _3 E* y$ ~7 M
i=j+1;
- J. Y* s& G3 `3 e }
9 Q$ d6 q% Z8 ?' z4 a. g# l else
; N# Q; R+ A/ W- B- L { if(a==1)
6 c: l2 k( j+ h { printf("11");+ \3 S& ]3 {( f: W+ d, i
i++;: z% S9 U7 ~% w+ O* C
}. V$ P4 U; N8 O; {
else' X* ]' c! ~0 M, ^4 H' N) c v/ H
{ for(j=i+1;a[j]!=a[j+1];j++);8 J% E1 k$ o; v7 j+ L
printf("1");% X/ s2 A& D1 @; j& {
if(j==n+1)
- M- q8 V( z! ^3 ?6 S j--;
4 v! o2 D7 x1 M for(k=i;k<j;k++)5 y, `9 z/ {- J9 r. H, ?/ H
printf("%c",a[k]);7 t- H2 T& ~' H; I1 S% ^
printf("1");, A5 {, V- V! D. Q. e b# _
i=j;
* T) G, \0 [% j- d6 k8 F }
1 S, L* s7 n& s& x }- c7 v' G7 A6 _4 V' d2 F
if(n==1)
* V$ x2 a# l; x) K& Q5 t& e if(a[0]=='1')% }% j% C2 W7 P; c4 D7 X: b( ?- f) ~/ I
printf("11");
4 k1 {* ?6 p S! y `/ l# x else/ S+ o! j! b, m: [; b
printf("1%c1",a[0]);0 }7 l& ~ P% ], E
printf("\n");
9 ?, E5 f* k4 e( ~& G7 `! ] }2 R5 b0 W- C- }( f$ I
来源:编程爱好者acm题库 |