Run Length EncodingDescription 8 C/ ~8 d$ e& L; _) d( ]
4 P& M8 _. M9 n7 V! J; r( O
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. 9 ], A1 t5 W- y( d
9 o. b) i0 l0 v5 X$ d
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 v, P. M* U, c n$ X
y9 l6 p# `; L& ~' z1 BAny 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 6 K, e) e4 g: }2 n/ I
sequence, it is escaped with a 1, thus two 1 characters are output. . h+ a3 T+ Q2 n1 \# }8 Z
: z5 {) }5 T' a4 g
Input
' I C0 _! Y0 S3 r% X1 o" j/ g% l7 L
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. + b$ U2 G# l' C5 P9 j2 k
" l3 D+ g; d# R5 W/ G) V2 _
Output % q* c; p- ]$ i. u/ A l9 ]
6 E, Y* b5 x9 f4 R0 [1 }9 sEach 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. 3 q4 U5 i, `5 {$ Z
) E; K3 h, k- _; A输入样例 3 B7 j& |' V" X, P1 d2 Q, s
AAAAAABCCCC
! {0 d" q& B' g3 U+ f/ }123443 a4 n3 h4 r# U. M P
0 C7 `- |& i7 n0 R; D, |5 L2 v4 t+ p2 y+ q8 g
输出样例
! V! s! E/ W. v8 i6A1B14C
8 q, {! g' }. F& {9 A* K8 a111231246 Y% F# Z4 H$ F1 S
; W6 `) f* S8 k# w
& t6 _( {6 l) F* v7 t2 V+ q
Source
5 H- Y: u( p" W; o
$ [3 b' L" q4 H2 fUlm Local 20046 S3 [# J# Y4 h
/ s$ ?8 b; v, Y% s0 R1 b7 F! C
example1: K h, v: j! H4 m; Z& z3 {% E8 {
#include<stdio.h>
W6 }5 @2 u$ ]) g3 [#include<string.h>
: [ i3 q; l( a( Wvoid main()
0 o6 {4 Z* L/ Y$ m8 g5 Y{ int i,j,k,n;$ r1 @6 a# j9 c- M: ~
char a[50];
) i& U9 q/ k/ r# H4 N+ v. W% X gets(a);
% m" R G" ]3 N: O/ A n=strlen(a);
( E$ M N* H+ L5 @: ~9 f% ]( U) ?# F7 j+ W; G
for(i=0;i<n-1; )* c) h6 n+ `) r, r( p5 m2 i- K
if(a==a[i+1]). }5 w! J: A) I: O, i( f
{ for(j=i+1;a[j]==a[j+1];j++);
9 t* t, H3 J$ z: }8 M printf("%d%c",j-i+1,a); w4 c& E% b' ^" E0 h2 S
i=j+1;
' O; y2 X, H: }# h# f# H }
/ k% f6 `. Q. F. Y' ] else
0 e( h- J3 Z: d { if(a==1)
& p& B8 |# O, I$ V" \, r& Z2 G { printf("11");; c4 Y9 M5 r3 l% y, r0 K3 ^
i++;# V2 w% _' p# n* D
} @- E* y% v) Q* l w
else* }& \' d* d' O/ J# d
{ for(j=i+1;a[j]!=a[j+1];j++);5 E# M3 ]% v/ ^2 D
printf("1");
' A) z3 j8 i2 H F5 o6 \7 q if(j==n+1)
5 }% g' u. [! T0 d! a/ F j--;
$ I) h3 T( }3 ^1 t for(k=i;k<j;k++)$ }* z0 _. y; D3 _( a' _. o. H
printf("%c",a[k]);
/ v' o$ U( L$ c: Q( Z) L printf("1");
: r; x, h8 j+ X% W8 ~ i=j;
9 R5 \1 l9 m( t) {) B }
; A& X( p4 \. v }
- e! |2 ?# W% K- r% K6 i if(n==1)% j Z1 @$ Q5 J: }
if(a[0]=='1')
# d% r3 u, d0 q' W; X. W printf("11");% F) T+ q: \& ~6 K' ^
else
: K, ?1 n* u$ I printf("1%c1",a[0]);
1 Z7 Q4 T2 B, ?/ C7 b5 j printf("\n");
9 H, s% m' N+ o5 B0 J }
. D( C% K3 ~2 l$ C5 g. u7 k评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>
6 E1 ~9 r" M# H3 O8 f#include<string.h>. ], y6 y9 y( r. n- V
void main()
+ _) @% b2 l! K m1 F5 n" ?! F' M2 C{ int i,j,k,n;
2 f8 b) ?5 S! j+ K% `$ y char a[50];
; {3 G6 |" f% d& N/ j7 e gets(a);# z/ O( m, I# o7 v1 c
n=strlen(a);9 T+ {" D& T, S3 D9 i! l* |
8 g5 q* H6 D( J* p
for(i=0;i<n-1; )# u4 a; m$ a# p
if(a==a[i+1])% W6 W2 S$ e8 }2 ]4 ~! J+ T E
{ for(j=i+1;a[j]==a[j+1];j++);
% R' u7 g' E0 ?: g: W printf("%d%c",j-i+1,a);( ~5 I0 q% A9 H4 a7 E( ~0 L
i=j+1;6 I7 W& w. e2 f
}' E9 U1 `2 d8 I! A( Z
else
% I }( P% |; O { if(a==1)
0 E: V t Q# ]* ? { printf("11");( L1 ^! x! |9 s
i++;
- U. ^ O; s: m8 g2 C/ A }" M5 O3 x& \' [$ Z* _! ?9 |
else5 A& I* U) N9 Q6 N% u7 B" c
{ for(j=i+1;a[j]!=a[j+1];j++);
# u8 }2 J8 ]. L, m( T printf("1");3 b8 f; f- Q9 U) Q/ F: D$ X
if(j==n+1)
. l9 `6 F- Q2 M9 l j--;
) ~( I; I) C2 A/ z t for(k=i;k<j;k++)% ^' N/ z+ W h, E
printf("%c",a[k]);
[ P+ Z0 J6 @( U9 @+ f4 {/ R; D printf("1");
+ i; b: m+ y4 h4 }4 j i=j; N0 o6 E. x! I- b3 p' h
}
( \$ U0 M% B' K1 f! F8 C* P) l( b }8 {' Y9 `& X3 t+ E8 [+ d5 t
if(n==1)
' K# y- L& t7 ?+ T6 S1 U if(a[0]=='1') z2 b( A; h. ?* l
printf("11");
% X, g0 O/ I0 N" U! Y: k8 B else
; m6 y; W4 K0 l9 e6 |/ z9 l7 [4 p printf("1%c1",a[0]);
) O+ _/ s6 ~' t3 D( F* M; p4 S J& S; r printf("\n");
, ^- w* q/ s% Y* W$ o } example2:#include<stdio.h>( }, L; u/ ]/ e% J
#include<string.h>
; A% U' N# ^ z! ]void main()2 p2 ]/ Z* R% p3 S) q* F- m
{ int i,j,k,n;
7 I% ]# P3 h* E+ } char a[50];# R$ j) W' `' T" v! o) Q; E$ V
gets(a);
" ^! \, `1 ?6 o( M6 `" y ^ n=strlen(a);
, [* l% n9 S' I/ p1 E
) [) p, R5 j8 c7 n5 T for(i=0;i<n-1; )
: s; H; }, s* m- k if(a==a[i+1]); y$ j; b) L5 }7 Y7 C! u7 k; L
{ for(j=i+1;a[j]==a[j+1];j++);0 t+ r) a- w. ~9 x+ ]" U
printf("%d%c",j-i+1,a);
$ P5 I$ H6 X2 `3 P6 P i=j+1;2 D" t+ E! T. V. E) J/ V! J8 e; @ r
}/ g6 k9 ?2 ?1 |) p
else
9 U6 `' O% a6 x { if(a==1)
' Q- m" o; {. k. I. `$ s: h+ Y* z { printf("11");# H" }" [0 U. @3 o( G; s
i++;; L' V# Q3 [7 r4 r* O
}
. |3 L+ s9 H3 d- J3 O else
- i6 h% C1 X _8 m9 {: J { for(j=i+1;a[j]!=a[j+1];j++);2 w) \( W) w# Y
printf("1");
& \' _+ C0 n: M j if(j==n+1)
3 ^0 W/ G; K6 G* ~% x- ] j--;
% \3 A0 s, Q$ O9 Y5 g, L8 v S for(k=i;k<j;k++)1 N+ {& I* n1 d; H9 m
printf("%c",a[k]);
5 D3 B8 a% o2 D- E1 p. Z3 n printf("1");5 w1 x, X! @) n
i=j;
/ t+ y) a% c+ ?+ B2 v2 L }' w0 s7 W/ V9 ^$ v# f3 ] v
}: }1 f) I0 s4 O/ t; g
if(n==1). ]) L) E. E M' m! \" r- F( v& ~
if(a[0]=='1')# C! z0 G7 H! ]" J: ^, F
printf("11");( ~7 X0 Q; K$ t4 d4 K! \, r* Y
else
& W* V2 Y6 r4 }" ^* z6 [8 H printf("1%c1",a[0]);
7 R3 F2 e" T3 u4 g1 i- L printf("\n");
+ J* t. u5 W6 P0 \ }
/ e. F; m. \$ x D+ L t example3:#include<stdio.h>
. t6 J& A* m4 \5 q. I#include<string.h>: T: @/ q4 e, Z! D
void main()
, U. M7 R' o( Z# W) d1 M" m, \, \{ int i,j,k,n;
. S. e; ?% }+ X char a[50];! `5 O, C1 r5 Q' D: J/ u
gets(a);
7 K5 \/ i6 L+ n; { n=strlen(a);
3 e& ^4 _5 L! _6 E+ v: X5 p7 K' N- S! S. ^4 W) T h
for(i=0;i<n-1; )8 }5 C6 h5 V8 \9 d
if(a==a[i+1])
* h- H, c8 p2 _" t1 C { for(j=i+1;a[j]==a[j+1];j++);6 G( G- @7 i! {
printf("%d%c",j-i+1,a); p- X4 p, ]$ I' Q7 _* _6 ] e* F
i=j+1;
6 i5 d# S. x: H+ e: d# C3 u0 L; G }
' Q! b) r0 K- D, V6 U else
: ]8 c& p: r- o# E6 [ { if(a==1)
' z# f! S* m" a4 i# k8 Y7 ? v { printf("11"); M7 ] ]3 `) s D
i++;0 ]. F; {1 x; ]+ a$ c: e( N7 f
} }5 D# U& |- L, r! l7 z
else3 F8 n! a2 f) v$ `
{ for(j=i+1;a[j]!=a[j+1];j++);' T( q# O% p) c; o+ S% \
printf("1");
' n1 q: q O4 S0 `2 d% F if(j==n+1)# k7 V5 u/ {# ]. u8 y
j--;
z# Q: s# T' o/ ~- U8 E' w for(k=i;k<j;k++)& W" ~8 t+ S+ D9 L J M$ K
printf("%c",a[k]); i. Q1 v) Z& u) _% a3 i
printf("1");. C2 p9 e; |9 V6 E6 n: Y$ _/ O
i=j;
, G, q) D( r* e8 M, w- D3 l* B }
+ t. j9 D+ _' n% n! X& f }
4 C8 B+ P7 C9 R0 }+ F if(n==1)
; D7 A; q# [: q9 z: [' b if(a[0]=='1')
9 H3 D+ a, j0 I% M% W printf("11");
4 L6 A g) Y: D& {8 i8 I/ x( M" G else: o& g8 }# z5 W! O$ f$ |% Y
printf("1%c1",a[0]);
8 ~: _. q, q4 {' o- `4 E printf("\n");
& Q' l! m7 a5 a3 s. F- I }% B/ @5 t' u* ~3 e' v, @0 v
来源:编程爱好者acm题库 |