Run Length EncodingDescription * Q# I. w r0 G w( {. l
% O- c4 x/ F6 b* _- O0 S
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
. ` R/ O r" R0 b$ e/ ]6 E, M# j+ h! v0 U0 i& T: X; k
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. w. q. p$ ]3 r! s
% `' v% b* q* d @/ `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
4 I3 e4 ^) D% @sequence, it is escaped with a 1, thus two 1 characters are output.
. N( }; m2 ^" N6 k+ z8 y- |+ ]
2 o2 D" F# z' m& eInput " m+ S" D& B. A2 e
# b8 i' x* \9 T7 x/ \9 F, \) @
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 h. _1 E. K, E2 P
, V5 {- V' d; Q, S
Output 6 t2 R9 E( m/ E. T
: n7 y/ P: O* @# P y
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.
( ~; ~; F% N/ E
. B+ P& r# c; g$ _输入样例 8 Y7 F$ S# q- F5 k
AAAAAABCCCC E2 L" a% Q0 I, [
12344! `5 R' H5 A% }; [
# K# C! l6 X' B. c8 ?# I
2 C0 v9 b. X# s; V1 `输出样例
9 Y0 w5 ?, A: l& z" Y$ n3 r6A1B14C( u% U# K' [* ^ P) Y. f) g% ?% F+ m
11123124/ E) s, g' m4 Z6 g3 M% |
" ?4 {/ W X1 G9 ?& w- r) _; l8 W1 P$ A' w% ^: X
Source
2 Z1 M% M( {- l, N6 Y8 n' o2 ?- z a
Ulm Local 2004
* h/ h. z* e6 L8 ~+ i0 M, e( Y3 [: _ Z& f. i8 K
example1:. d" w# t. m7 l7 Z6 T
#include<stdio.h>6 y' w: R! F4 N, @* X
#include<string.h>9 o" W& }+ T3 @8 |
void main(), e s) B- R% w# S; S9 l
{ int i,j,k,n;
5 Z; Z7 }2 y8 @' o char a[50];
8 a/ _8 f. l# X9 v9 X$ L' g( D7 i/ [ gets(a);
3 V1 @9 M- d+ J& _* t% r' } n=strlen(a);) q6 h% f) V/ u( _0 s
1 M% W& ?, l. w2 n t0 j for(i=0;i<n-1; )3 ~9 K* x( ]3 j/ R$ [: o+ v) H
if(a==a[i+1])! J Z" P; U7 P8 F, H
{ for(j=i+1;a[j]==a[j+1];j++);0 q$ z) J! [+ \
printf("%d%c",j-i+1,a);
) ]$ ~ }9 G9 W i=j+1;. K* Z5 ?: h. ?) H& D* o3 Z3 q
}
) p* P3 j4 v2 L- {, ^ else
: w0 X4 \3 n7 v { if(a==1)% N/ N3 ~6 h7 U$ t
{ printf("11");: q0 ^8 c7 w" A; @: }- |* m
i++;
3 @1 ]- q+ [" C y; K; H0 A; F9 } }" X. F5 F5 @7 W+ v
else
6 N+ Q3 f6 U& @0 D/ y { for(j=i+1;a[j]!=a[j+1];j++);' x- G" g/ O. w/ P V
printf("1");' d" m+ f) ]" ~) ?4 A T b
if(j==n+1)
! H5 n1 X$ b' T4 Y/ R j--;( s! H' `$ O" g4 x
for(k=i;k<j;k++)
# W+ b1 L' B1 q6 y5 } printf("%c",a[k]);* Y" r0 z6 I z3 s% T
printf("1");
# |3 i: G1 N5 y! H i=j;
/ L7 s% A: e" H9 Q2 K8 V' P }- I6 P' o1 m5 h$ H
}, V4 N1 j2 u3 r* {+ u$ p
if(n==1)/ o0 L! N+ I2 ~6 c9 I W! M
if(a[0]=='1')
4 b. X8 r- r( n2 l/ K5 _ printf("11");
, L" j. [2 v. f5 S7 p/ b8 H else( H7 \: }6 O" ?7 K. I5 F4 h' ^7 s2 A
printf("1%c1",a[0]);# t( q1 w" O1 d! i
printf("\n");
! R$ U5 ^" H- t' F- K0 i2 ^( Y& V }2 V( C$ |9 ~& {+ h# T5 Z7 e
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>& B, T' ?7 y y" q; \
#include<string.h>& E% V4 g8 r5 c) D
void main()9 k0 r( d( ^/ G _, R$ N
{ int i,j,k,n;2 ~! i) ?! W: G( ^2 [7 p
char a[50];1 R# \/ Q" [4 @
gets(a);
) T( u2 i3 D- \0 b; B n=strlen(a);3 i3 y( G. J0 k$ J% U4 Q
0 b2 z, f. b. g2 x9 p
for(i=0;i<n-1; )( ^% M. w/ h$ r) I) |2 j( a
if(a==a[i+1])
; ]% k, ^9 k6 S1 W5 X { for(j=i+1;a[j]==a[j+1];j++);
/ K' A C; E/ b* l printf("%d%c",j-i+1,a);2 [' j; _* N, M& Y& [) E
i=j+1;. C+ A* F! I4 \/ n, o/ @* Y
}
( @& S1 D6 E5 t! N else
+ x: ^6 ]8 L& ?! J, P8 l+ } { if(a==1)
( c% W8 b( Q" M; v3 b { printf("11");+ v9 N5 J- a& J1 f3 r
i++;/ }3 g; t0 f: |
}1 g+ M1 @, B K$ w' {$ O2 L
else
4 S m8 |. c& K8 T; V* C { for(j=i+1;a[j]!=a[j+1];j++);
3 E- [0 s& @) |( W6 Z! e7 r printf("1");3 V& h" e6 u8 C# W" }( u' }
if(j==n+1), u7 R8 ~4 R# m, Y7 Z$ g' e* D$ F
j--;
: U1 V+ |6 E, e# b/ o for(k=i;k<j;k++)
( q3 b/ @9 b) l: l; R5 n& a printf("%c",a[k]);
8 J' g1 t* f( |1 Z+ ^/ f f printf("1");
. D! J3 @8 \- u+ i N6 D i=j;8 g' F7 K# f3 e+ x" G
}/ c" c3 N$ N+ G; |7 `4 n* r9 H
} [9 U: Z% D7 j8 ?7 v" o: r
if(n==1)' t+ a# D/ d1 [2 R1 f: O8 D
if(a[0]=='1')) k/ C+ s# {" i: h# R' {, R
printf("11");1 h2 c; y$ V0 q
else
8 }2 e/ f, f9 G, B printf("1%c1",a[0]);
9 }( B# j. A1 g printf("\n");
! P0 ?- _5 t8 A- m } example2:#include<stdio.h>& d5 L) \2 p3 a# M4 E! }
#include<string.h>2 Y: s6 u& X$ }3 g( n
void main()8 F5 M2 T* @* P- @1 y, y) g/ }
{ int i,j,k,n;* Z8 x; X2 M' q
char a[50];3 Q: Y( V3 R8 Y. r8 J
gets(a);+ c$ _1 u% k6 ~# g1 b8 T
n=strlen(a);
& s; H& i3 K3 L' z+ m( l" I! S' e% d; ^! ]% x$ r
for(i=0;i<n-1; )' R! i; s; `5 X' }6 z1 T' X! d
if(a==a[i+1])% B M" o/ K! P, x X+ L+ M
{ for(j=i+1;a[j]==a[j+1];j++);
+ P$ K" w7 O1 V8 j0 t1 J- X& e% M printf("%d%c",j-i+1,a);
4 N K: M) B* l; _. `: }* b- J; P i=j+1;
/ w8 e0 q$ f n! A }: Y0 |2 z& H' F* l" B
else5 Q) e8 ~" t9 q- ~ {/ ?
{ if(a==1)
+ @9 Y5 a5 p8 w. z1 S8 M2 B P { printf("11");: R9 W$ z M2 ?
i++;
/ d" F, ? d! K" I }& P" m" s b: T3 J, R7 K$ O8 O4 {
else
* `0 w$ l7 W( }: i+ M, @8 ^/ [ { for(j=i+1;a[j]!=a[j+1];j++);: ?& ]8 D& Q+ b+ z- c- E9 {5 v2 U
printf("1");
4 W7 E( J8 C+ ^2 _: w/ X if(j==n+1)- y6 l9 Q( }6 [' S
j--;2 ^) Q, P& m6 |6 E3 q5 s& Z3 ^
for(k=i;k<j;k++)
0 P& j Q+ W0 {; d2 F9 y6 R printf("%c",a[k]);
2 C; V9 j8 r. [! ]+ g& ]- _2 a* |# h printf("1");$ X1 S1 Q3 k( c5 Q, I0 a) h
i=j;
9 y' z3 a+ ]. `" Y$ C- M }( p# N4 ?( Q& W' ], r% P4 q
}/ U! f7 R1 g' Y- y
if(n==1)/ x9 Q$ R1 m. X- C* z N
if(a[0]=='1')9 P! {" j' K) a" {/ ]+ j
printf("11");
$ {. ~- P) }4 ?9 h& } S; p6 N# i else
: L1 e6 @! Y1 u2 p printf("1%c1",a[0]);' d0 P; I3 _" K
printf("\n");7 s X z, h9 a3 C
}
% t; Y+ G% O3 P9 p: j* y7 u example3:#include<stdio.h>
# S0 V6 K' |+ X& G& a$ L#include<string.h>
/ m( V2 |/ T! o) C4 X6 i/ ^void main() U# ^3 S r" o
{ int i,j,k,n;
& `1 _- D0 Q3 w f7 } char a[50];
3 l' z( D" l* D$ [ gets(a);- ^, y7 m1 y% D) w. k
n=strlen(a);
% c! _/ F! P5 E: i5 w. d" \6 X
: `4 l% {4 G( m0 ~+ h for(i=0;i<n-1; )6 ?1 h2 |: W; W" J
if(a==a[i+1])
* r8 t( I- Y. Z: Q4 k. G. R6 B4 X { for(j=i+1;a[j]==a[j+1];j++);: C W$ d2 }0 Z6 X& o% ^/ T. i0 H
printf("%d%c",j-i+1,a);
) h2 q+ Q- @5 G% ^0 y2 D% m, r i=j+1;, |% e" X5 r( \) l- ^, l7 i. K
}$ A; `4 U& T9 R- T- u+ Z
else
! R) o) A6 \( \. Y# R% M: g { if(a==1)5 _5 ]2 {9 n2 x3 {& S3 F& o
{ printf("11");
- y6 E$ X" R* d% j, \) K i++;
" C2 t! K7 y$ h! |3 b) \ }8 y! x* C. H1 R( I) o
else
4 J: y w+ f. Q! U { for(j=i+1;a[j]!=a[j+1];j++);" U8 y" J' |$ y
printf("1");- L4 X$ b# C. o: z# V3 l
if(j==n+1)) ?, ]! o1 Y! v2 d+ i) D
j--;' d+ Y& n( j* o( a( ~/ {8 P9 G
for(k=i;k<j;k++)0 |+ j. }# s: b- }
printf("%c",a[k]);
4 M8 D% h& q1 }* a printf("1");3 |7 U, y) O8 i' A' i
i=j;
$ n5 i7 m, m: c5 [& d }1 B! F- b) |& M# D6 s7 s
}# d9 R4 z0 r. E; d- @( ~
if(n==1)
- b2 p0 n' `2 h$ i7 W) }! L if(a[0]=='1')
$ V% Z/ e4 W+ e printf("11");
9 a/ m: D) Q3 \) g V else6 [) O( Y; i* z( v
printf("1%c1",a[0]);7 ]! n: V8 J+ e7 E
printf("\n");
E. |* o. B& F3 n' y& S8 Y2 n' f }
; |& W: Q, I* P7 r 来源:编程爱好者acm题库 |