Run Length EncodingDescription 2 g9 ~ M+ f: C Q+ R% V+ J
: t. \' y2 D! D0 NYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below. - L% c- v9 u& x0 S$ g# |; _
2 X c# f. ^) |* Z; \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. % _4 H2 T' O- @+ b+ w
9 T. u' I+ P; u F2 r
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 . y4 I) R* K2 v7 A! ^
sequence, it is escaped with a 1, thus two 1 characters are output.
4 B2 U! {5 y, l k p- U/ |0 m$ l6 G" i4 N( I: O: G1 Y8 M
Input
1 \$ C% x6 t( ?9 p4 G( W9 j
; J) X6 z5 U8 _1 x# b# X) EThe 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. - D" v+ J2 J z/ q' E
! [9 o8 @! m1 }* W( ^4 \Output
* {# V# @2 n: [6 y, ^7 q+ U9 l5 ^* j2 r0 P3 t7 T4 s) Y. {) d
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. 8 ?9 i1 V/ [* o' _
5 A, w" V9 p- F- z+ b
输入样例
3 T( a8 @" e8 e4 ^0 M% {6 YAAAAAABCCCC, M y5 z% C: V/ I4 g7 }$ Y& ?
12344
5 w# X6 m; P+ A * Y1 {3 ]4 R, K8 J! U5 d
) h- L! }$ H0 V5 A% y输出样例
# J1 H! D0 Y$ ^6 @9 \6A1B14C$ P* i k, t) R% C2 q8 [/ s( Y
11123124
# ~/ R" a- s3 n5 |' L' K # s8 A+ J! {% n$ a% }3 t! O$ f( A
5 x- j( P6 X7 Y8 g
Source# w3 |0 w; i$ c
2 \2 a: F; v4 R3 ]6 u6 }; g
Ulm Local 2004
9 S) p4 C( e& V6 f' a
! m# w9 |8 \. j' J* i3 Zexample1:9 z, L4 g) c' Q$ q O& Q; Q/ _
#include<stdio.h>1 Q: k! m3 V T6 X$ r( t. w! @
#include<string.h>% k: {4 V' \/ [- r, j, |4 }5 k
void main()
& [! B! W) H0 Y; J% d G{ int i,j,k,n;6 p* Q) y& l [9 r
char a[50];* ^" x7 I. b$ @6 R0 R
gets(a);" ~& x: {% C; A) p; ?* J4 h: W# M
n=strlen(a);! y: G& r9 H# v5 u) \
# J3 U$ \7 i4 g/ c# x0 P
for(i=0;i<n-1; )
- J, j5 f/ V; ^9 j# A if(a==a[i+1])* | K, T( U" g; X% S V" Z
{ for(j=i+1;a[j]==a[j+1];j++);
. n$ d9 u; C6 s9 u0 Y printf("%d%c",j-i+1,a);6 i& a+ {* Q. d' T
i=j+1;) ]$ `) o C3 y
}
, d4 ^) t I9 j, b9 q4 v# @ else
6 |! a1 {0 U3 h5 u/ E9 `# e1 i { if(a==1): Q" B' l3 s% D' I/ J% e
{ printf("11");. l+ w: v, q2 g W/ c" b+ s
i++;0 ^) V8 L8 d# ], I' G& ~" ?; Q% N
}. z9 r' r9 q# u* Q% T1 f
else' g3 |' r. I9 N3 \9 |
{ for(j=i+1;a[j]!=a[j+1];j++);
' o8 r! s& v( P# k/ ] printf("1");9 D, y' H! T. t6 N/ q2 w, C; f R
if(j==n+1)" C; i3 i6 A3 P% @; Y% e
j--;& _3 } p: ^0 L* i/ `
for(k=i;k<j;k++)( v( A3 H; b7 t( ^
printf("%c",a[k]);
* [4 N3 L2 R" [. z5 k' v- ]1 s9 o printf("1");, T7 r( D5 L, r: @8 q
i=j;( A* N1 |1 Z, z' J& T4 w2 ~4 {
}2 J3 k* Q7 ?# D, |
}
; R" S+ d; Z5 U9 N: H+ c if(n==1)
6 x; N2 j6 [$ f, f! ^$ h if(a[0]=='1')) I' A) G; U) ]# N* f& O9 V$ O
printf("11");
3 g& e2 Y+ [8 R/ z% ?: Y [2 P else
1 K6 M5 y4 l& I( T7 K printf("1%c1",a[0]);
- l5 ^; n4 f7 R5 g1 Y# J" P Z- Y" ~ printf("\n");
4 G$ O3 t* b; ] i }
% d- R: b4 h. F1 p; s. v评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>5 N2 ^5 n( J8 q8 e
#include<string.h>/ t: W% i, K5 U, |1 y$ {. j. C
void main()' z, w7 I# S, {8 S K
{ int i,j,k,n;
7 x" X y. B9 j/ p# D8 Y/ x char a[50];$ c8 f. ^% x" m$ u
gets(a);7 O! R% S1 q$ g& N6 c5 y$ u
n=strlen(a);3 X% m6 G Q* S* A/ ^' Z {3 [1 i
5 w. k/ |) Q+ n7 x' N
for(i=0;i<n-1; )* Q' v* H) _. w. L
if(a==a[i+1])" y' W' ?' |7 o' {5 U
{ for(j=i+1;a[j]==a[j+1];j++);
3 e4 u, n9 m! p7 C9 S( i" K, { printf("%d%c",j-i+1,a);4 ?) m: a. |0 z; L
i=j+1;- d. }/ D8 F% G7 e8 z0 I
}
2 {4 \1 K* _6 ]- ^! ^ else# ?, v$ f6 C/ E! S
{ if(a==1)9 I' Q' z% Q7 }1 t3 `
{ printf("11");
, l& _, ^- a: _8 x9 } F7 } i++;+ d! u: K* F' w- x8 c2 p/ t% i
}
! {. d2 ]/ ~8 ?5 T! a9 J! c else0 q! D4 }5 e" t; a6 W+ O
{ for(j=i+1;a[j]!=a[j+1];j++);+ I1 c* |+ G0 ^) z6 ~
printf("1");
w8 u v) A: c- H7 E if(j==n+1)4 {8 c' T2 m# G
j--;
+ i9 x% B. T: m for(k=i;k<j;k++)
8 O) B6 g3 k2 g* ^* B( ^ printf("%c",a[k]);
# W' s6 i: J* p printf("1");! P" }# K! v- g, D& R8 R
i=j;
! N8 I4 p) d' k3 X } E- v$ k6 `- y" N
}0 Z7 ]( F; @1 Q. D; b- x& H: R
if(n==1)2 C+ u6 B4 ?3 i3 p+ S" }( h. T
if(a[0]=='1'), m8 I6 z3 T; ?7 x: `% B
printf("11");! m* K# d, W! f: B; Z4 m/ c' L
else
, {7 |! \% S0 |. g printf("1%c1",a[0]);
1 ~, j T' f5 J; Z B& i printf("\n");
+ o3 ?1 A" A( I) z } example2:#include<stdio.h>* s+ |3 o6 m1 [9 _- Y6 H2 D3 F, N: e N
#include<string.h>7 J3 k) @9 Y' q7 n! k V9 Q
void main()1 d, W) H4 Z5 x6 U/ o# Q7 f
{ int i,j,k,n;9 f; z3 z: A5 H9 q; p1 O0 n
char a[50];1 B" H+ \* e2 ^- D
gets(a);' D6 d5 X& J( { `
n=strlen(a); ]' z8 `( z3 S( \2 E
2 J. n) z# S& I; e
for(i=0;i<n-1; )1 @8 ^! m& z7 X, @# o: h$ k
if(a==a[i+1])
$ F$ j6 j/ R( k+ [1 b5 @% _9 G; A { for(j=i+1;a[j]==a[j+1];j++);
: S o& Y3 o7 B; Q7 t printf("%d%c",j-i+1,a);6 p( | E; Y% f6 B+ p
i=j+1;6 d. [6 O9 }7 l8 R R
}
4 i1 \1 r" y5 V: I: ~ else
7 p8 a' G2 z, D9 @! e' S' ` { if(a==1)
! a, r3 g- T# i! e* u* q { printf("11");
) c* E! l- ~. A& G1 Z7 a i++;
2 _' [/ S6 n. K5 Y! k M, F }) d. \: E0 z, v
else& l! N& R4 ^! u) i
{ for(j=i+1;a[j]!=a[j+1];j++);& W& q. \7 [. ^, u& R8 R
printf("1");2 [ B4 V- p/ I+ H; p9 I
if(j==n+1)
! c3 |5 X/ l+ F! G/ _. o j--;
: z/ {1 h+ J$ v; a4 s for(k=i;k<j;k++)
( I& f( ~7 V& J6 W7 x) v printf("%c",a[k]);9 v6 e# @; K7 J% @ Y
printf("1");
" g$ d$ R* {6 c7 e; I i=j;
( u, n% D) ~6 j/ N }7 t2 m/ s- I2 d ~; n
}
5 ^- s2 ?+ M5 z if(n==1)0 R* D* g( ^ v' j- p
if(a[0]=='1')
' t+ q$ ^% |4 ~ printf("11");
% q" J. A$ I3 O. ] else
! G. a0 u8 ]/ R9 U% E J; y0 m! X printf("1%c1",a[0]);$ b8 C: Q0 \5 Z' B! O9 i o
printf("\n");* y h3 I& c7 \& r. _: s
}
" l( F8 ]# a3 X) z, C example3:#include<stdio.h>9 Z5 k9 a# r- d2 L3 R! s
#include<string.h>' A7 \" e* a* j+ `
void main()3 G0 Y2 d+ V i
{ int i,j,k,n;& X9 n. z9 p0 d: [( ~7 T6 s, j
char a[50];
- f! H5 f% ]+ P6 t% M gets(a);
5 i2 }7 y: b& z5 K4 C n=strlen(a);
/ L! c# [, v5 b" g& \7 x( P" C. J( I; A) r
for(i=0;i<n-1; )
5 l5 ~+ B, I) F7 N if(a==a[i+1])
& m- j' T/ q5 o. s { for(j=i+1;a[j]==a[j+1];j++);# N0 }$ R9 X, S6 k `+ c- b
printf("%d%c",j-i+1,a);
, H. _( W# D7 h" `* T& f9 b i=j+1;
& C: Q0 Y- w( p' ~* y }
: }& t( o7 W) p: e9 H5 Y3 t3 j else
/ F8 }" V3 F1 | { if(a==1), n+ ^6 s q$ A- G2 e
{ printf("11");/ q2 X4 V; I! {! h
i++;
3 U2 n: S2 `( W. \( a4 z) ? }( U# e5 I. r4 d( L
else1 `) n9 h5 o8 T Z! G
{ for(j=i+1;a[j]!=a[j+1];j++);
( C# P" o7 D, E2 r8 Z' W# L& I# A; h printf("1");
/ O2 q- o! X" Z3 ^) ^ if(j==n+1)
7 ?9 j. G& Q2 `) \- A9 W j--;% J. H9 s* K. ^0 ]- s
for(k=i;k<j;k++)" M' A- d- [ I' z% z3 v
printf("%c",a[k]);
( y3 E* T: c4 O1 n: }0 l printf("1");
1 l2 G9 d. X9 p' F2 R. ` i=j;
( P2 F7 G5 V- | }
( Q/ S0 Y( I: f, n+ b! u+ ] }
2 b" U* J$ F4 v- q; T% Y if(n==1)
5 q/ {! B H- Y" G. Z: ~ if(a[0]=='1')( l$ d0 S: D5 ?, m
printf("11"); |7 ]/ w. P3 L0 v% p% d
else
) L. D) [: A# p, o# r printf("1%c1",a[0]);
! ]% `) K. h' I: E3 \ printf("\n");
7 s9 o0 m, x+ q( U! l }/ {3 g& l* z. N; p0 Z- L9 w
来源:编程爱好者acm题库 |