Run Length EncodingDescription
3 I! M& O8 h( h0 C7 S7 z% S9 a/ y' ` W0 p: W+ {0 }; M
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. $ U( _0 P+ ?) E& I% k
; J: O/ x& R" R- ]' y5 OAny 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.
' ?6 {2 }- o& }' [ m# @+ O
$ g7 }+ i0 V8 W* V2 e/ `6 KAny 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
2 X5 Y8 J* N4 V: O( D7 V% _* s$ Nsequence, it is escaped with a 1, thus two 1 characters are output.
9 g. q& Q2 Y" v: [
8 {! Z4 }+ r7 {% |& s4 K: NInput % ^' J/ p5 }7 t/ C9 O% b
9 T: B, V6 ~$ E, r0 `- Y
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.
8 s1 t- A- N- E; K0 J/ H
" @5 m# M8 i7 i& o9 z4 l9 D+ XOutput
: q6 u7 j- V) F ?9 t. U- P
U7 k2 t7 r& l( Z/ b0 H/ B% I w& EEach 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. % b2 Q2 |/ j7 V0 k
7 w$ }0 z: M, F+ l输入样例 % i0 F& J1 R5 c* s5 Q
AAAAAABCCCC
, t) e: z* p: X8 J& J9 S12344
* e F( m. ~" b" |- L
6 ?2 U) e. r. Q( x& w& l' P5 ?: \3 m
输出样例
" s& ^, B) [4 T- ^6 I2 z1 v6A1B14C" C3 g3 F- n1 M8 T( i; T& {
11123124. J* T! e: E& S* Q$ h' v( h) [
: j* @, P8 ~3 m8 c% k: m1 W: U o; E6 M+ Y
Source
5 A3 H7 ~0 A, l' k2 D) n% h+ {& K) U' N5 \; d. V; h5 e% |
Ulm Local 2004* h8 u; f0 ]9 f1 ]3 z
! }7 V* ~* U9 r9 T3 a. M% c
example1:
8 Y7 O& M$ ?6 s/ m4 @#include<stdio.h>
/ o! ~4 b% i% U#include<string.h>
6 D& {2 G z5 \0 @. svoid main()
& ?9 `# v" c! L- s& N7 q{ int i,j,k,n;( t" F( ^- _/ _* J! M: q
char a[50];3 T& y; N' e$ f' {$ t
gets(a);, }8 G- b. L1 c% l/ l. I
n=strlen(a);
7 b& f% l5 C/ }$ E$ \ @( }6 V3 A
1 i2 G, t; `. v1 N for(i=0;i<n-1; )
! t; n( A& Z& B$ K( R5 d9 p3 S if(a==a[i+1])
3 v; ^& m- V$ w# V; Z { for(j=i+1;a[j]==a[j+1];j++);4 M4 F/ ? W: w
printf("%d%c",j-i+1,a);
$ H3 \6 n# q4 I5 a# Q7 q i=j+1;! }2 ^9 t: Z% M/ G/ D
}
+ P5 v3 ~3 @6 o, |: @ else: l& P9 n. V3 L0 b+ _
{ if(a==1)) q6 H4 ]9 e- @2 u. Z% U+ X
{ printf("11");. C2 A r G9 M4 w6 H* Q, a
i++; g. p5 S* N: B: I
}
' k, [4 d" t: i7 x- y1 G else
7 e( q( Y$ F2 h( ^" D. H0 W! B( z) e { for(j=i+1;a[j]!=a[j+1];j++);* D3 l) o" B) C
printf("1");2 R4 W5 J6 O, p0 R% P7 P( W
if(j==n+1)+ L6 s# u5 L. N1 J4 a* n! i
j--;
7 X9 X" h c7 f) z, J for(k=i;k<j;k++)
5 g) n" I+ w) f: p: N printf("%c",a[k]);
6 Q1 V0 b Q7 g! V printf("1");
/ p$ e1 C8 [6 p. h% o i=j;# j' U9 X. v! ? T
}, o W0 Q: s: H9 |
}
4 a* K$ k7 [! t: p2 A if(n==1)$ a1 o7 h5 u( A6 E# L) b
if(a[0]=='1'): }9 Q. M3 b/ ` h; |! w$ A
printf("11");$ N0 e/ f; f4 g3 H I
else
0 U" Y9 q! E: L8 I printf("1%c1",a[0]);. w x# g3 L, f
printf("\n");& ]. T2 j3 v7 _ _; u$ p$ N
}
- J. l* _. L& o- _: K评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>, s n) F$ r- N4 ~4 ]! n2 X
#include<string.h>
" P/ G6 m2 ]1 N$ @, ^* hvoid main()
( q$ l6 ^1 o0 t1 ~: L& w{ int i,j,k,n;# E2 @4 W" g/ B1 n5 p4 i
char a[50];
- z5 B* Q1 |0 J* R" n8 S3 N gets(a);
! _* \$ Z& ]8 }2 z. S# d) n/ t n=strlen(a);
, I* n/ v% Q! s( U1 r/ ]4 x4 m s! g
for(i=0;i<n-1; )8 k% \6 W8 ]/ P/ b, W9 r/ U) Y/ E
if(a==a[i+1])
' z# f0 D! O( S" q1 B" o# K { for(j=i+1;a[j]==a[j+1];j++);6 D8 x( W3 ^/ N1 U$ n, a
printf("%d%c",j-i+1,a);) i0 t2 x" ~) W( a) M
i=j+1;
& Y9 D# }! [5 } }. |) U) w& a4 ?5 `- g5 ?, h
else! N4 A7 O/ A9 ~1 y" j$ u
{ if(a==1)
+ a, f0 I8 t+ T$ k+ ^+ c8 K, p { printf("11");
- H5 v9 P* b0 ^- G. }' j, T i++;
7 r8 ^4 u- A* F2 q! n) v) k+ [' J }; i9 l. S( u: d0 v
else
1 y* B5 }, D I. T" R$ w { for(j=i+1;a[j]!=a[j+1];j++);
' k) I8 v. G+ T& q% `$ M6 V printf("1");
: @' h9 _* G3 W% d) t if(j==n+1)- \# h; N: g: [& r! e
j--; q9 G U l* ]2 v) H; d5 i
for(k=i;k<j;k++). S8 v7 t/ @( }6 Q( P% l
printf("%c",a[k]);- w8 F5 O) j+ {! T6 g$ G
printf("1");7 s6 q& r0 F* l- }5 P
i=j;, g3 C# ~ Q8 \& |. x ]' P2 o6 V
}2 L( F( ~' C4 ? K' M6 A( X1 H0 j
}
2 j5 ~6 j1 c- _0 R$ a) D if(n==1)1 n: E2 l; k% @/ x5 b2 B& _0 I
if(a[0]=='1'), X6 b, X4 M* @
printf("11");, A/ v6 }& [5 M
else Y, I' E3 p7 \) j
printf("1%c1",a[0]);9 t4 h9 N1 P/ k7 Q9 b
printf("\n");
+ ?+ O4 u. _0 ]/ s! ~% B } example2:#include<stdio.h>
2 [" p6 U+ b4 Q4 v#include<string.h>
- K, V1 ^+ s! G9 `5 j# H9 mvoid main()7 ^5 t/ d6 B" g2 u% t7 R2 m
{ int i,j,k,n;1 V( e# |$ v1 {) S
char a[50];
& a) N" y% ` x6 x6 K- u% Z gets(a);
' R" n J0 ]# J' n. P. P) R n=strlen(a);' P' H- {9 n: Z4 U' @+ G) E9 e
; I/ g" h) C8 e J) q- v: C for(i=0;i<n-1; )/ l: k7 ?" v% U" D" J6 U
if(a==a[i+1])5 K8 m) H( U' A, |/ R" Z% G4 A
{ for(j=i+1;a[j]==a[j+1];j++);" R# \% K: l' M0 U9 i) j
printf("%d%c",j-i+1,a);$ ~2 F' i/ \, V; b
i=j+1;' {4 \/ t. g* U9 L
}1 [# @8 V& `0 Q; F& J j' z8 G
else/ n, p- j* F/ C% a* P8 Z f. A! }
{ if(a==1)
7 t& Q! f0 A' k( x+ m' l { printf("11");
( I9 ~3 q' ~ G i++;2 C1 F6 t) V4 h& c h
}7 w W$ H/ I |/ h( ]; _9 ~& k' r
else) z; Z5 U. ]) w! [, ~
{ for(j=i+1;a[j]!=a[j+1];j++);
' G# D% a& }, ~/ d" F. } printf("1");4 b1 I. p0 \3 ~) h
if(j==n+1)
3 r% g( n6 f5 |1 x# D3 L& Z4 X& H j--;
4 ?1 l$ E0 w7 _1 t1 h6 h1 F for(k=i;k<j;k++)9 i; n4 A; C0 |
printf("%c",a[k]);' w; f1 k: W$ f' ?8 l
printf("1");
4 E( r2 w. b5 ^* S0 i# j" U7 }4 Y5 M# G i=j;" G! {# n9 c. U8 ]- B! ^- V! v0 N) y
}
( z) \) b# P1 R( A0 @' a0 N }
. M& B3 b! z! {4 G if(n==1)
2 _4 ]+ `" f% N6 ~ if(a[0]=='1')7 ^0 d/ P- O/ c! Q2 _* u
printf("11");
+ w/ s* u+ m1 l* P else
9 w6 X B) _) A1 V N printf("1%c1",a[0]);- x* \. u; r: |) n
printf("\n");
) _# c- }% C1 X" O- D$ { }
- F! I: {; P& S+ Z example3:#include<stdio.h>/ S+ o; ]+ R/ O' m
#include<string.h>* K8 K- D, C J' Y# j
void main()
8 ^# m- i" |$ P{ int i,j,k,n;+ O- u$ f7 ]0 q- t) G
char a[50];
5 F7 T, @- X- a, y! t( J gets(a);' i5 j* B+ t% E$ M$ d/ Q8 m r
n=strlen(a);
/ k- {, y+ i3 |6 Q$ G, Y
5 ], |: o8 _! I4 s- w for(i=0;i<n-1; )
5 r$ U" R' H4 b! l D" V1 ^5 H if(a==a[i+1])
% H! v7 W. m, }5 H8 u) t { for(j=i+1;a[j]==a[j+1];j++);; Q! h' k2 R# k p" B k* F u
printf("%d%c",j-i+1,a);: L! ~) q% o, }$ F
i=j+1;
7 `3 F1 P7 O/ B) F. a }1 D* }* t" C0 q& ]2 F7 n
else
1 {2 {' P# X }$ V { if(a==1)) f8 w/ | g4 J) t
{ printf("11");
2 q! A y+ W2 g i++;2 |% Z3 ? C5 {. y
}+ i4 V2 K0 N, A$ ] r7 l/ g q$ N
else+ m. z K& n9 r/ p3 N* {. k3 P
{ for(j=i+1;a[j]!=a[j+1];j++);
- X2 B' f3 ^/ {9 ]$ H3 O printf("1");+ W% N; Y. u& k
if(j==n+1)$ o$ _! g# l: o) U4 C- k7 r" |
j--;0 W& E, M9 E" F% r2 U- M; }0 |8 L
for(k=i;k<j;k++)
& X: D# q2 Q8 m! X/ _2 O" D printf("%c",a[k]);! M4 U/ O( F$ I* F% V
printf("1");' P: K& @! {4 g
i=j;4 f" }3 C; w+ y. ]/ R
}
5 @1 j5 X- F, E' {! U# P& t }
; Z9 g! s* r' Q" v# |& M if(n==1)
' ^' c' ^* o% b! I7 K if(a[0]=='1')
s5 `1 R3 D$ q. D/ y# c+ |+ U printf("11");4 a: Z% R1 J& H3 p: F0 r
else$ l9 `2 c' }: N! Y6 f5 H/ |0 ~& d
printf("1%c1",a[0]);
* j* U ]* ?1 b1 y printf("\n");
/ {& c% ]/ ~* G" f( K- E% g; U }: a; K! r# g2 T. O8 |
来源:编程爱好者acm题库 |