Run Length EncodingDescription $ _) X1 E) f8 ? `4 ?, ?
) o, l# v3 v. e) L3 x) B2 R
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. 0 j4 J7 Y# M% |5 y* t
, p# B4 k, z7 g+ }, U8 ^1 mAny 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 E7 l) N1 H3 p+ l; [; J5 }3 v3 \4 g q3 X! {2 e5 X, o& R7 T
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 & G+ v3 q' F8 w( @
sequence, it is escaped with a 1, thus two 1 characters are output.
& V6 H8 O4 o# ^) A. @
. x* }8 ^( b* w4 FInput
& ^' I. h4 M8 l- g# _; L/ w% K6 U1 C+ w% W5 p* l9 y' y) b; P8 m
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. , I. e$ B. ]5 [2 c R
6 B& j6 _$ O( x# T) NOutput
5 H: y4 \3 y k! E. n, C4 h& ~2 y! G0 F/ l. G: Q U) 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. . `" h5 _8 x+ a0 L$ F0 U/ r3 S% S
7 c8 N( r: h" g1 ]' _) K% A5 u
输入样例 ; s1 B D; J/ K. J- d% b
AAAAAABCCCC% U* `' e* W0 u2 z
12344
5 `2 Z$ O" z4 Q( [ g ( X' J d6 K( |. M1 M5 t
7 a; J' N, ?$ l输出样例 ! C+ s( ?# B0 h' `8 V, c9 [
6A1B14C8 \1 P t+ y, K* |- a/ C
11123124
; l' p& @, P6 v
6 w: [+ `5 L8 L! m, g% r0 J2 A# q' K& C7 A* s2 L
Source0 c# U8 O+ G' f% C6 Z4 \5 F
8 z9 }2 ^+ T1 ~
Ulm Local 20040 h- h; f3 s$ c4 ]8 O+ n4 j
6 t; T% ~7 d( d3 k
example1:% f& O7 ?& x: @( Y9 u
#include<stdio.h>
8 G5 I4 F# n& }#include<string.h>
! g% p/ [+ d4 u; h/ g" Yvoid main()
% V h( q8 B1 k4 F% Y9 o. d{ int i,j,k,n;* e0 d, x7 y+ o. u; `2 S
char a[50];
( ]0 ^/ Y/ |4 [1 A: W- m- t gets(a);) a) N* r/ I" T" b2 E. M" r+ d
n=strlen(a);
" Q: d, S+ r6 |- T8 I
' L/ L: W: R. I, g9 m/ u9 x6 G for(i=0;i<n-1; )$ h8 v' l( ]# Q0 \7 e7 A: j
if(a==a[i+1])
- w( Z6 V3 J; }$ o& i6 T$ e { for(j=i+1;a[j]==a[j+1];j++);
; ?8 } R/ _$ }7 ]7 e" j printf("%d%c",j-i+1,a);& ^2 T7 G/ J2 W' h5 V# M
i=j+1;+ x, H3 s+ }1 r. o
}
, t; Z7 X( p: Z! l else
# B6 m0 d1 ~. @: a, x { if(a==1)- x4 c) _7 K2 y
{ printf("11");
5 F8 X1 u* h9 [' r; w i++;) E8 p6 C& J! \) {8 y! {
}
! M9 f' L' C7 k' r else
4 v, U3 i8 B: A7 l" F/ { { for(j=i+1;a[j]!=a[j+1];j++);* J+ B7 W* T5 E2 [! }6 H! r X/ v
printf("1");% ^( {; \6 U3 m6 l( P
if(j==n+1)
7 D3 Y( H+ f r- D) n* H j--;# w- d1 |2 ^5 S; H" [
for(k=i;k<j;k++)6 ^/ c3 }1 Y; ~ O# }5 A8 ~1 S
printf("%c",a[k]);
9 G$ u9 B$ o* A. [2 {- d% C, ] printf("1");
( I9 \4 k0 o/ ^1 C- k i=j;
- I8 X A& b% |# k, p1 ? }
- q$ J+ a/ @, y0 n }* H# t0 g+ s- P6 y# C4 M/ Z
if(n==1)5 _. Q) R! M8 X
if(a[0]=='1')
& `7 Y. d' h% p printf("11");- a9 f% n, u; U6 g' [" C
else; ]5 T4 L& n0 B* h/ v+ }$ p( H. q
printf("1%c1",a[0]);& G2 o1 _- z" N* Q( b K$ l2 L h
printf("\n");
+ E4 o" h- ?) G: d }2 ?) p$ n; ~ Q5 Q
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h> `0 c9 U* [3 N5 g
#include<string.h>8 Y# i2 ]; m/ `. ~7 _' ^
void main()
9 N% w; _5 u+ h7 [7 E$ j{ int i,j,k,n;/ Q5 M( w, z6 X+ ]2 ^8 y5 H+ j$ K f$ U
char a[50];
. h9 v, u4 G3 I gets(a);
1 X5 Q1 f9 L) P+ l. r n=strlen(a);
2 Q: ] Q9 l( _3 ?8 q( l" U/ f( s! N- J: p1 `, E
for(i=0;i<n-1; )
8 a" k* ~ o* g k2 c if(a==a[i+1]). j1 ?' J% E5 b9 i8 O; T2 M
{ for(j=i+1;a[j]==a[j+1];j++);
0 W1 z2 N! H* U6 x3 w printf("%d%c",j-i+1,a);
3 O& F* \& a' q& J' U i=j+1;
2 w- g' P0 C5 G3 E1 P3 [) O, K }8 E5 _3 F6 b9 P6 R
else$ c) l: U+ }. _" t8 f
{ if(a==1)
( d* z4 O3 l- y: C( T y$ c { printf("11");
' {4 ~& D* F+ M* D2 J i++;
( e9 D: H8 H+ |# ?2 w2 \6 K5 g }* c' C( V" Q/ z1 T
else: K* \: h9 k7 Q7 _+ r
{ for(j=i+1;a[j]!=a[j+1];j++);
?5 S) n) N' N& U- t printf("1");) u% k/ H! y0 w
if(j==n+1)$ [) ]& U% L, L" s# q
j--;: e; ^5 T4 o y8 m( F+ B" N; D3 x
for(k=i;k<j;k++)
/ [" I% ~( X+ X4 D( U3 ?5 k printf("%c",a[k]);
7 B/ z# n2 U! ] printf("1");, P2 v2 j7 m: ~, P6 U/ s, x4 d
i=j;
1 W" P: p1 L% t }
u8 P0 J1 T2 b) J4 U7 s$ \ }3 S& d2 r. ]$ H+ }
if(n==1)/ ]$ u* K0 e9 _2 C& I- g6 T
if(a[0]=='1')8 A: @- u8 o+ H5 ?6 G
printf("11");7 J' u! m. V! w% q6 b) m
else
" P! t6 }* l( _8 }& P printf("1%c1",a[0]);
# z/ O& w4 ]. _3 I6 ` printf("\n");
2 Q9 l6 Q- S; w) { } example2:#include<stdio.h>
& q$ I+ o& m! V- u1 i( f& K#include<string.h>
' u% k& }- J3 A0 ~. Bvoid main()
& V- ] O2 m* Q) Z. z+ F{ int i,j,k,n; x4 U4 p8 ^6 e' t" {6 I
char a[50];0 q# I/ k# J V X2 D
gets(a);4 n2 U: V( G: P! f& c$ t
n=strlen(a);- R; v* f! W( h; m* |
O$ |$ E- p) ] for(i=0;i<n-1; )
( h8 C) U5 i) P/ \2 [ if(a==a[i+1])5 E0 B# s& L. q) \3 h( ]2 H" d
{ for(j=i+1;a[j]==a[j+1];j++);4 p1 B3 o) M3 ^; R/ \/ z
printf("%d%c",j-i+1,a);
' r0 q/ O1 k( U- H' N i=j+1;
$ Z7 ?% J7 R8 p- s }" O+ s5 \' v$ s! j$ d3 y% q* [( c
else2 v+ Y* |! M7 C" O. m
{ if(a==1)
& y2 D. m1 A. _* i1 A { printf("11");
0 f9 s% S6 [" o% n i++;
# z3 c; ^* n; i. |- Q$ q8 |2 X }. x9 `/ x% q4 k) \8 x B: Z
else
" r! t% A$ u4 D { for(j=i+1;a[j]!=a[j+1];j++);
0 ?6 y4 V6 Z* ]2 u, q# t6 ^0 _ printf("1");: {/ u8 E9 s$ {8 z$ I9 A! c
if(j==n+1)
/ R: E4 Y) l2 A$ U( @1 S, ` j--;
+ M H8 U" @/ `0 w5 A" }: V: ] for(k=i;k<j;k++)) C9 ?- R# g y2 [+ }) t, D2 M
printf("%c",a[k]);5 \/ {* [% P) k' G6 L; R* f
printf("1");* e. }+ V( X7 A0 { |' u
i=j;
3 [% S' E% ~& W' D% C }
4 ^& V7 o/ H8 a" ~6 } }
& A" \! Y% M$ R7 P/ O1 h" [ if(n==1)
0 d1 Y% E& J6 J3 F! b5 y( a! ^ if(a[0]=='1')
' ? ]& c/ X* N; p& C! Y$ z printf("11");" C* L; `3 c* L+ E5 w& X
else2 B7 V J" c( `. n1 X
printf("1%c1",a[0]);' { s/ L2 i6 g0 F4 s
printf("\n");) S# b: }1 {' i9 w
}
8 I0 ?8 t5 X1 b example3:#include<stdio.h>
5 x6 i" o$ J3 f/ i8 ^#include<string.h>
# Q3 f1 n" U9 R: o. g/ W/ {2 W ]8 Rvoid main()
5 O, a& K5 j3 u) C{ int i,j,k,n;
: O7 c; C: M' {! g8 u2 t char a[50];% }" o$ I3 b+ g2 I7 U3 q3 A6 @
gets(a);
/ j. N7 D, P; L1 h n=strlen(a);+ } h9 `1 G# @3 g# J/ R. q" n- L
6 d; J, h1 @) a' T' u5 C- I* d( t7 C
for(i=0;i<n-1; )
8 R4 s6 g9 t9 S: _1 |% K9 k& h if(a==a[i+1])1 _8 v5 K$ F* I+ b% m6 e6 k4 o
{ for(j=i+1;a[j]==a[j+1];j++);: q5 T, n# ~( ~6 ~$ p* ?
printf("%d%c",j-i+1,a);
' C% K1 O6 X8 E' N& L a i=j+1;7 ^% k% y" _3 A
}
. P. o( W' d7 I/ v3 d2 Y else
+ W4 X+ O5 l" K2 L { if(a==1)& z4 K3 b0 d- K" U9 l/ a0 ~2 [
{ printf("11");- S& C4 u4 r4 f- ^2 v& m( d
i++;
. z# h& j" U1 h7 b& Q& l }. o& `* B3 @9 y% {5 g
else
, S. Q) R) k! ~) ~ { for(j=i+1;a[j]!=a[j+1];j++);; k: F( i# U5 Q% [
printf("1");! U8 M9 o( V1 m' o4 i, j/ U" b
if(j==n+1)
% T' [3 u$ D. x9 l j--;5 A" G( k7 I" j" }7 y" J
for(k=i;k<j;k++)
9 K+ d9 p) y0 T' U0 h printf("%c",a[k]);
5 ^. z+ U, Z& K% b printf("1");/ K5 t2 {/ }% l& v- n/ S) ~2 I
i=j;
, C2 j! L5 J2 N* d: m. v }- B9 D f/ Z7 c/ b) y+ B
}- h# r9 W/ n0 @( V* V. Y; ~5 _
if(n==1)
# o- V5 [3 @2 ?3 D if(a[0]=='1'): P$ i( h) l' f9 B
printf("11");
' ?4 Y# x3 C" m. Q8 l; o! ^ else; C9 n. n3 f7 S6 K1 e1 G
printf("1%c1",a[0]);
5 m# N3 |$ D' s4 g printf("\n");
/ h; c; D2 [# E& n4 X9 F/ z' d2 t }+ j1 p, h7 \ S [
来源:编程爱好者acm题库 |