Run Length EncodingDescription ; R4 d/ N" n4 b V
7 r2 q- `4 D- t( T2 F3 L1 |
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
% s+ n' i4 \# l: j
8 @6 l: @' q) IAny 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.
1 U# J0 M. P4 I8 J7 ~$ W, ~3 N" b( q4 K5 A& b
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
# `& j% n: q5 w3 M+ O" Tsequence, it is escaped with a 1, thus two 1 characters are output. 7 a2 G8 e5 G* [4 g2 E# r B, _
6 X0 D ~' O) H+ XInput
) Z" E+ I3 k8 t0 C, v1 z$ _! K. k& C& i, x9 `' X* A
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.
1 ^7 V& u) C8 K6 ~ E* L
" ~5 d% |2 `$ O% |. }2 T; I: v" AOutput , v: p5 O/ U* m
1 g: {% M2 J& c4 D5 ?1 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.
" @. l8 Q' d- L' Y& P4 T3 e9 y
5 l) X/ P' a: G4 s输入样例
; m1 q, Z4 B6 Q% g9 gAAAAAABCCCC R4 J2 R7 \8 q9 E+ J8 ^1 @& {5 w- q; ^
12344+ `' v K* i6 }6 X' W& k
{; f( [$ K- g7 e( R
7 D/ V6 u8 e6 x( ]输出样例 ; E$ \) v* M3 |. C5 p3 i! ]
6A1B14C
. q* Q% R5 l8 u% i- D; _% [& C' O# }11123124
1 v* \! z( ?$ N; A( G% r. Y * v W: E7 e0 F4 i: k5 I
j0 n/ {1 d. T* JSource% h7 k+ l5 U. ~, v( B% c. E/ {" r
: X; [6 L, d: `9 I' F; y7 U) Q
Ulm Local 2004
9 z& z* ]; R* n) J+ l8 Q' l$ a% ^' O w/ \0 A: |
example1:
, b7 U* V; m8 X#include<stdio.h>
1 { r& b7 j( X$ B' R/ t#include<string.h>$ h& Y( I9 @, t0 H' n: w4 g& ?
void main()- O, `" N' u1 r
{ int i,j,k,n;
6 r. ~1 M9 g, ^7 c0 W& t& Z: ?) ^ char a[50];5 c5 f3 V6 g* q8 x- F% u1 |+ M
gets(a);" P* }* g# u2 W9 U# r: Z+ d
n=strlen(a);
% G5 ?. N. G' d: T0 ^. B
) ~ Z+ u- U4 h1 D$ q for(i=0;i<n-1; )
3 Q3 m6 k- C( o9 K! D# H if(a==a[i+1])6 L4 g+ B v- T, f, [ ~
{ for(j=i+1;a[j]==a[j+1];j++);
( K- ^' ? K& G' k" \! S5 }4 n. z8 q printf("%d%c",j-i+1,a);
6 @" k- y0 K& c, t; G6 o& ?1 j i=j+1;( }% \& x' i! q, W' d' R; P0 C
}1 E3 \% H2 Q0 Y9 ^' P& `
else
( D' M9 Q1 P( k5 G$ a( k { if(a==1)
2 @4 j2 F7 i$ Z" z3 w { printf("11");
; A2 o" I+ i% O3 s i++;
( r7 V9 @, V0 y3 @. K% H. l6 z8 @3 d }
- }* C* e$ T; ^. q else
% A- L: G a- I { for(j=i+1;a[j]!=a[j+1];j++);
6 w5 m0 \9 T* G$ S1 @7 { printf("1");
% x$ T, n% D& z: x if(j==n+1) O! F7 M& p" g) b4 e
j--;
3 E' q0 n- Q" q: K' ? for(k=i;k<j;k++); B; |8 \( a$ [. \4 z; y) Y
printf("%c",a[k]);
. G, H; D0 q7 I% R printf("1");
* A0 ^$ X/ U; c i=j;, n: x' F3 a1 P" g" a6 ^: t
}
( e) P: Y# Y9 b9 J+ w9 ` }( E/ I Y' E3 N0 l
if(n==1)
8 {; X* G% }: [4 Y if(a[0]=='1')
2 U( G1 z- g* w- N printf("11");! ]% [5 k3 \! w
else
5 j8 R7 {( f) d. i/ L printf("1%c1",a[0]);
) N% k5 z1 \9 Y5 {8 K printf("\n");6 {" H+ f; X& X) C& r+ j: e
}
( U4 q" B" T/ ?8 c# ~& H评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>
% \# P) _' |( D$ B9 Z G#include<string.h>
& n+ S1 ~8 [& w h1 |! {' d9 _void main()
4 W+ a& `8 s J% |* {{ int i,j,k,n;9 Y" k- ]& K) I; z' R9 x
char a[50];& o8 ~) n$ C3 ^+ O. a
gets(a);
! W' P4 A$ m1 s3 G: N n=strlen(a);
, a, G2 H2 W: b' r' ]" S r7 @2 [+ R) @* g- C' Q* D8 b
for(i=0;i<n-1; )
6 b) Y- o+ E6 m. ]' B3 y if(a==a[i+1])
- x& c- z Z- e% r { for(j=i+1;a[j]==a[j+1];j++);3 J5 F3 D; X- e# h
printf("%d%c",j-i+1,a);5 Y4 E) U, @# a5 Q6 [
i=j+1;
" t. g5 l. t1 } }
* h# Y( H- E w$ ? else5 x( ]& Q# M8 V0 k5 {5 c7 S8 x
{ if(a==1)% Y0 C" T( ~, n+ c
{ printf("11");! S4 C6 U6 X; j0 s4 |4 g
i++;2 y1 J2 `" Y. [
}
- A( G4 d9 Y( B+ ^ else
/ H% I1 d: Q) N! \2 s# o { for(j=i+1;a[j]!=a[j+1];j++);; H3 B8 L$ i. M0 Y: B3 F
printf("1");
! n2 p5 j3 M) P. M& i if(j==n+1)6 W6 G; F! O8 t& u' g) T' \
j--;
. C% f: F# s. F for(k=i;k<j;k++)7 T7 x! y3 H3 V: ]( P
printf("%c",a[k]);
- e. ?. O# N% y: j4 N4 j5 p1 l) @: ` printf("1");3 ~! ]( L9 e+ e7 s. M% P
i=j;- J1 x' `+ K2 \0 o
}
- c0 W# N- L% u! u, R6 x }
O( B# N4 y6 T/ Z, }/ H if(n==1)
) P! A; Q3 Q1 X: f6 Z/ v6 \ if(a[0]=='1')
# p! c0 |: D9 ~8 \ printf("11");
5 r6 K5 g8 q4 C1 t else# [- m- t& j' o6 L) s) ]
printf("1%c1",a[0]);
5 j- I7 |% {( B6 M# ^+ F printf("\n");
! d' K' `& i' { } example2:#include<stdio.h>
4 J- {; D& y- |" ?6 s6 J#include<string.h>
* H; d! `! k7 Dvoid main(). ? |0 f8 ^( G& u4 ]% h
{ int i,j,k,n;- F5 ?2 ]& d, T" J. w
char a[50];' H" L5 x8 O' o# l$ Z
gets(a);
* u% |; j: f# m4 Q n=strlen(a);8 |: @) W3 \ |6 U
0 J( w* Q3 j% O7 {1 w* p- O
for(i=0;i<n-1; )
( L5 u+ C6 _$ H3 ?! Y% R+ r if(a==a[i+1])1 H, v# ?- _" F7 e
{ for(j=i+1;a[j]==a[j+1];j++);. _7 u9 }" D. P/ h, |! n
printf("%d%c",j-i+1,a);
4 K/ f& S8 M) B/ t5 O# ? i=j+1;
4 ]- B: `/ M/ M1 V* Y }
* C! `( V; h5 r5 [# L2 r d) r else5 q# e3 Z% V6 k. J+ C
{ if(a==1)0 v% K+ h( P; K, \6 @/ V$ O
{ printf("11");
8 }9 q9 M/ h @# Y- O i++;0 ~- l' c9 p" l& ^1 ^. x
}) a; Y) ]; @$ m: l; R
else" V. ~ A; V5 y: s1 P, H: H/ O
{ for(j=i+1;a[j]!=a[j+1];j++);
1 I+ C8 Q( V# v4 L: Y printf("1");
1 a% X% p. a+ s8 C7 F; U" k7 h if(j==n+1)
- j8 Y+ b: D% p j--;
' H% L& S( J) Q, y for(k=i;k<j;k++) i6 O9 X! n" n
printf("%c",a[k]);! T- f# B% a) ]+ X9 p& M
printf("1");- S/ b. N; b( n4 a; v# L( E
i=j;6 Q; F% s, o6 F+ e2 Y Z, B0 O
}
4 m$ U/ n, R. A9 N }
( z2 g6 @$ J$ C5 R" E6 r7 { if(n==1)
. \. D( U) A' c; ?/ N if(a[0]=='1')
" V) j( _( W1 f$ l: _: U, v printf("11");
! V; c0 Y. G8 A- ? else
- ^$ f9 _) T1 m/ _9 D8 a* \ printf("1%c1",a[0]);
" H- ]# G% J0 N* ? printf("\n");
/ c- o) K1 k! _, D5 r }* h( {9 m/ f; p) P' ^4 h
example3:#include<stdio.h>" n& e- S( x w9 B3 Q6 S+ b" Z& K
#include<string.h>
+ W8 w( K- k; c5 svoid main()' h T3 P% O x" r
{ int i,j,k,n;! S$ E. b2 L* v9 ?+ M
char a[50];/ e5 R- X2 ]! ?# h5 g
gets(a);9 X/ {2 `4 C6 Y
n=strlen(a);
: w* \& m4 o. ^, Z9 f/ w) b; n
. V4 P1 S/ H) k$ l5 b _ for(i=0;i<n-1; )3 H' k1 d6 t# [0 B9 q8 ~
if(a==a[i+1])
2 p) X9 t) [ Y8 d3 g { for(j=i+1;a[j]==a[j+1];j++);
3 z9 O. l- E; q- e printf("%d%c",j-i+1,a); C+ P2 K2 c5 B- G8 x. `
i=j+1;
: _! t* m' d. R/ Q }
( @7 y- [0 [& ^ else
) x9 I2 f" c$ v* n { if(a==1)
* I7 _* S7 H/ t( t+ L { printf("11");% C0 G' N$ q$ t& M( L
i++;
. ` W$ ?, @' m+ }% h }. O3 O1 ?9 ~3 `) ]
else- Q5 j. I7 a8 j2 q* Y
{ for(j=i+1;a[j]!=a[j+1];j++);
, F" M8 b( X0 W7 ? printf("1");2 z1 P/ I: D6 j$ f3 h" V
if(j==n+1)
4 S: R" f* z7 p k* H j--;
. g& {0 [3 V/ x3 @ for(k=i;k<j;k++)
( q: z4 Q4 K/ ^7 L$ S9 m* G printf("%c",a[k]);
{1 j& @' s* g printf("1");
- T$ F, @3 K% q% l0 K% J$ F! t s i=j;/ q3 J! V# Y% q, D
}
0 e$ B/ @5 t8 P' ` }0 g( h* F& M7 h/ P0 x
if(n==1)4 {1 F8 ]* p( D
if(a[0]=='1')' H- u, B* s7 D$ I% d* ]5 ]8 k
printf("11");
* a W Z4 |! y0 F5 @9 [2 t" J else
8 {2 x5 @$ X$ y2 U! J7 T+ x+ q printf("1%c1",a[0]);
/ f2 j' C Q; ~+ e+ O" q( V% D printf("\n");' L: z0 |/ y: x' ~5 N# _' G
}) ]( u- q4 T, \% J- M* R
来源:编程爱好者acm题库 |