Run Length EncodingDescription
) _' |0 V% b; C
: N$ d3 B8 Q( Z& r9 |5 ^9 ]Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. ( h6 j' g% f; ?6 d" _$ J$ H& v
4 @/ E' {8 ]. c& f& _; lAny 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. ' m) @$ M. Y# Q# ^4 q) @
7 q) Y N0 l5 F( p# K
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
" F8 q1 n' }9 ~sequence, it is escaped with a 1, thus two 1 characters are output. / w3 z8 a1 j$ }4 W; p
! L: Y D5 F" k" B* T7 s
Input ' s8 k( D/ u* N a" F
1 W& J# E3 E) Z$ e3 _% 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.
2 i3 G4 O& t) @# Y" v3 |. t
4 Y4 s- n" ^7 HOutput
% c% ?' E6 b2 E2 l% R0 B
+ B& g3 L- l @6 p4 P8 tEach 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. 4 w# I. n# k* T' ^; w5 L
/ L0 r5 `- x$ `& X6 n/ m输入样例 ) q5 _ K; C& W9 Z) t
AAAAAABCCCC- n: R1 F1 L( x5 v* x/ r
12344
5 \0 o2 d, P' h, V* G% m% w
2 @, j; W" N& ?2 `3 ^- t: h& R; Y
$ ^7 L; @; R8 }- Y3 f/ G输出样例 : D7 T$ r) ^$ W! V q
6A1B14C( D0 Y; m' i, i* g7 `. P( S5 f& ?
11123124 Y3 O( I7 Q7 c M; J
. v7 U5 K8 `" c1 g3 t
1 Q |6 Z- f' Z( ~% C1 W$ P8 cSource
8 y5 A$ G4 ], q7 T2 L" {/ x( }4 V5 R7 g' |* d- s& e
Ulm Local 2004 w: T; m9 H1 x# w# t0 G8 D( _/ d
% p, |+ h6 r4 q+ P
example1:8 T8 F9 X( ?) B" s+ Y
#include<stdio.h>) L& v |' D/ f* Z: E/ i8 T
#include<string.h>' E0 c- U2 {. [# I" T" w" `
void main()
* i- n0 t: `# `, A# v{ int i,j,k,n;
3 B2 G" C5 P' P+ ?* Y char a[50];+ V) Y1 Z, V' A2 {0 v( \; q
gets(a);! c& p% i9 Q2 H0 v
n=strlen(a);1 Z5 V9 c) @9 L) Z5 J! Y; W( W
, L, m* F# V/ m7 ]! \/ N, \
for(i=0;i<n-1; )
0 J5 e9 z# |- B3 { if(a==a[i+1])( b9 A* [& Q- z+ @
{ for(j=i+1;a[j]==a[j+1];j++);' x5 P: g( I5 V: e4 g3 G) q
printf("%d%c",j-i+1,a);% w& n, P- Y7 Y2 m% D; j+ O3 }
i=j+1;
* V1 _. `$ ^* \4 G( |/ u1 N }$ m" q1 B2 a& q7 Y+ w9 f
else+ Y, h, |+ e q7 K/ i( o6 w
{ if(a==1)( W% _* }- c% C8 F$ i) R. ?
{ printf("11");
) [# x3 I! m( o0 y) G2 w6 Z i++;
- f( M5 p @, I8 O, j J& S8 h$ I* l }6 _3 t8 n% {' p6 h- [3 O
else V7 Q4 Y! n7 s
{ for(j=i+1;a[j]!=a[j+1];j++);
, d* _/ ~6 ~5 u9 P- X' e printf("1");) T; c% s9 |0 {4 T, _8 n' V
if(j==n+1)9 }; _, z, S$ h8 \# d+ B0 h
j--;4 ?& b+ a; Z: \0 X
for(k=i;k<j;k++)
% G* [( A% B+ w' D/ t% m0 ~' d& ` printf("%c",a[k]);5 h4 T! M7 }- m5 k
printf("1");
, {+ ?, R0 }! o W i=j;2 N1 S) Q D$ {# T
}6 r# I' \$ M2 @7 o9 e2 [+ N' ]
}& y+ E5 h, \% e K) M6 A s
if(n==1)1 H- w) r. E5 M0 D% X
if(a[0]=='1')
' y% {. C/ c4 |) q printf("11");
" }0 ?1 c* ~7 T3 R- h" |$ p4 J$ B else% w4 f2 o( n$ ^; x' C+ |
printf("1%c1",a[0]); s. D6 f& L' o. A P# |, e& v
printf("\n");# @8 b* H) e( O9 ]
}' s% k2 T( Q3 G: }$ L" E0 b. v
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>6 b; v5 n7 S' ]) I# |# m3 Z
#include<string.h>
g# y2 H6 }4 p+ Pvoid main()8 H: V* m4 e% \# M7 d8 v
{ int i,j,k,n;. [3 Z/ f( M, _& o. l I9 G+ D
char a[50];$ w4 [$ t- } W7 {4 |9 t
gets(a);# p( Z; b4 g( _% S S9 G
n=strlen(a);
6 }" H: X. X0 t6 |3 G; }0 l2 |8 [- Z }3 N% K. D
for(i=0;i<n-1; ). W4 P. X9 `9 l& ^4 U( W9 R
if(a==a[i+1])
5 x. n+ ~ L% x/ F { for(j=i+1;a[j]==a[j+1];j++);
; _. X. T/ g* C/ C, R9 k9 @ printf("%d%c",j-i+1,a);7 M; Y# v" V% ?; Q. X$ c
i=j+1;
; T: m: Q! Z& O( Y, u }- I' V9 f, d m' ~/ g) j
else
+ I9 \7 \- K; y8 Y# P { if(a==1)1 I2 \ h0 p! v7 e
{ printf("11");2 r1 p, ]# m9 L" A$ `. q6 V
i++;
" k6 b* Q& a* j" F }
# D. B0 j, C( b1 `) K else
! B2 N! K3 r( ?/ r- Z9 u { for(j=i+1;a[j]!=a[j+1];j++);
" g* B4 V3 w+ R printf("1");$ M* S% X: `5 I0 t/ F' U
if(j==n+1)
: Y1 p, [) h5 a r2 F- E j--; K# K F$ y) m& F0 ~0 e, o
for(k=i;k<j;k++)
3 }' L0 J9 M* R p printf("%c",a[k]);- C. h& |: Q* j' K0 @, Y% d
printf("1");
9 u2 x& I$ H$ S( i, t i=j;( e( Y3 d% Y6 ]: T" m. x1 B' A
}
, {/ p* E6 f; _0 \6 U }
& T. D/ t7 [1 Y0 T& _4 I if(n==1)
2 u. Z+ k4 G: `/ U* b" P if(a[0]=='1')
% |) K- @& c2 \ printf("11");" X, H- @8 m I- e
else1 P x U# L$ }- I( m0 d) }( Y0 Q
printf("1%c1",a[0]);# w/ W( @% p; E( p$ u
printf("\n");
D% y7 Q& j g! U+ D } example2:#include<stdio.h>0 B9 |( y. r! i& s
#include<string.h>
+ s( ` q: k) `2 G% K4 X# nvoid main()
7 u) E4 Z; n$ ?5 i' `{ int i,j,k,n;
' l8 n& r7 ~8 m% `: O char a[50];2 q6 `1 y% M: O; R, {
gets(a);
: z8 |3 e9 `: a- k' K1 |( U n=strlen(a);
/ P' j. |/ s7 i% o0 w* K$ E
5 q0 U* ?5 [5 v }+ u for(i=0;i<n-1; )
$ ^: D7 e) q2 I if(a==a[i+1]); a- t$ t6 n9 z, d- Z
{ for(j=i+1;a[j]==a[j+1];j++);
" o; |) M7 l( u6 K, ~; M printf("%d%c",j-i+1,a);
2 P$ g' L* s1 s i=j+1;; k9 L" e) {9 S* M
}( X: ^ O+ u' C( u# J, F& h( g9 p
else
" g2 |/ x% y O- ^; a7 \( q { if(a==1)
' G- w0 h4 {! o2 g% W { printf("11");6 e1 Z N: P9 ^
i++;- [3 F7 B7 L! y5 [( o
}+ N4 j! Z- U# `1 }' O) N2 X& e
else& N0 ~! `- b$ p) x' ?. { H
{ for(j=i+1;a[j]!=a[j+1];j++);
' s! ` I4 A0 H! J0 K printf("1");5 g& \8 v4 ^$ x2 R" ~
if(j==n+1)( \9 v( _: ?$ w: V, ~
j--;
9 v" k% c) I, _& z for(k=i;k<j;k++); @! U4 O# A; C
printf("%c",a[k]);, R! `& n* t5 Y" V9 J# Q
printf("1");, _$ j& o! r1 y; \
i=j;& I! d! Q- F6 ^$ @4 F* v" {8 W; F, _
}
1 [& Y) O( _7 g5 f0 H }
/ N, k4 M' ^+ u0 L i' z$ J if(n==1) ?* i: p+ O- Z) M
if(a[0]=='1')$ } j) Y- b" _1 I
printf("11"); X- J) Y O1 Y: H& r: j0 }1 o
else- L8 u: q8 z+ w1 w% X- m
printf("1%c1",a[0]);
- a/ e/ e3 Z3 d* W! K3 l2 q printf("\n");9 d' B+ n# \0 O0 f# Q& t2 T' U
}
" h9 Q. f. ?7 G) v example3:#include<stdio.h>
" o+ d1 A, d$ _1 m- t3 T# M#include<string.h>
: Z) u8 c3 ^1 D6 x, w* u3 hvoid main()& ~( V. d9 d% G7 x y; w( U
{ int i,j,k,n;% W3 P6 J! \- q0 Y/ k
char a[50];
; V* \( h6 f# M2 d gets(a);
2 {$ @, H9 @1 o% l, ^ n=strlen(a);
. }1 U. ~2 F: Y' d v% T( D7 Z9 ~* T9 O
for(i=0;i<n-1; )
) I. F: J1 I$ O! k+ n! J! [ if(a==a[i+1])- q6 _5 p& N( U. P- z( i3 `# }
{ for(j=i+1;a[j]==a[j+1];j++);5 X: L r' ?4 ^$ r; e
printf("%d%c",j-i+1,a);
2 Y' l$ E1 d2 S" g# F i=j+1;3 q3 c: C( u" ~* A& E6 p4 O
}: A2 e* s( V6 J( j/ R
else8 f; Y5 P" G5 U/ ?% T; b
{ if(a==1)
. C; J3 ?% n. J9 l0 y4 d { printf("11");# N0 }! k3 H, M0 \1 j
i++;
$ `3 p5 y! ?8 b2 s7 z# H }) k, I4 _. Z( ?
else: F2 P7 P; o9 p- ^+ b% a* U1 ~: k7 i
{ for(j=i+1;a[j]!=a[j+1];j++); ^, G2 U+ b! p* C; C" s" v
printf("1"); N& U3 y5 m' Q' f3 j
if(j==n+1)
" v$ ^& y2 _* a/ V( Y j--;% K% C# ]/ v( x- ]7 V
for(k=i;k<j;k++)
. a+ i) v, K8 x4 D7 B; |" \ printf("%c",a[k]);
) P1 `& t# j4 j2 U3 O printf("1");
' G" B- Z5 A6 G$ L* w M- O i=j;) w6 r7 @( d o! q- ^* ~. ^; R
}
3 n' O5 ] U7 [4 Z; e5 I$ W, B. M' f* r }, O" s. m# u9 r+ L; H: ~, A& M5 K; G
if(n==1) M9 ]& a+ { f5 `( Q
if(a[0]=='1'): D2 `0 T+ M0 u& p S
printf("11");) m& s! _* C6 J
else
. _8 H$ p3 j1 @+ K printf("1%c1",a[0]);
/ [: Y0 `) M/ I$ ~4 ~! }8 F' Z printf("\n");
2 s3 [4 i0 }6 M2 B1 y' {9 x! Y }
( P2 p# U. t$ y0 C 来源:编程爱好者acm题库 |