Run Length EncodingDescription
" U6 y- G l- ^: ?" V* E
" t4 t7 w* e4 S* vYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
2 i4 [" \, j- }/ W4 o% u
) C# `2 C1 I4 `8 ~( F5 PAny 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.
% \: r% G9 c9 H0 K0 p
: l& G$ e9 e2 e9 f8 S: ^2 \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 & } |! L7 g' u$ r. g: `3 d
sequence, it is escaped with a 1, thus two 1 characters are output. - r2 U4 L- _; p9 ~7 v- ~
( n" ]! l9 G! m( i3 R8 ^3 ]( `
Input
$ d$ c8 g- c) ?/ m# ^+ Z
1 V5 k5 }8 S* AThe 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 x" L. i& e; a |- ]
' R$ ?2 Q9 T4 \$ m) D6 |( r# [' \
Output & w* d2 N- V: v; h
/ M8 V+ z; C% O5 w1 f2 j
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.
. I: e) E+ k: J2 D: {, T1 o+ e7 W: V$ G, P* z$ N6 J B) }) x
输入样例
& k" ?* u. p3 n, fAAAAAABCCCC
1 s- B1 j5 H: R0 g, s12344' F; |4 G" {/ A" \& j% O
4 v6 r! I5 }) m1 I) C; H4 g
/ s6 w6 N, J) K- a输出样例
8 S1 v- I9 G- D6 D6A1B14C
; \1 `1 J+ n3 n& L11123124) z5 D) s1 k' z+ F% P
& ?: [8 v0 o! [ `) f4 s* i/ ?- k# u G6 a7 r) O, y+ B
Source
. r, _; B- N/ o+ ?* d! R/ ^6 o ?" H! `; f; r/ D# C8 L6 o
Ulm Local 2004* H3 a/ r: d! m- a& R9 _
5 G4 ~* ]$ k. j% i& F% C: a R, Xexample1:5 d5 H7 Q5 t# q) [0 z
#include<stdio.h>
) l; z0 b% e) L5 x#include<string.h>
# {4 F* G. r; W L0 e6 d+ qvoid main()# t8 y/ N4 m& ^% T* A' Q
{ int i,j,k,n;
* A) V: E$ q; D* [# w1 l( { char a[50];
8 C4 s- H" U9 a* T, z0 N gets(a);" K2 A" d2 ~) @- d
n=strlen(a);
, q. ?! Z0 G* o4 S* f5 {6 _& f8 V; P* B! \2 c) b
for(i=0;i<n-1; )1 K/ `. D7 z8 @+ J$ f% J8 f% g
if(a==a[i+1])
: ?; e, R# a1 {* y. b$ \ { for(j=i+1;a[j]==a[j+1];j++);$ @" h8 u2 P4 J( |, e1 t
printf("%d%c",j-i+1,a);9 ^( R* }$ {0 T2 o
i=j+1; ]% a8 z2 H- F
}
I# x1 x- U% H; x+ u1 c5 G else
* w8 u- k5 W9 N { if(a==1), w0 B; }/ J: E4 |0 h1 X! p( \! z
{ printf("11");6 m( N1 D/ }" j0 X( r
i++;
7 z% F+ a: {/ B5 U' o0 ]# k }" \6 V0 W: k: T$ F8 S% {3 X
else# c, c2 m+ k! h7 ]4 N) k( j' X
{ for(j=i+1;a[j]!=a[j+1];j++);6 X/ x8 G2 ~& V6 P; T8 q
printf("1");
( R; _+ G/ R" {3 `8 I if(j==n+1)) |0 s( P& x$ Q0 Z- g' H
j--;1 n+ i+ ]8 t/ u2 N
for(k=i;k<j;k++)# ]( t. j% a4 _0 [
printf("%c",a[k]);, Z; p6 B/ ^" U7 z5 A; G
printf("1");
% u1 A% X+ m; d6 V7 l* y7 Q. Q i=j;
I1 u2 r! T2 _/ N5 J }3 n# Z+ F7 v u9 d" ~
}% n% h; k: |$ d) q4 k5 [; f" ?, K
if(n==1)4 Y! c W J) |& A# B0 J! i' o$ ]% p6 |9 p
if(a[0]=='1')9 ?: B5 }3 i4 Q$ f# K* i0 Z: X
printf("11");' y& p6 M- V4 A: @3 }# c7 l! J+ D
else5 d( s& y0 v; f x( g
printf("1%c1",a[0]);, O$ ?. e% R/ k0 y
printf("\n");
, w* Z" ?- i; m( A }# i( K8 z* f( f* x4 _& a- Q
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>
2 I5 i: D5 g7 j. t) i#include<string.h>' p" t }. g" P% N$ ^% ~
void main()
3 o( B1 i5 P P: u: }& o( q{ int i,j,k,n;& S4 T: W" i9 [1 S" B
char a[50];$ s2 r" H r4 M2 p
gets(a);
: V; L) Y {) t+ N n=strlen(a);1 C6 e/ H- N' _/ c6 B
* ]) Q# p, g3 B! G) p& `2 J4 w" G
for(i=0;i<n-1; ) [8 |$ w$ R% p3 H; s, l }
if(a==a[i+1])
4 |0 x' B4 A% G! Q; ] { for(j=i+1;a[j]==a[j+1];j++);
8 f9 |$ W6 f1 @8 ?: a printf("%d%c",j-i+1,a);& Z2 }, Q+ j/ y% N# Q$ c
i=j+1;
O# m5 a* A3 ~, ?: C; T }
8 D( e0 x# ^- X- E else% H1 o1 f. X* R" h6 L7 v
{ if(a==1)
0 ~; F1 D, a4 ~! F4 D8 T" f { printf("11");7 a) K3 D Y* w0 N- V
i++;
. l1 Y" a- x# ?' L. u: w2 d( \ }8 ?) _5 e) c; |3 l z" L
else# X- @: ]& j1 D
{ for(j=i+1;a[j]!=a[j+1];j++);+ \8 L) \6 \9 H$ s" w
printf("1");$ o5 j' T; v9 `$ e, Y5 z9 M5 I: ]: g
if(j==n+1)
+ W# ^: w# L2 d+ c- D j--;
5 w5 B, f8 H3 H1 Y3 F, x for(k=i;k<j;k++)+ G3 o' e! T1 V9 Y* M. U* U& Y# y
printf("%c",a[k]);$ p( j _! w, d2 T
printf("1");3 ]: y4 y* J% z6 L
i=j;
2 m8 r: K; E) C }; s) h4 e& J! g) r' t
}' ~0 C/ J6 S% z* L
if(n==1)% F G( ]7 |, a; z7 p) g
if(a[0]=='1')
" W+ \" c% a4 ~( X/ J3 v printf("11");4 D5 H' C: X }; z3 F
else, n$ U5 i/ P% F8 X* d4 M
printf("1%c1",a[0]);# G4 \9 t1 z9 T$ A/ Y
printf("\n");# y P' f/ X+ A1 f; A. g
} example2:#include<stdio.h>& U* D1 [$ d2 g1 D; @" p4 F& d
#include<string.h>0 c' o u% } Z) q O8 [9 j
void main()6 Q# ~, R6 H% G# H* t! S i$ s& j
{ int i,j,k,n;0 G- v1 D% D5 x0 E$ r/ z
char a[50];3 V T P( ^7 ^* Z
gets(a);
8 [4 X* c( Q; k( K n=strlen(a);3 `: h, P6 a) f
0 O% z/ K7 h+ n5 g5 U/ |' p for(i=0;i<n-1; )
; i; J9 z9 P, J! R$ D# j if(a==a[i+1])
6 N$ d" U, @, c7 H/ O" i8 ] { for(j=i+1;a[j]==a[j+1];j++);6 N3 Y A( m) C" c
printf("%d%c",j-i+1,a);! a% i0 x: F% V
i=j+1;$ @/ h, E! i, u c) S+ |: ^) r: M
}& m3 W0 P6 ?4 x. Y- _
else
% A9 p4 q" J2 M { if(a==1)
# t, E! l2 S0 ~' a2 o( l& e { printf("11");
9 \: @" z# H$ a- }, k( z i++;
- P8 [ b! M9 r6 E8 Z& k3 [ }
( l# R0 T1 g& u% B' U& M% v4 ~* c else
0 v% W- T' C' r { for(j=i+1;a[j]!=a[j+1];j++);
9 a# S- X( o- z( S printf("1");
) U: |: }) C) x4 t7 g! A+ G if(j==n+1)$ i+ P4 T/ b7 B9 F: I$ `% G) G1 k9 x) K
j--;
! W* S& t3 d( A; q for(k=i;k<j;k++)
5 T5 J3 D" m: } ?* h printf("%c",a[k]);' c( z" ]+ r, N+ E! y4 N8 @
printf("1");
" @% u: h. @# ^9 ~! \- @ i=j;3 D) ~. y8 p% f( l
}
" q* p4 l1 r! {5 @2 S; H$ e }
$ r, C: C, L: J: M F if(n==1)9 W d' m" b. \2 g
if(a[0]=='1')& l9 Q4 R3 u! s# N
printf("11");' r% N- v4 O. ^8 o! i2 N5 j
else1 x2 R6 q, C# C( r2 z( w
printf("1%c1",a[0]);
9 r9 C; t! c6 `- w- g8 _, ] printf("\n");
- Y, t6 U3 ~# O6 @ }
# O9 ]1 }7 ]3 e, L: m' `# t- i9 ? u; p example3:#include<stdio.h>
) ^4 k) q5 D g+ ^& V4 D) l#include<string.h>" w2 K/ s0 b7 f0 J
void main()
" b, [& U3 }- h1 |5 D- x9 m{ int i,j,k,n;
9 r k; u( j- @2 @ char a[50];; i4 V. e" K* Q& C* S, P
gets(a);6 {6 h) e/ S( w6 m4 Y7 x& l
n=strlen(a);# T% W( x! A- `& g( D7 S( i7 D. l! z
/ _9 X# f8 Y; J+ ]$ w for(i=0;i<n-1; ): A7 @! T( C- O# I R4 [
if(a==a[i+1])
0 }$ {* m7 Q5 P# h { for(j=i+1;a[j]==a[j+1];j++);
: Q1 [3 C1 _$ f0 L2 D printf("%d%c",j-i+1,a);
( R1 x# Z: D: u i=j+1;, D& \/ S6 n; I
}
% P9 Y$ k8 X& ]3 Y$ E9 i5 Y; B else
% ~: }! N0 w3 B& }9 ~9 E { if(a==1)
f; w7 B V1 E { printf("11");
1 k r8 I6 K& [) k i++;1 \* t7 T+ t) x# p9 O
}
( }& F( y" `: r else
/ M/ e' G0 C, l) U% a. h, f* } { for(j=i+1;a[j]!=a[j+1];j++);
( K2 H. i9 v3 \" z printf("1");
1 H8 F+ V ~8 T8 m# }& i if(j==n+1)
$ K" N- X% `& k" d j--;
3 o7 b4 J" h, b) X- S; O for(k=i;k<j;k++)6 o m2 K7 w0 k' q; W+ `
printf("%c",a[k]);$ f& ^0 G) K: b5 a
printf("1");
& K. R$ `& K7 S2 `% f8 j i=j;2 G- s( i- |. q$ D: g/ r" r
}
. a0 _! {3 ?" J4 K9 i2 G }
: h2 H. n* Z0 [" w! { if(n==1)
& B$ {5 ^9 l/ X- a1 \, V if(a[0]=='1')
/ a l9 f# T8 O7 n. i. c+ N+ d h Y3 c" g printf("11");
7 n1 K+ U+ h* E& I8 l- H- n. \ else
0 Z) D! {$ v. P b# b9 N& a printf("1%c1",a[0]);
' |$ j+ K) m. K4 ~8 m printf("\n");
! a" l5 f( s7 m. J: H! ~( R }
0 y9 ]6 u) } B) |( D; @$ w 来源:编程爱好者acm题库 |