Run Length EncodingDescription 0 Q' w+ c& p! C8 @7 E% g4 P/ W
+ `: Y9 S& ?. I. J* UYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
0 a8 O6 |: J$ m, c+ {( a% d: [7 s6 M# g2 o
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. 7 O1 i6 J4 {8 S, S3 q
, @8 Q2 p8 w6 l- i2 ~
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 " j! X! b" Z3 _
sequence, it is escaped with a 1, thus two 1 characters are output.
: ], x8 x3 F' V1 y& ?( D1 S* O. Q" |
Input ( M. G4 P1 i; P, { b+ G) j
: C' Q8 x7 d5 D0 c9 S( q4 l+ r
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. " O( {6 N# ^6 i$ y- T0 j
" E. w% M7 }* e$ AOutput ; o: }% g5 D" I2 R: A) g/ B
' e3 r. E$ W* u' v8 a! z0 KEach 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. 5 ~; }: i* R: d% C$ V
% I, p7 x7 w4 t1 t
输入样例
; V9 s2 w4 d: @' k( I; |3 p% ^, ]AAAAAABCCCC
: ^' }8 f7 F: o2 _, k; N W: I& R123446 G8 S# [! z9 X. Z" }8 A
+ q5 K S! t2 q7 T% U/ z- O6 H3 \, i
$ W% D' w2 R6 r9 U# L9 M. ?* h输出样例
8 ], M, o! x. h" q0 Y* _( C! c6A1B14C
% s0 U4 t3 d/ {% `+ N$ x8 o5 Q11123124
# E8 ?5 F" ?' S5 X3 j % U. W- {* P$ }6 J8 E a# v
6 M8 O) M: r( f4 _- R& G+ h4 g5 i
Source
4 `6 Z8 C5 g) m+ `: {' I& a/ A; r+ R1 r9 K7 ~9 k* P. a
Ulm Local 2004
; l+ g# D$ A3 @* m- a C2 Z% H& n A7 @2 ~+ d. f; S
example1:6 b0 g; S- P9 ~4 F) F4 ^8 e/ ?
#include<stdio.h>1 M4 I S* W6 }0 U3 i0 G7 E
#include<string.h>- v" f! _9 F) y0 J; }% T( F
void main()
' ], S- r3 |" C7 f4 X) V* V* Q{ int i,j,k,n;
% u/ D% q- ^$ o% A7 k* p char a[50];
+ o) C2 }0 K5 d3 m- l gets(a);
* R, n3 r1 o# l2 L: H* T n=strlen(a);" h7 i& R2 A; ?1 z) `
. g9 ~5 c J) k2 s4 P8 F, W, q for(i=0;i<n-1; )
4 O4 h% U* P8 U; t9 R0 c if(a==a[i+1])
/ w9 q3 ]* T7 E! c( G3 n; C( q5 {, v { for(j=i+1;a[j]==a[j+1];j++);
5 j% r7 l& f4 q3 \) Z$ X printf("%d%c",j-i+1,a);2 s! P" T9 b) N
i=j+1;1 \4 K- {% ?5 P9 ?0 A/ d
}0 p6 p+ O- @& }/ C0 {
else
6 A$ ?; F5 t$ _/ N% Q6 r H$ o { if(a==1)$ M8 H7 X% {- i+ \/ R7 O \3 U1 F
{ printf("11");: Z" b m" v- E2 Q2 x
i++;9 @% G& F0 I& i
}( {5 G/ l7 P: n* w$ n5 o
else
& `$ b( T5 P7 t { for(j=i+1;a[j]!=a[j+1];j++);
3 P' [1 e7 G7 Y% ~ printf("1");2 ]; i+ a: K7 k
if(j==n+1)
( N7 V- ]# f$ Y/ u8 d. r! m j--;
# I5 M" P9 m9 C- @* Q: w5 ` for(k=i;k<j;k++)
0 g5 u" _% [( O0 J printf("%c",a[k]);$ Q' Y/ ]# O4 P9 X2 k% e# x
printf("1");0 m8 G& j9 d9 n4 x
i=j;
" H( |& A) K. \ }
7 J0 ?/ H1 G7 _) H; k) j' J }
" p( o& N8 A6 r( w if(n==1)8 C6 n1 F5 @5 r& }0 Z9 ~% \) Z5 F
if(a[0]=='1')! s4 w% R5 |6 Z+ u0 ^
printf("11");
4 G& x; P* S* t8 n* y: F% t' U' W else
4 ] u% r* U- b- c" {: O: O* n printf("1%c1",a[0]);, |; Z/ v+ I% C
printf("\n");* F% `' R A0 q
}) v" x2 }/ h5 l3 O k' q
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>) m0 y, c2 a- D5 f5 N( N7 q" ^' F
#include<string.h>
) G, j7 g$ ?0 D3 _) \4 U6 jvoid main()1 o; T% x" I4 n- D0 d
{ int i,j,k,n;
$ |+ `7 j/ ]: H% u7 \. G4 ~ char a[50];
/ c5 d9 [& d/ U gets(a);
2 }6 ?9 g7 l0 w; r n=strlen(a);' g# y! Q5 i6 W" Q
/ H+ R7 e/ c- }+ Z% V4 W4 e [/ ~ for(i=0;i<n-1; )
9 M6 M- R$ Q: [1 u; H4 W8 C if(a==a[i+1])
2 i+ a& i+ f' B { for(j=i+1;a[j]==a[j+1];j++);
. J1 T4 c# P2 R" H6 Y printf("%d%c",j-i+1,a);
+ H C2 F* r/ _" ^ J: B' S( b i=j+1;
2 H. j! C5 D m9 X* p4 v1 s# t }; K" d8 F1 ^( m5 s4 S: f0 `
else
3 D# x" T7 R$ X) A9 f% T+ E { if(a==1)
7 ]" j+ \1 _1 y+ Q2 f# I { printf("11");
: M/ r* t A" ]! M: R i++;6 H0 I* \1 E( |0 y9 v- T5 v1 a
}
- h! F5 k# [- v else
# N% G( X; R- z l- a { for(j=i+1;a[j]!=a[j+1];j++);
}; R0 H$ Y7 }) S t' P printf("1");; `- H: z" ?' B6 q7 ` s/ ^
if(j==n+1)
2 c" g/ T' K' e5 M% u j--;
9 z! \( a* C9 R% O for(k=i;k<j;k++)
$ V- S, u% ~5 k4 q& U; t! k printf("%c",a[k]);4 U& S! x5 U! g/ c3 a; N
printf("1");
, } o0 D; [4 o/ y7 f8 e, q i=j;! W* A# e) w* o- |3 _
}
6 @/ X3 Z, o9 u- g* ? }' C" a' D; ]5 A: i" m
if(n==1)
5 R0 M8 v8 r G, x if(a[0]=='1'). f% s$ @6 }2 {3 q) @
printf("11");5 Z% L& G! Y3 O4 p6 V# u
else
0 s( |6 S/ `/ ~ |! U' y+ Q! A5 x1 q printf("1%c1",a[0]);/ [4 y4 I: Q X8 Z
printf("\n");& m! | x& u' {
} example2:#include<stdio.h>0 {; F' B2 C& O( h: D- Y0 w/ q. l
#include<string.h>
# S$ |; f' G6 \. A. lvoid main()6 T4 M8 e/ g( n; Y9 q. W x- Z
{ int i,j,k,n;
2 e8 Y( O+ G1 L i4 D9 r char a[50]; C' A) b3 b9 G: n* U& Y
gets(a);8 o" ~ I+ P) H) T/ \
n=strlen(a);4 h9 K: J5 J. V0 @# D) f
Q$ E! X( w* i3 c for(i=0;i<n-1; )5 y- g; m! l, C2 Y- m
if(a==a[i+1])- P7 a8 I$ c) G
{ for(j=i+1;a[j]==a[j+1];j++);$ \- }# W) f s, B7 q" U9 O/ D
printf("%d%c",j-i+1,a);
* h, L6 k# S8 R9 G/ Z: M i=j+1;
* l/ K! n0 ^4 U' @! }2 e }( _/ s" g% u9 c# u
else
3 B" ?; @. e6 F& J! j, n' p { if(a==1)
. W/ i) r2 j7 @# r) Q. V { printf("11");% D( U7 }. F1 G9 u
i++;
2 e: B# \, _5 v0 K }( i+ t+ H8 l6 I
else
5 ?( l6 v( Z6 Q$ J2 L { for(j=i+1;a[j]!=a[j+1];j++);, a; f. G+ P+ a$ N2 q; U
printf("1");, a9 @0 R$ G) c
if(j==n+1). _' a$ J; _. W! q% X7 b" k C
j--;
+ S! V! A- @- D+ g for(k=i;k<j;k++)5 E1 R* i+ I* N$ ~8 [% Z
printf("%c",a[k]);5 M7 g" t& M5 j7 N y' {* }9 W
printf("1");# Y* h9 f- x+ @, x5 w; \
i=j;9 B' _" }6 m2 q
}
" R* o: K/ R1 _2 | }9 _& P+ a1 d6 J
if(n==1)
?% z( f( R! d7 b0 Z; J if(a[0]=='1')2 e3 I7 D" ~) \) ^
printf("11");
; h4 @" `% K, s- w! p$ p+ \ else
/ p$ h* R7 `+ i) Q+ H printf("1%c1",a[0]);
1 r: A0 C; m( S, w$ U3 w printf("\n");
- o, d9 r( g: ]" { } W+ T: u5 ~6 w0 M: m
example3:#include<stdio.h>
/ P5 B# {9 }! ?0 R% J#include<string.h>
7 I# {! A6 S) Y4 U/ {+ a3 mvoid main()3 L: h* ~. _2 O( i- v O4 `
{ int i,j,k,n;
) a4 L; S! A( C% Z char a[50];2 F; ?8 `; b/ f* S7 g& f
gets(a);. U0 G# A% y7 r7 N* y9 l v
n=strlen(a);6 i4 f( S% {& j
# L# Q' A7 b ]. M: t for(i=0;i<n-1; )
* S: C) D8 h4 z, T if(a==a[i+1])
' S. m- C% M; S9 V { for(j=i+1;a[j]==a[j+1];j++);8 w2 B* k: V% |1 q# H+ Q4 a4 s
printf("%d%c",j-i+1,a);% |% x9 K% I# E. I
i=j+1;
8 l* C! i5 @. c P) p, l+ ]: @ }
! H. z+ v- Q) |' x x else1 \5 G& O( P; t' S& }% J4 k
{ if(a==1)
2 [' W1 [ w* i! x9 ~ { printf("11");
2 Y& t' Q' T6 h6 {# D0 u+ j i++;9 E- {4 ?7 \/ J% Z3 y
}2 o: c0 k7 r( {3 E
else
; s) H9 H* E5 {6 K/ f { for(j=i+1;a[j]!=a[j+1];j++);
3 m# @' p: L: f) M3 J printf("1");
9 M. u6 N' G& x0 L+ `: }2 ~3 j/ t6 u if(j==n+1)! q4 e3 O. e b- [7 N3 \
j--;
/ k' T+ A" S4 {5 Q0 O' z for(k=i;k<j;k++)
6 G9 f. ^" I+ @& r9 E8 r" u printf("%c",a[k]);6 y9 _$ W6 X- o/ e& Y/ e
printf("1");) }/ I- H q- L2 U
i=j;2 s. s4 Q& F$ E4 ^/ l
}
% I( t0 [- P0 ?& G0 c( I1 U. [0 t }' b0 L# V# Q9 [$ e6 Y6 |
if(n==1)8 F! g- j, U& U' L% Z
if(a[0]=='1')
/ |9 S; R" f$ X/ v1 ^0 b7 P7 ~, V printf("11");
, q+ O/ s) s; q/ j, Y) h. ` else
9 v7 B* s; k: B+ x' a5 ^ printf("1%c1",a[0]);
3 @4 |: G* ^9 {! p3 f printf("\n");- S& L5 h" }% s9 s
}5 ~; c# G. @& m4 v
来源:编程爱好者acm题库 |