Run Length EncodingDescription - [# |' R0 }. O' g1 ^5 ^3 n" Q
. D* S% @+ ~ i4 q; R) s6 l
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
( W7 B/ w) E5 P1 t( b2 |/ J# d1 i& v* \& v: L
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.
3 m* @# M0 n* p& z! h! `, t L/ W: \! s! s# ?' P. k# P7 t
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
B. e8 ?7 E; Rsequence, it is escaped with a 1, thus two 1 characters are output. ; L/ u& J# g: o4 R* |0 R0 Y
6 R2 B# A5 ?: ?Input
$ `0 V" \, H- P3 u) ?. \8 N
s4 p( K( Q; Q. T% oThe 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.
" h# o1 R+ o1 W/ w. p7 \
; k" w3 O( {8 z2 [( bOutput
: S( H3 o2 @, Y+ `2 B
4 J ?3 K. [% J7 XEach 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.
; s! f5 |- W' t( Y& D( }" U. J( Z. d' q3 O4 ^: H
输入样例
! b; l3 H) e: s6 A/ e2 y1 ^, sAAAAAABCCCC+ p. X6 l E; m) y) _6 [
12344
: L: P+ _6 r ?; v: |
y' C% A0 C. w( {+ |8 A) s/ h" P- x
输出样例
6 w" n9 E1 _+ {3 }# i) g" V6A1B14C
4 ]+ |- l0 V& n! ^ b11123124" V# Y9 g% x! ^ D7 M* t
. Z3 s& G2 H/ T! K- x2 k, f8 k6 [% H ?) |, k5 Y- B, }# C
Source
$ g- E" j7 d& e5 ~* m6 O5 R* Z9 u A) X& k1 ~# G. `
Ulm Local 2004
; I! |$ K; F% G0 N& Q4 W5 Q
, U+ E! H' U; ~! T3 b& ~1 @example1:
" J v1 a+ Q: y; i& @) q#include<stdio.h>+ J% c1 D0 ~& A2 _! W+ Z
#include<string.h>
% I: X$ g5 S" u& k% Y) {& Rvoid main()9 p* w4 u8 |7 v) u. w! V
{ int i,j,k,n;
! h( O2 K- L, I4 G" w6 I( M9 G( B. M char a[50];# s* x- R! h: Q8 l7 D
gets(a);
% H" U3 q( d% L( q n=strlen(a);/ |5 H$ n9 M S2 ]# p1 W& j
% t& e# ~( _1 W( L- ^ D1 H, f* w& C
for(i=0;i<n-1; )8 s' L9 l" Q4 Z% E, L, X: O
if(a==a[i+1])
% P% _, k0 m( } { for(j=i+1;a[j]==a[j+1];j++);' I8 l* H. E) R- \& S D
printf("%d%c",j-i+1,a);! B/ |/ y' R, v/ F- q; m- e7 G
i=j+1;
+ G0 ~4 c, P7 ]' b }
) v8 N1 b' ^' Z/ H8 \4 l+ i; V else
, ^2 j" B* c7 q3 o c8 ]8 _* ~ { if(a==1)7 ~; v+ t" e$ c6 A+ L
{ printf("11");: o/ p7 s, M2 E P* f" E
i++;
$ I8 [1 ]0 |' n' Z! d' r5 J, L8 M. e }
9 z* F/ q1 U7 N5 m( a! C8 t0 n else
2 {/ u! q8 Q7 O8 @8 ~ { for(j=i+1;a[j]!=a[j+1];j++);$ U( v( N5 O2 f( f4 P
printf("1");! J( b$ w: G) H |6 \
if(j==n+1); P3 D; M3 N: O3 b. @. b1 H
j--;6 d) b3 j. D) U; t6 R/ ^* _( o/ |
for(k=i;k<j;k++)
7 r2 X1 O+ [% f/ l6 m9 R4 g printf("%c",a[k]);+ t" s4 V1 H) G( K1 P& ^
printf("1");
S- j2 N. }3 R9 w8 |6 T i=j;
1 t2 D* E' K3 c* h# T3 c' H1 C }' X8 ~7 g/ [+ v+ t# i/ |8 f
}/ v3 [ p9 ]# L+ i; r# ]+ E
if(n==1). ? d" y0 C" l. c( ^
if(a[0]=='1')
8 l3 l7 T5 G ]! x printf("11");
0 a0 d6 g, ~7 }2 K, c else$ J1 k' A5 i& h1 }8 h& T# K
printf("1%c1",a[0]);
# N4 s* k$ o+ I' U: B printf("\n");
6 M4 A/ i$ P4 h; |9 z2 K }6 w! R* S3 h0 ]
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>+ J) p2 |' Q0 W1 W
#include<string.h>7 t8 }$ r9 V$ @5 b
void main()
; c! V4 O1 q; l \{ int i,j,k,n;5 A a) C% |. Q& X$ v8 ?7 e7 I
char a[50];: r2 X; Z2 \' o' s% a
gets(a);
, i U- u0 V7 `3 a K n=strlen(a);
: Z9 |# Q' C6 q `' [' _; f+ T8 ]
- H- j. i" m4 _. z for(i=0;i<n-1; ): |- d$ R5 o+ p9 V! i
if(a==a[i+1])7 Z# s2 Q) D& R4 P8 o
{ for(j=i+1;a[j]==a[j+1];j++);$ p# d+ U4 g! P$ I1 \
printf("%d%c",j-i+1,a);/ f" J! s' ?$ D' A
i=j+1;
R7 T9 A% {+ O, U* a }
& y7 S+ }( G6 Y else
7 O) D2 w* X( v% `! C3 o* @ { if(a==1)
, `7 u: g/ q: p: o, T% G9 g { printf("11");
) l5 g# j5 q {) \' q% E' m( R i++;7 H$ O: g# \2 s, e7 W. S
}
/ d b4 _- n/ C7 h1 |6 m7 l else
. u2 U! |' K3 C: C { for(j=i+1;a[j]!=a[j+1];j++);( U/ e7 ?& `/ A" b! i& y6 @( r
printf("1");9 L' N2 L/ \; b& Z+ ^
if(j==n+1)
$ [# h6 p, u S8 v5 m j--;0 ?% y8 J5 H l. ^( A7 O3 e
for(k=i;k<j;k++)
! n) F- A X9 o% M' v printf("%c",a[k]);9 i* _. a1 ?1 B. R/ m. a0 t3 Q2 T
printf("1");
) D- D/ B# M0 f% e) j# ^9 q i=j;
" t" a* G3 f) A }! w7 T7 _- M( b! t% N
}! _) P9 A% T, |. x+ h
if(n==1)
9 D7 F9 x% Q5 S' b, q3 l5 H" f& K/ G if(a[0]=='1')
' s6 Z5 ?0 T6 m4 A# Y9 b& ]1 W printf("11");
! D. {3 R M Q- _5 J else
' W8 Z- k) f7 N printf("1%c1",a[0]);; p3 G V$ w' f) |# \$ X$ \& P; t* J
printf("\n");/ u* F) X. M8 b1 Z5 n
} example2:#include<stdio.h>2 @ O$ \4 b. U1 B) d. W
#include<string.h>4 D5 K3 \ E3 T# E
void main()
4 j7 Y2 H4 a3 A{ int i,j,k,n;* `" }2 d9 W/ y( }& f
char a[50];8 T0 v, U7 V* w
gets(a);( B" x: {, L! n" f4 L, H
n=strlen(a);/ F3 j2 p+ Q1 Z* u( g
" l7 \. V( r/ T$ a F8 v for(i=0;i<n-1; )
1 E* @9 K) M5 l* K& K) X7 x5 s if(a==a[i+1])9 N7 F/ l k+ p. N2 x0 i
{ for(j=i+1;a[j]==a[j+1];j++);0 U+ c1 J& [* p' m/ s/ P
printf("%d%c",j-i+1,a);
0 z) S2 D' j7 z" O) B& g3 ~ i=j+1;
" l6 f6 n) X9 h9 e4 z4 S9 L }1 d X1 n" M g2 w4 n
else( n0 c9 ?0 b% @8 }1 @; f
{ if(a==1)
3 C3 `% Q1 K3 Y0 Z6 q$ k { printf("11");) s0 y! h) j! F! d
i++;. v8 [& Q0 o2 C, Y& ?
}
) L1 p' { D# Q" j8 S9 ~7 w5 X else
! f# L0 i+ y( y& v1 { { for(j=i+1;a[j]!=a[j+1];j++);
8 i: u: Q5 @- }, ]8 m3 ~# n4 L/ Y# O printf("1");, V5 ]& q; r1 n
if(j==n+1), }$ r! l% q4 V; V4 ~# F6 W
j--;
; Z: Q( | n! k( M: ], } for(k=i;k<j;k++)
) W- T0 [& S8 L) k! a+ y# F8 d printf("%c",a[k]);
# V/ j4 r9 t s; R* Z7 Q' o6 m8 j3 @: q printf("1");, L3 O; v, M7 M" i4 V$ c; u2 X6 ?
i=j;
" j% N7 }, H( B9 e% G" S& j- E }& e9 u; {4 d6 a$ v' U \' t
}
* W2 D4 t* b* k6 n. T; B. i* \ if(n==1)# R( O! L: ?2 F0 ?! o4 e/ k2 E
if(a[0]=='1')( L4 N& q1 O8 ?2 N! K* z5 g
printf("11");
8 i/ T4 m5 H5 g+ ]8 L, H else4 N5 `) E- B6 V5 O1 E1 Y' x
printf("1%c1",a[0]);( H5 b" ^) U# e! m( o. s
printf("\n");. V) U6 f# k5 L, {: F+ @! o
}
* L8 i/ ~9 E: c, [% _2 s; R& \ example3:#include<stdio.h>
' k3 l2 F9 m9 N. |0 }. a, s% [#include<string.h>! H; _/ X9 E* A; X, g
void main()
4 S1 ^3 U+ m9 M( b" a& `{ int i,j,k,n;% K i/ @4 x% Z) b
char a[50];
! {! g6 c& ^2 @$ F1 `! a! L gets(a);
/ J2 Y1 \ y- s/ f n=strlen(a);
# {: E; a# T( C# t7 D
! P5 V4 C: R* L1 b6 Y+ n+ n0 j for(i=0;i<n-1; )3 z3 k- a+ U0 _. Z+ R0 f$ ?
if(a==a[i+1])1 H& X: O! P& _' e
{ for(j=i+1;a[j]==a[j+1];j++);) D% q; B2 i5 z( L% [: u% a8 |
printf("%d%c",j-i+1,a);
" i9 W7 j W& v& h8 z# s9 w i=j+1;; ], [ l [) u
}: J- z9 S$ J5 x, {" @/ }' H
else
. z0 N r, o# t! H7 O+ L+ o { if(a==1)
9 o" Y' }* {% [" r0 f { printf("11");
6 ?4 ~: e; y5 K" b8 a; P i++;* r7 |4 j( J& O" N* B
}
6 q6 i0 Y( U% u0 a( c2 I3 a else* n u8 }& {: G& Q/ P6 m$ P/ u! c
{ for(j=i+1;a[j]!=a[j+1];j++);
4 y' e5 t; B: y* k4 w printf("1");
! o3 F4 X/ _/ c+ m8 t if(j==n+1)# H, D, `9 D. O, P/ ]7 e1 P% ?9 A
j--;
0 M; h5 f0 @) B+ M' J' J for(k=i;k<j;k++)
" {0 N+ }# N2 j9 J/ x& {" W) S printf("%c",a[k]);
: G) ]. o t) I" P9 J& Z" t. G: P' T printf("1");8 u D4 Q9 G' ^4 f
i=j;
9 h& ?- z" S; }* F }4 ]# {# s- j0 l! C r
}
8 l" t; ?) A- t: k* s0 p N6 H if(n==1) Y% O" Y; k4 z! m
if(a[0]=='1')
, z; m) A! O2 c" i9 j printf("11");/ ]* z& V. ^! k
else: Q* h0 P+ _4 F& x; q( K
printf("1%c1",a[0]);7 W; }3 Q/ ]# C7 f5 J
printf("\n");8 d- a" [# _& h9 X! C
}, }5 P# D2 i9 D$ Z7 }2 U
来源:编程爱好者acm题库 |