Run Length EncodingDescription 5 Y, g; G# _" @7 o# _/ M3 n+ v# F8 R" O; M q( _0 p
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. 0 ?; J( g/ E8 [" y- l0 J+ e4 Q6 T2 ?' @ g7 m# M7 h# 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. 7 P! c$ C* s" ?1 j
0 P1 ~, T( G1 P( q5 \# 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 - J5 q7 I2 g3 esequence, it is escaped with a 1, thus two 1 characters are output. 8 V& ?0 n0 C% w- N: h
, g, x6 x$ _. L. ~, [$ IInput ( l5 o3 Z+ V& |; `2 T: I$ G! R. }8 s2 k/ V; `
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/ X' y8 `/ S$ `4 a, G1 U
( A( R3 M; u$ G3 o1 x, ?0 q4 EOutput d9 [" [' G+ `: t0 m2 _9 v
1 W7 B6 X, M5 S4 Z9 m9 v) s+ h
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. . K% u6 v% t4 C: ?& y$ V" [ B2 C6 ] h! Q. o$ ] 输入样例
/ B& E2 `( G- x' n- i5 `7 C) U3 NAAAAAABCCCC8 @2 @8 {" c3 Y; [0 R
12344' u8 W4 u7 x1 Y L/ i
0 j4 N- U' G% x* o4 C+ t8 p5 r" D* y 输出样例
* V: w8 K3 _' t9 E
6A1B14C7 o' J0 |% g. l' Q
11123124. J& k) U7 w9 Q. z( Y+ a
7 u, i7 D- o$ r, D$ f; D# ~' V1 {- L: O2 y$ j. e0 X
Source % t. a' s r% @6 w6 o - ^' ]5 O/ N+ x; v% F$ {/ BUlm Local 2004: k2 e# p# e) c* n" j
! |( A/ P' a+ F! O" }
example1: * m5 {* n) u% u; ?# @' b#include<stdio.h> % c% @& w) `; V* K3 ]4 \) b#include<string.h> 5 q; s7 ]5 n2 r' s0 T# vvoid main() * z! r4 t+ N; }{ int i,j,k,n;) P/ e M* R# Y# Y- i+ k: k* I8 h
char a[50];5 H% ^) n4 f, u( ]4 [% Q1 F
gets(a);. n+ r. x. L- \4 |3 Q/ E Z3 K! t$ W- z
n=strlen(a); # w5 ^6 V; S* y: s, O- E 1 T. B T+ F8 h+ C5 W- c for(i=0;i<n-1; ) 6 z, C* s( O) s if(a==a[i+1]) ' A4 |" Z7 _6 B) g2 w5 g2 T { for(j=i+1;a[j]==a[j+1];j++);) ~7 |- ?' @3 p" T" @5 i' m8 [
printf("%d%c",j-i+1,a); 1 {! B) t9 w* Z1 M% V" i- l r5 I i=j+1;( ^& J2 C, ^: @5 F4 m9 f
}. {7 ~# ~& s& u8 L: |" a
else, K I: W* b+ V- @7 ~
{ if(a==1)" w/ k j5 v0 o
{ printf("11");; g, k8 E2 @+ t
i++; 1 D5 v2 O. h% s9 H e }1 c& g, \2 \+ H: _9 U0 W; a0 K
else0 D/ R0 a* w6 Y5 j; R
{ for(j=i+1;a[j]!=a[j+1];j++);" ~; K7 T* J1 |3 I9 z" M" {
printf("1"); ( v# @2 L2 ~1 \. N! J' J if(j==n+1) 6 }3 g& n7 u4 {1 F j--;( r- A. S2 u* x$ ^: P, y% h v
for(k=i;k<j;k++)* H! L1 h' ]& ?" v
printf("%c",a[k]); 9 D+ A8 k+ g. b& s' {6 v printf("1"); , j8 `3 B: Y" X! R/ d i=j;% ]# ]7 \& c! y8 M' J7 P( {
} 4 N+ t. s1 k3 y; e6 X5 ?7 l9 Y }( p X+ i7 t( J2 o$ f& m
if(n==1)9 g4 n( Z# r/ v# v; v6 O6 n
if(a[0]=='1') h# p2 {) S0 _+ M) D' U printf("11"); ' d/ }0 t4 P* m; j$ m" y' b/ a; p4 E else. s. t( B( ^; F; x8 A1 R
printf("1%c1",a[0]);% ]( z! G7 [1 Z% b' d
printf("\n"); - h6 R4 @- u3 l5 E# `9 s& B" n# L } 3 e$ y1 q0 Y/ e$ A9 O评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>3 ]- a8 d( O+ W. A
#include<string.h># ^2 e" W6 w/ I
void main() ' I9 `# c) Z, W2 {{ int i,j,k,n;8 L y# Y1 P7 G8 I) `
char a[50]; ; u1 ^7 I% e/ q6 `4 F: [( R gets(a); ( ]! }- L5 E5 B+ l2 ]: i1 q n=strlen(a); `0 o1 e2 Q: ?3 Z& G; o0 Y) f+ H2 p" S4 F5 }* j
for(i=0;i<n-1; ) J8 ]' G7 J+ }. S% ^: C" H! j
if(a==a[i+1]). O9 Q2 Y- {/ K( ~! {- T
{ for(j=i+1;a[j]==a[j+1];j++);! H& C0 [* I8 T5 x" g
printf("%d%c",j-i+1,a);" K p8 {! o" @7 n7 q' h: N
i=j+1;& J. C% A2 ]) U" a4 t6 Y
}; b* ^: M2 ]& g4 p$ u2 ~
else & ]4 b4 {8 Q3 d3 P { if(a==1)& |' f9 ^3 H1 j# E
{ printf("11"); ! w W* k3 R- o8 @ i++; ! E$ `) j$ i5 s- A; \/ ^, i- H }; h# {9 _: O' h! [1 i
else . H7 X- X+ N1 f$ O, I { for(j=i+1;a[j]!=a[j+1];j++); 7 C$ o) G, E5 I/ R5 c( E printf("1");7 O( H u" Y, n; [, }( g) G
if(j==n+1)" b! K5 H% n# ^9 R( e
j--; * y0 n% c! a$ u) q1 X for(k=i;k<j;k++)& L# p# d9 h" w. K
printf("%c",a[k]); & ?/ c4 T7 U1 N& ?! z- z printf("1");2 M( Y8 v; `4 T1 C) n5 ^
i=j;& k+ h p6 w. N2 d' b" C2 Y
}0 Q7 Z6 a1 j( k; F
}4 k/ t+ W* z% ^- L& `1 ^$ G
if(n==1)* G( ~9 w9 V; U! J
if(a[0]=='1'). A8 B8 ^* u/ m: M
printf("11"); - R# L, S7 W3 Y3 L1 {4 T else2 x% u Q' r( [4 N4 G- `+ ?; v' }# \
printf("1%c1",a[0]);- h6 \9 ^' T2 g1 i" g5 m" V
printf("\n"); X# w* X+ J% i; f, ?' ]: ~ } example2:#include<stdio.h>5 r2 n. G3 O8 C6 g$ Y7 p
#include<string.h> ; F$ T6 H) o7 L+ Yvoid main()' l8 I' C. d2 b5 @
{ int i,j,k,n; , J+ D$ N7 H! U8 C; G char a[50];3 a# V- V7 z* I4 u& W/ |! ^
gets(a); 2 q0 |6 T6 S/ F- ~* I n=strlen(a); . Z I- ~, W2 q3 d3 y. e; k; a 7 B1 }8 e) l2 Z; c& _' S/ O) h& A for(i=0;i<n-1; ) & n. @% Y' k7 f/ r/ j+ c/ Y4 Q0 V if(a==a[i+1])% V3 K; i: r4 [( X% }
{ for(j=i+1;a[j]==a[j+1];j++); 9 C2 O5 Z/ @ b' X# Q4 T9 }5 N printf("%d%c",j-i+1,a); # p7 W4 L% N* ^. L: g i=j+1;( I* y9 ?* O7 D. Z- K; Y* i
}- A- f4 v' B( s2 c9 [+ i/ \
else$ E) A# i7 t) [8 D ~1 e
{ if(a==1) 4 J8 l- E) S% G { printf("11"); 6 X1 i, x' [# K8 }7 j7 M/ l4 | i++;/ x8 F8 T& B( y P& M
}. G: @ a8 D- v5 A5 v: l- i( z3 \
else n q$ R- o! y# F
{ for(j=i+1;a[j]!=a[j+1];j++);; d5 l7 p: J* w) o. {3 R
printf("1");( G. x/ p6 D9 N" E$ c0 q4 v7 t
if(j==n+1) $ T2 H ?7 z6 f! H j--;. ~3 `3 y: y/ X6 F
for(k=i;k<j;k++) ( D# i3 ]5 ~: n+ h3 b. u5 { printf("%c",a[k]); [8 D3 i) i8 u+ Z
printf("1"); & h% X) }5 |- a i=j;7 _+ @ i& U! B2 | p
}. k1 m0 H" l- k" w }! W
} - Z$ c* g/ F! h if(n==1)# d: F: L* p! W) r
if(a[0]=='1') 9 ?& P+ P7 Z. T# i" @# u printf("11"); 4 E7 p3 l" b: J0 w0 \ else' e" v% U; B$ _. O8 T% O" r' j
printf("1%c1",a[0]); : m: y ^2 q# ?1 Q# ]+ f printf("\n");3 b% q% Z6 A1 y7 ]; O5 h0 ~
} . z7 h4 B3 ]+ ]9 b( r: T; [ example3:#include<stdio.h>7 s% `: S$ g- Q2 U7 l6 Z, H
#include<string.h>( o0 Q6 ^) _9 m' V6 G4 N$ |6 |
void main() * \3 ~6 K( R: s* ^: V{ int i,j,k,n;2 I5 H4 [! G+ K/ b. f M/ d
char a[50];3 h7 W2 v X4 c/ J/ }, s
gets(a);, V" J2 T! U( r. e+ Q8 y
n=strlen(a);: w* n/ ^0 p. t0 P3 O7 m! m
" _% s9 n$ n# J2 |7 W, {7 F5 r2 K for(i=0;i<n-1; ) : h& M& H1 ~9 t: V7 S if(a==a[i+1])# L/ m* R- Z+ Y2 J, \ }4 ?7 C/ U
{ for(j=i+1;a[j]==a[j+1];j++); 5 Y" N' d* T' y/ t printf("%d%c",j-i+1,a); - b4 @# k! e7 ` i=j+1; % a' J; F- j( I0 M }$ [! A# u6 p' R
else + A/ E- {/ m$ \1 M { if(a==1). v4 ~* T2 D; v+ o7 x+ f
{ printf("11");3 j0 S! p2 z/ ]
i++; ) ~/ ]. K7 E) h9 L* e& ~9 C }, Q* c3 {+ Y7 l; A0 M- i0 Z0 ^* v
else* ?8 z* E0 U E5 e
{ for(j=i+1;a[j]!=a[j+1];j++); # l. o9 i0 G* T+ O: P$ i7 K" F printf("1"); * H3 G2 I2 g6 U! _# S if(j==n+1). \8 p5 R) }. N& n0 q$ U
j--;- V: k% K' ^- J) h/ S$ r4 Q5 L
for(k=i;k<j;k++) 7 c" ~: z" X: G: Z2 g; S printf("%c",a[k]); $ `, G) L1 h) k& I printf("1");3 K8 H$ @( v* D0 A% u: w
i=j;4 ~4 h' U7 ?; @. y @
}$ s5 O2 h9 l$ {2 p+ l
} & l, ^8 H' w' [ if(n==1) % q1 M/ e8 L( j K5 i4 M if(a[0]=='1') 8 d2 r# p; U2 ^1 P1 T9 C8 [ printf("11"); ' v! q0 I& m) N else . D8 l7 N' F6 X; B printf("1%c1",a[0]); ) K7 f% h. e! Y$ Q printf("\n"); q% I. \+ a- D" Y9 D2 ^# ? } 3 @8 d1 ?. z# k( J5 ^ 来源:编程爱好者acm题库