Run Length EncodingDescription 1 m, R2 a& `* m& a1 c; O4 \! e( v
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. * u( G" P8 t w: b- |$ y% l2 b' B " K: q, }" X U) J" C. \$ QAny 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. # x3 G& }0 ?6 @! ^/ h! ~) X0 S6 q1 }5 v* y
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 + f; J6 ^( T0 d/ _% Y& _2 [sequence, it is escaped with a 1, thus two 1 characters are output. ; l. Z/ X& a$ q& F6 ? 8 C% D/ B! q. f$ _. p+ H& `7 qInput ' m$ h2 o' H; C2 R ~
$ N! S/ G' H! \8 Q0 r8 i
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 H1 W# T1 h3 C
8 T. v: U& j! q! COutput + h7 z: V2 f$ }( e3 P9 p; b
6 U+ B% c! s& c! L% o1 Y# VEach 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. * U+ y% k( I9 q! j( D! p; g, y# M) S; | 输入样例
& z$ Q6 B" s( i. i$ ?& i
AAAAAABCCCC ! ]# Z4 s( U0 O K123442 v4 X$ j+ {) r& `0 i7 N$ D
! ]$ T+ [+ _7 V' R# T
4 o$ q! j0 N; Q, x! d, j9 e 输出样例
6 v% M! C& g6 G; z9 V/ Y% A
6A1B14C 3 `+ n# M& k% V( C* m1 L4 s11123124 M0 A* s8 u9 B) F: G
+ H8 ^$ C- i4 H6 J* d; z
, i n" F/ N4 p8 b2 sSource . i2 t2 D7 I& L7 [: k, r/ c) v6 ~1 {+ k" P% [6 s; @, g
Ulm Local 20045 X w4 ~0 B) S L2 V
3 [6 K5 X5 F O# W$ {' G; }9 |example1:$ y9 V& w! `. D! c* v( `; s
#include<stdio.h>1 p& M- z" n! h7 i3 v7 q/ ]4 @
#include<string.h> , B9 f# M1 F* ?1 kvoid main()& h) l3 q8 Y% `1 r1 \
{ int i,j,k,n; 7 k, M. u C3 c8 S: w char a[50]; U" w' D" ~7 v" I4 D gets(a); 5 ?8 c) L) u4 l, x2 ?& c n=strlen(a); % L' [8 l) |( Z, v: m6 B2 q% ?; X& Z6 O7 m5 y5 I
for(i=0;i<n-1; )+ r* i/ W7 }: V3 e* Y
if(a==a[i+1]) " r" ^" h( }% ~2 O; I+ ^$ F& L { for(j=i+1;a[j]==a[j+1];j++); * c% S7 J0 V" g& S printf("%d%c",j-i+1,a); 8 q$ A' Q% ~3 P7 t) y2 I9 M9 k# [ i=j+1;7 Y/ `5 p D- d# S) K$ ]5 I
}5 P" P! d0 r' {
else - ^, ~. q+ Z' n { if(a==1) ' C4 f+ S7 r5 L8 i; V# j Z { printf("11"); 0 _) H8 a/ S: @; j5 q/ y" y i++;: U$ a9 y6 l# ^/ J; r# f
} / R% B& ?& G" V else; j: r) E0 |- `% M; `
{ for(j=i+1;a[j]!=a[j+1];j++); 6 @" R, c) s6 ?6 A4 n7 g' h printf("1"); ) w( W8 R9 a1 D3 N if(j==n+1)" S6 s, g2 K; T4 H7 @, s
j--; O5 U6 Z" S- I5 ? for(k=i;k<j;k++)' t$ Y! n9 r9 R; c* G
printf("%c",a[k]); ' L; _( [. _9 K% l& Y$ G printf("1");6 s k5 y# `. H" M) [8 J' }/ ?, B3 \
i=j;4 U! u/ s4 z9 W, P
}; _" d# `. x, u1 O) v7 P1 q
} 8 ^1 @& O2 h+ X* N9 g! p if(n==1)' K, J! o9 D. J# E; W
if(a[0]=='1') : L0 O0 ]' k5 f; l printf("11"); 9 Q7 C* c- R5 C: L2 F U5 \ else ! U m7 _( [. c printf("1%c1",a[0]);5 P- ?/ q6 w: L
printf("\n");1 g8 E R" A! _8 c7 X2 M
} ! a( k+ z8 e% R% b评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>( s% h, s5 u8 C) w
#include<string.h>1 D( K$ U! D1 w* Q9 H/ B {+ r
void main()) Y- ?) d2 r) m' n& \) ~0 |
{ int i,j,k,n; 7 ]' I* R' J, y! b char a[50]; 1 B7 R7 X3 H! T gets(a);0 z4 N0 y4 A$ @" ^2 o4 |6 ]6 x; B
n=strlen(a);- P7 B+ w: f6 l2 k, c( R* F: u
6 f% m/ J4 x( v7 A/ F5 ^# x8 Y for(i=0;i<n-1; ) ' X& f% b& o# S8 P) J if(a==a[i+1]) v+ l- P, P, G5 j8 q* _
{ for(j=i+1;a[j]==a[j+1];j++);4 j7 F7 {" \1 L5 p( B
printf("%d%c",j-i+1,a); {( Y1 g4 t! a/ |) x" M) N/ `
i=j+1; - _' H# F. x* `' r+ y# ^7 s ~ } ) r1 T2 m3 f$ E) G0 W: s4 x else" Q! R* M/ `( g
{ if(a==1) * N1 u3 N7 z! d# ^ { printf("11");' w4 }+ e& A4 w9 S4 E- ~8 ]
i++;6 U( @6 B2 s3 B! o/ ]2 `% C
}: y% y9 f! ^8 A7 j. m9 \
else3 z# J" S/ x9 p) K* r/ `
{ for(j=i+1;a[j]!=a[j+1];j++);. p* P; x* V# H% T2 \( N: T
printf("1"); 7 Z7 L( ?# J, P0 ~- q if(j==n+1) " s5 d. M5 m$ ^- a5 C j--; 9 }/ e# H0 q j( L0 P! ^ for(k=i;k<j;k++) \, P( T8 T( k# t printf("%c",a[k]); ( \& x4 H5 }+ S& M* `- h& A \ printf("1");! _: [7 [9 X: P* J( M: d
i=j; 6 N* \3 C8 i, b9 a } , m% O% G3 c, h$ D } g4 l8 y; G3 G* v- \6 S' m if(n==1) 9 N# ^/ _$ ]( a3 T& N7 g* v, D if(a[0]=='1') + |( N6 @. ?$ v9 G printf("11");/ k( Y3 Y/ N2 k. W# E) x$ A
else$ h/ A8 f8 k3 t0 F! }6 F; {
printf("1%c1",a[0]);$ T5 W/ Z- h9 S/ x
printf("\n"); 6 g( D* y$ e4 T$ P% g } example2:#include<stdio.h> j6 j2 E+ A) _! }! t( V
#include<string.h>/ U2 B7 x( J% ?) Z+ N# W; t' ~$ g$ Z
void main() 6 D3 r( F7 u3 [; G2 ]{ int i,j,k,n; , s0 f$ ]; G4 I1 i. L; P8 | char a[50];. g2 U" Q& v6 G, ?" Q/ R) |$ h
gets(a);* d p$ X' d4 X! ^2 l; F9 U' Y
n=strlen(a); Q4 \8 p4 i! M# a0 @$ o$ D5 D7 h
for(i=0;i<n-1; ), k h; Z! Y" [. w( Y1 v
if(a==a[i+1])0 k7 a% \* ?+ Y6 L7 u9 q7 Y0 P
{ for(j=i+1;a[j]==a[j+1];j++); ' @; j; E$ f Q5 }5 Q printf("%d%c",j-i+1,a);! v% ^, E( h" c9 E" }5 m \# \
i=j+1; : z6 \. T. E+ U3 x8 O$ J0 E } & l$ W: _, ~* Q: [) x7 b else * Q; ]! {! C0 k9 I3 j: o A2 w4 ^ { if(a==1)' A2 z" a, E' b/ d3 y
{ printf("11");) ?' q% Y6 n8 k) D$ I% y
i++;# W/ L# j, ]+ C+ e! w
} . x% [- Z( R2 Y3 Z6 K; v$ I. | else 1 _- {9 T1 ?+ u- N8 {2 g! H5 O) c8 h { for(j=i+1;a[j]!=a[j+1];j++); 0 C5 Q S U. x }, n5 o printf("1");! I- h7 {8 d% t# W" \# Y
if(j==n+1)8 i: a$ ^( A/ L* G' _7 R2 [6 m
j--;' D+ w( z9 m3 P6 A" s: K
for(k=i;k<j;k++) % j F% c' [9 M" a' l: C printf("%c",a[k]); j$ C _- q9 K C/ h) O3 a
printf("1");) R ?& r$ `- O% P
i=j; ( N5 X; f" n- l& {7 d }, d; D H# y7 R* x
} 2 h4 n' @3 g% C' D k. y if(n==1) 7 c& s: _- c/ T if(a[0]=='1') ( a- n1 w$ c& H- k printf("11");. z1 V" ], F1 i9 N, W+ ~
else- G# c' Y2 X2 T) ?* w
printf("1%c1",a[0]); 0 o* h9 \2 E; P, u8 u printf("\n");! \& F' _) g L6 Z- U+ O% R
}+ \' b1 D% B. Z+ n6 D0 ^5 F
example3:#include<stdio.h>" V, G' c9 f, A5 ?1 w8 Y5 K: V
#include<string.h>) s, a7 f: ~! M/ k5 q
void main()/ p; C6 H' H6 C, d
{ int i,j,k,n; ! g' i. f# C6 b$ l char a[50]; + D( I6 G- P K4 X8 ~ gets(a); ' N- g# u, P' r% v+ S$ [2 d n=strlen(a); 8 d/ k Z. i7 w- `3 [( W5 {' g( _' f4 ~ h8 X, W2 i& I5 L. f
for(i=0;i<n-1; ) + d0 D9 U0 I( C( u! o if(a==a[i+1]) J/ R7 p- ]( ]3 z# |8 X8 p { for(j=i+1;a[j]==a[j+1];j++);' j% y8 ^9 A p) t0 H: E# K
printf("%d%c",j-i+1,a);. y" O1 g: n" O! `
i=j+1;' `$ ^( k3 u* j' y5 u% ?
}9 b% T( i7 u/ k: \' D( U. T
else . E! O" ^; r+ i { if(a==1) v! L4 ^. B: `* g { printf("11"); - B, M0 J6 q! @3 }* ~# H i++;0 {3 r) p( ?; F* i
}7 U, m) f5 U+ H( L( F" }" f
else v S! e/ s9 y8 X { for(j=i+1;a[j]!=a[j+1];j++); ' H- t4 J# h: P9 O: L. t; [ printf("1");. _4 y( ?4 M! j0 [( C( m
if(j==n+1)4 p) t& z; k' |$ u$ m% z9 _
j--; " @( K' v. ]* J2 w2 W: I for(k=i;k<j;k++) ( a! g+ |, m; K% g printf("%c",a[k]); 9 Z8 g0 X# v/ V5 \. v printf("1");8 P B9 H& m/ R$ a' D7 L8 P
i=j; 9 q1 U- N; D( A+ M6 [" L } % Q/ p: l" C4 B- J, Y# E$ R }' n# d C( L2 n. j. D4 F' j
if(n==1) 0 F3 }' a" Z9 K6 O% H if(a[0]=='1')" ?( U- `1 X% I. W
printf("11"); 6 X" \" s4 g) P% m' W9 z else% ]; \8 E X/ M/ M5 F+ ]" s
printf("1%c1",a[0]); 1 y+ _! R8 b) H printf("\n");# n7 }! ]# [7 k3 |4 ~
}* G: s0 i% W) D' t1 ^- X7 Q
来源:编程爱好者acm题库