Run Length EncodingDescription
( p0 K& J s$ K5 g
) V; k7 T5 R- rYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
* Y1 c2 P5 B j% f
# ?9 R- \: t* QAny 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 x2 k1 s$ I' Z; M- \3 k" k
l8 @3 ], A3 S+ pAny 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 ) L6 a! n7 `6 l/ h- Y
sequence, it is escaped with a 1, thus two 1 characters are output. $ w' z: d, b0 h# y/ Y( ~6 E: s" h1 M
0 @, T+ {) o# ]& i- TInput 1 c9 C9 G9 ?3 `: V" y+ d5 W
& n0 i y3 J: \! iThe 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. 0 |. @% f9 v' @4 T3 \; f" x
+ b% s, _ S4 m4 r' y) ]- ZOutput ; i, F) I0 j/ f2 }2 t4 P
! I; i! H+ I! i" Q5 VEach 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. & s0 [3 Z" a9 O+ r
, W3 Q8 K) D5 d1 ?9 N! j
输入样例 ( Q3 D! d' ?' F
AAAAAABCCCC
, v) y/ r" f2 ]9 M( I4 x! z/ J123446 B6 E( Y3 X% }, Q" y: X" Y5 K! b
8 t \% W; _& ~. Q4 w
3 {& U6 h( t8 P输出样例 6 \2 V' h( P* T1 k" X. z* r" d
6A1B14C4 K* e8 h0 { q
11123124
# G( U; m) m3 G+ `8 U; q' T7 F , F2 z( T$ z& N
: B2 z2 f& A7 ?7 J% P, N$ s
Source
% Z) W8 H- t0 H0 l5 z/ K
# m! k" O. P: J$ DUlm Local 2004* P$ |: b7 S3 N% g" q# R; ]- f
0 Y4 ^" V# l& P( n7 ~
example1:
/ Z0 e$ q' M% D( C( B" }#include<stdio.h>( Y$ C' u* I8 \" R' d
#include<string.h>
& q* k6 @* y) k: a9 x, f3 @! cvoid main()$ R) J/ x Y7 t% E6 n! Z
{ int i,j,k,n;5 g5 r: p; y: m! |
char a[50];% H( }/ J( p4 i5 `- z
gets(a);6 }2 i l7 M X6 Y5 E! Y
n=strlen(a);% _# Z# J& E, }# V" c2 d% b' D' l
* Q+ F$ p+ v) P9 @, @
for(i=0;i<n-1; )$ v9 n; n# l0 {% h* S
if(a==a[i+1])$ v. D+ \; f. J1 A8 t3 j
{ for(j=i+1;a[j]==a[j+1];j++);
; A; B$ ]; S1 H: f& D printf("%d%c",j-i+1,a);7 [# y' S4 U6 G+ X/ h% q) X4 n
i=j+1;# p8 f+ F8 |1 U7 b% W1 ?' D
}' J$ `! h2 g6 o
else& i8 b2 ?) S: D" b5 Y0 k
{ if(a==1)( F6 G2 M3 S, \6 {. i% t* e7 j
{ printf("11");
0 y! F0 B( f! Z( ~9 p i++;5 E8 N# i0 X. r4 L, M4 M
}
; J1 F* \$ I. {4 u else
* U8 V1 z6 q. T2 N# ]6 f { for(j=i+1;a[j]!=a[j+1];j++);
$ k8 J, i; ?' w. L/ p printf("1");
( M1 l, J/ b5 ]* b/ Y! \4 l if(j==n+1)8 o0 n2 h- Y/ x+ Z, v
j--;
, q9 c% E) u8 A! U for(k=i;k<j;k++)
4 k) y3 o' l1 E- M printf("%c",a[k]);
, V/ F; J3 c4 j- j5 \. T1 O! @. Y printf("1");
$ B0 |0 U. g7 s; g S! G# n i=j;9 T; C9 h: P/ }
}
( ?" d$ Q7 V0 I) U4 s }6 J( ~8 F: Y% v6 V( q
if(n==1)) Y; x1 U7 h" |& N' i2 r2 S
if(a[0]=='1')
) I6 {2 D% x" q; C( \ printf("11");
2 a& x- J l- H$ T else
# @: w M2 y5 o% a printf("1%c1",a[0]);
# P+ G) U1 J& [3 @) o3 a0 g printf("\n");8 ]5 ]' e! q. L5 F
}
* ?9 c6 Z; v4 \3 M; S0 F, |评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>
2 `2 f3 u; O- ]# H#include<string.h>
0 S8 b% B# Z ~- q7 o4 d4 |; ~9 @void main()
+ R1 \9 [* d! D. u7 W9 Q& a- t$ T3 s{ int i,j,k,n;
. ~1 k) {2 C% P char a[50];
# G1 t: ~5 A6 h, i) C- ] gets(a);
2 x& [3 @: J6 X3 K% N5 R! C3 H n=strlen(a);# U6 y$ e% a8 L% ?7 Z ^
9 j! L6 \6 B) q5 I& ? for(i=0;i<n-1; )
]* |' |; h0 w H! z if(a==a[i+1])( i$ }9 z. r! Z$ v- r" _# a
{ for(j=i+1;a[j]==a[j+1];j++);$ N( ~( @* \( \+ {: g
printf("%d%c",j-i+1,a);4 r) h( w: D9 G9 U& r
i=j+1;, z! r! \7 X4 Q
}: J3 F+ C! \; A' P4 t
else& g. P; q+ O* h1 ?1 j
{ if(a==1) z- c% p: {: B+ \4 m
{ printf("11");
8 z8 h# f! R, i6 R" b; L2 k" T i++;: M- ~6 e0 O( h0 L( r
}
2 \8 V; c, F' e- P/ ]9 U else
& _1 C; D9 E& x { for(j=i+1;a[j]!=a[j+1];j++);
* s6 q1 o# o) z# W printf("1");
& y ?2 G8 j. V if(j==n+1)8 E/ _) u" K- L5 Y$ t0 A2 t
j--;) f: R. }( _$ G
for(k=i;k<j;k++)8 a" J6 ] c" j+ E
printf("%c",a[k]);
S" ?* V. u0 f: f printf("1");
& @7 A% n& r$ d" x i=j;
$ D; J8 j7 }0 f; Z3 k: F+ M }. N( n* O" R7 [2 q% Q- k
}, g! V/ f) O0 j: ?
if(n==1)
2 o) [' [- z" ^& a- |" J) |: n' f if(a[0]=='1')
) L- D/ _( Q# A+ h5 L" _0 o printf("11");
! a1 k$ R+ G @$ N% U else/ A/ [; Y/ R9 _
printf("1%c1",a[0]);
M8 k& U& s+ e6 |! ] printf("\n");
' }9 d! D$ \% ^$ P( h* C } example2:#include<stdio.h>
6 R" P! H# K8 O L' i0 Z, _#include<string.h>( k1 I4 Y" ~# }& f0 ?! x2 n
void main()/ Q/ `- {3 T, x g# f6 t& S
{ int i,j,k,n; R) W! X K% m/ j
char a[50];
7 j; V+ r. ^* e& Q3 s gets(a);+ l9 T( v# Q8 [# ~+ e5 R, y
n=strlen(a);2 Z& o7 L5 G0 g7 ^& O4 j
! l; c' k" k/ T6 H$ T9 Y8 d* o for(i=0;i<n-1; )
: Z3 k1 t, h* u/ Q! K if(a==a[i+1]): _% b* p! A" F" G
{ for(j=i+1;a[j]==a[j+1];j++);
2 O- q% n) J8 L; ?9 z printf("%d%c",j-i+1,a);
% T7 U4 q; Z5 N: k" ]( i i=j+1;
, s5 H. K8 Q: Z8 N3 u }
9 m7 ^( I( l/ T Y) H3 d; ] else
9 d7 a1 g* b% T2 @7 L; g$ j { if(a==1)7 U4 Z1 F% C* ]0 |: o- ~
{ printf("11");5 G7 v+ h7 A1 r5 q6 p0 z) T* w
i++;
3 N, B' w" X3 t0 X' J" i }
. ^& j$ k5 g$ Z3 O7 n6 [; H8 h else, x- K7 o+ s5 ^* X- i' D
{ for(j=i+1;a[j]!=a[j+1];j++);
' P0 k) Y# p! L5 m1 L; C' d$ P6 M printf("1");
% [1 W4 Y+ x- {8 j if(j==n+1)
2 z p, E# R( w- l. t* } j--;( M c* k5 m$ l: l& L
for(k=i;k<j;k++)
- e* A3 r1 ] s9 k$ v! P printf("%c",a[k]);9 @; R3 r$ d- D" i/ M
printf("1");
5 X1 O5 E0 w0 w4 x i=j;
" S0 F7 j5 x0 R( @" u- P }
' E* B* ]2 G4 K+ r3 S) F }0 l& Y* }2 b4 _4 J- I4 n$ ?; M
if(n==1)9 B0 L- y# g* t2 M7 o
if(a[0]=='1'), }: @& X; ]( L; S
printf("11");- E* N/ |% t7 ~. J- V7 u
else$ d9 k1 ^0 {% f/ j
printf("1%c1",a[0]);' ?! c6 R9 S/ g. B3 X7 }
printf("\n");
) }2 N# U1 [+ ` F }% s( m! @6 V! S; m
example3:#include<stdio.h> j' C6 v- a) _" m% q }
#include<string.h>
' t8 j A% b- E5 O; dvoid main()
8 e4 t: n" L6 |3 H3 K e8 d; |{ int i,j,k,n;2 G7 y( b: x# Z0 W
char a[50];7 k0 p' N- t; J- f* }
gets(a);3 w" d/ S9 c$ i2 _4 Q( Q' r1 m, K2 B
n=strlen(a);- M/ w' B( J' p( G$ p# ~
0 F- z) Q* @9 L% ?
for(i=0;i<n-1; )8 Q% m- p5 L6 Q; \; g5 S
if(a==a[i+1])0 ` R+ X, z- r; _' i% f
{ for(j=i+1;a[j]==a[j+1];j++);2 n1 l* N6 {6 A' {4 L
printf("%d%c",j-i+1,a);. \7 L0 U& h1 h
i=j+1;/ E) G \- a. Q
}, f; J) E) }5 C+ E$ Q
else
* N0 A" w, ?0 _+ b. g* Q { if(a==1) Z# v$ _+ o4 h: k! x
{ printf("11");8 T: ^$ S; R* j
i++;) B5 ~$ {4 i( z- Z/ [: h
}
/ q# y c2 p, X4 a$ n' | else
1 g9 N0 _: C4 N# ~5 K& m { for(j=i+1;a[j]!=a[j+1];j++);
! q4 J: R. c" C s! q printf("1");- `; D0 S" H) f+ o- a! r) Q
if(j==n+1)
9 J0 Q% r P P3 Q0 I j--;3 p' |1 K9 q* w* u) p
for(k=i;k<j;k++)
6 P6 q8 z1 N2 c3 X+ |9 J printf("%c",a[k]);
1 b4 ~9 h, `1 q+ G$ b printf("1");. F4 D# S8 Z- e; u' r6 S4 f5 Y1 t6 M$ P; T
i=j;
) T1 @' q( J/ ] }' l2 n/ c2 E4 u6 z% @
}2 ]9 \% R% D3 U2 G8 G7 D
if(n==1)& y5 D' k% b- J& e/ \
if(a[0]=='1')
6 S; \% L) a5 a% S( U7 J+ e printf("11");! I+ _8 F2 ^1 r# A2 g, A1 _7 _
else
8 ~& Q b" u0 R printf("1%c1",a[0]);& d$ o8 T4 t5 ~5 }) U
printf("\n");
( S9 x7 p7 ]$ V }
5 R4 Y* r5 }/ |, v# k 来源:编程爱好者acm题库 |