Run Length EncodingDescription
! e, e3 `! X- g p4 O
8 c$ X+ h2 ]9 i- |Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. @& N: R8 X* v
- X! }0 J$ L: W- x, K' e& YAny 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 S6 j1 H }2 A# ]7 n! g4 g' U4 p$ J4 }% Q2 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 & N' ]! q( Y( |" A( w
sequence, it is escaped with a 1, thus two 1 characters are output.
! f( _! G' \2 G8 f2 _7 D, `1 `& S+ L
2 ^$ q$ U: E' i: u A0 Q( \Input 4 I, e) m" v7 x
5 {+ S7 a6 V9 {3 j% n
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. & g- l. f3 {9 A& h/ i# |
" w _. u& X9 aOutput % A4 V* V: B3 \9 t
6 ?3 A! J# j8 zEach 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. 6 l7 f7 A! }) B$ a4 l
+ p1 S! k5 d( C4 S
输入样例
" d+ [. t9 J8 m) t! Q- p BAAAAAABCCCC; v/ n& _8 u8 z
123443 H" ` Y) h5 w4 x* T
9 B9 c! c z3 J& Z
, g* ~% L2 r$ ^
输出样例 2 z5 H' c [- m/ A5 `: Z
6A1B14C
7 I: D, l, v6 u3 `3 W3 P9 u111231242 p7 x8 {- H9 m9 L4 n
: E* I3 T- t( q" v+ U8 ?
# m; m$ @9 _# D1 dSource% F% v. b- x% o4 [* \$ ~
$ O: i8 Y* ^% tUlm Local 2004
; {3 e+ p8 m9 v$ |3 G' N% M- B3 U' @; Z, H' w9 u3 F
example1:
x" G5 o" V F. ~7 m#include<stdio.h>
" X8 A! R3 g9 n' z#include<string.h>% M2 z( }- I% d
void main()
# X+ R- e# A7 B' N{ int i,j,k,n;
, ~; c2 |8 Z8 i( V3 R& y0 p+ \ char a[50];. l! W# ?' e1 r7 V* } f
gets(a);
2 m( d5 ~% K& _* Y+ n. l$ X n=strlen(a);
: C: f# `7 |. }
. [5 g$ Y' Q. Z1 P8 X. O/ } for(i=0;i<n-1; )
& a5 ]8 [. Z% M/ i if(a==a[i+1])
$ a; R( Y6 U1 t# G8 n) H$ K { for(j=i+1;a[j]==a[j+1];j++);
9 X5 C+ e0 ?* h, a printf("%d%c",j-i+1,a);1 [5 O4 h( \: k: @& r; Z
i=j+1;7 I) b8 D( g R6 q
}! O- d3 ]" _6 Q/ w, M( _* X1 ^% D
else
) {! b: ?+ }6 g6 V8 y% N { if(a==1)
% y: y$ [1 y) i1 _ { printf("11");
$ z, S. j) i# i( c i++;
. B, W- j* { [8 u4 ? }& I$ \! g( y% p M! c! r5 A
else
( ]" v' F G- q. v { for(j=i+1;a[j]!=a[j+1];j++);1 R# W4 V$ h0 L3 b- a/ u( _: i
printf("1");
! N% f3 R* X& X& L if(j==n+1); o8 [* W, ^" R
j--;) J; w$ x9 A% }* b2 S
for(k=i;k<j;k++)
2 W* J# \( q& a' G printf("%c",a[k]);
( c y& G0 O0 g4 E# L) T* x printf("1");& _% B2 i0 [) Z) N E0 ^: q$ D! l! B
i=j;
2 w) g6 ~- Q2 Z+ n! u( f3 ^: k7 V }
3 J) P; Q; L, D' G5 Q }
/ l, I) X7 |" d' B# e9 B5 q if(n==1)
2 G& A% ~- i: A! G8 a, G5 F if(a[0]=='1')
' K' Q6 l* ^8 F" ^; Y* T' P7 J printf("11");, R( T6 B7 e6 f& `5 Q! b
else
7 j6 Q; i" G* I' x) ]4 b* D printf("1%c1",a[0]);
- h) F2 `0 q1 S# `# e printf("\n");
9 o+ q; ]% C1 k( ~* O" ^" @& r' W }% N3 Z0 b, p6 [5 B# M1 B6 j! g
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>) T! A$ i& ?0 `7 q. b
#include<string.h>
* b" R* b" Q( |1 U( i) Y9 j. n, h8 uvoid main()# r b: {. c5 o
{ int i,j,k,n;
1 Y: \( }2 \! ~8 k* p+ p& [ char a[50];9 _( T. i# f6 ]3 j6 U
gets(a);% v6 b# J, O: Q& p" F7 P; K# g/ Z, \
n=strlen(a);
# e Q2 I1 w3 S& e) u5 Y3 j; h
9 M( g5 [5 P- U" @, Z5 a& l for(i=0;i<n-1; ): ]: y/ y5 W+ Y6 y9 o
if(a==a[i+1])# [- T) F. M: V: z. E! t
{ for(j=i+1;a[j]==a[j+1];j++);) z4 a; q# l6 \; K
printf("%d%c",j-i+1,a);
1 p0 p9 G/ a# s8 q, A i=j+1;3 l8 V' l( S: a* j5 g0 h. p6 u
}
+ S4 X. f: n. X. G else
; Z. e" x& W; B6 } { if(a==1)
+ A5 @* f# U5 p! x9 y( J* v1 P# g { printf("11");
7 i. x. ]/ k# W' d i++; g% j" ^- `( ]' P' |3 B% T
}
# o& Q& \1 X$ [9 u# N else
: f$ J5 L/ [7 n/ V3 v { for(j=i+1;a[j]!=a[j+1];j++);
2 T5 g2 k1 R4 O, Z printf("1");
5 B" S+ }* {2 k2 I; P3 ~, l if(j==n+1)
% R" J9 r, m9 M3 l) u) `" K j--;
1 c7 D& ^/ y( n8 I& Q, ~3 t for(k=i;k<j;k++)
: ]# J* D9 a' r. ^3 _0 f* C- x printf("%c",a[k]);+ q; c' g. v5 e+ J' E7 K& s- ~
printf("1");
4 E- K& x: e* j0 N! Z6 W) _/ Z7 f i=j;. W8 J3 M# m, _! L0 y
}: ]/ W9 s. a/ @# b. o
}! i5 h! A' N3 E" l
if(n==1)
& S/ c; ~% @: U/ s- T' l2 p5 R if(a[0]=='1')
' ?+ V4 {6 `0 M+ i) V. k9 k9 b" d5 k" } printf("11");" F6 P& k, c% U0 o* ^
else
. M$ B; ]" V. E$ _# i$ J printf("1%c1",a[0]); y9 _! c$ I+ l6 f
printf("\n");
$ R* X3 z, _: |* B } example2:#include<stdio.h>6 q- P. O7 F: W: O* l# J* G
#include<string.h>% Q% N; i* S( H6 r8 }7 h$ c: p
void main()& E% P# I v3 F" Y
{ int i,j,k,n;
2 L' G1 {7 ^9 x& M char a[50];' b* U$ ~0 k' j% k" G1 {* D z0 c: g
gets(a);( ~6 o r4 Z' f
n=strlen(a);& L4 g% Z5 X- A2 A1 I
( |5 k0 _7 V$ Z( D) g+ @/ {
for(i=0;i<n-1; )$ @+ X% X+ |- Z! r
if(a==a[i+1])
& u7 ^0 U) E& b& B+ [: n7 W8 T1 d { for(j=i+1;a[j]==a[j+1];j++);8 q* @9 Y% a4 V4 E' g" Q# B. r
printf("%d%c",j-i+1,a);
7 G; F9 C7 c O( f9 N7 v c i=j+1;5 ?4 k9 C; q! X# Q1 a
}3 w) {& ^1 A/ b: c
else8 f! ?2 O2 ?- u5 _2 @0 v1 W
{ if(a==1)
. K' K! M' r: ~' V5 c+ g+ `- U; q { printf("11");
3 A) R- n& b7 Z& F i++;
$ ~0 w( C" @+ ~# S: T9 F7 _/ @# L }
7 n0 Q3 R& |3 d$ E9 {8 c" i4 c" h V else
* r( v: D2 d5 r% @" G: } { for(j=i+1;a[j]!=a[j+1];j++);6 r5 U, u( m: f3 C+ \
printf("1");& D+ {7 [; R+ H( v F
if(j==n+1)* S% N! O; P. y f
j--;6 N- U& u5 {$ {- E
for(k=i;k<j;k++)
# K+ U3 h; Q/ q% C7 F) Y printf("%c",a[k]);7 _6 V& ^* d! t
printf("1");. Z; A$ r7 D3 a9 w3 U2 n
i=j;
6 x( @; V3 D, y) y& n9 j }
1 q" G) o: [- {; U, G( x5 T) Q }% q8 D4 }2 r: U' W \9 k6 o
if(n==1)1 x0 d( }+ U# `7 u+ e" T
if(a[0]=='1')
5 r9 W r. W6 M8 _: { printf("11");: W: m$ N* w- r( p! C% v. x& ]
else
- |& o* Y, r6 \ printf("1%c1",a[0]);
7 W! b3 h% ?: ^9 T! |& r printf("\n");. F+ m& J- o: N' t" j
}5 `0 G) K V( R
example3:#include<stdio.h>* B' k( s, p0 x& r/ |/ h( I, D! S
#include<string.h>
, P/ |5 X4 j3 t( y0 U! Nvoid main()
4 |) o& _' S4 v# Y0 ~{ int i,j,k,n;
) r2 G4 _/ n* t. I$ v/ ~- Q char a[50];- u m9 V9 w' z7 C, M; X) R
gets(a);
# R# a0 x M, z$ X8 y# G, ~- e, G n=strlen(a);
$ X/ V$ v( t# `2 o9 K$ _! y6 t: W: z& y& r0 x" I" L5 G
for(i=0;i<n-1; )( z2 W) T- }% {- d
if(a==a[i+1])
9 L2 c8 v, d& z { for(j=i+1;a[j]==a[j+1];j++);3 ?( x3 W! l, _ N
printf("%d%c",j-i+1,a);
" O6 B; S! Y+ h6 a W( z* P7 C7 p i=j+1;
& U, Z. N G8 B+ X: Z }# K; V/ v' s- E* `, B) }5 @
else
- n! K8 D8 L: A: N6 I { if(a==1)! r. g F$ X* H6 V9 v( q
{ printf("11");
% }4 p! Y6 s% @$ s" ]" z& C2 H8 Z7 R i++;- Q+ s9 S! h0 J( N4 U6 H; J3 m
}
# U+ F+ f$ h9 L; S; } else" P, R: ^; n: X2 @
{ for(j=i+1;a[j]!=a[j+1];j++);4 _ x& x5 z# h
printf("1");4 b- P7 M3 f5 b) ~% t
if(j==n+1) L" K+ K: h# E9 B
j--;: ~ q& t+ i) J
for(k=i;k<j;k++)
$ s5 ^& [9 A1 E printf("%c",a[k]);
2 y/ l' j! q5 }/ V2 K printf("1");
* F5 O% i# v2 c) h; W( m4 h5 W+ f i=j;. h4 X; B2 M7 \
}
- ^$ H# m/ j% f; X8 C* a! e& s }
C) L) X) V7 D+ Z+ E+ I if(n==1)) S' }( {: R( h% H, e! y+ ~9 b
if(a[0]=='1')
* E% G4 r! S: j) r printf("11");* h/ N# N3 Y5 U0 Y
else( ^4 {% c% y0 C, v
printf("1%c1",a[0]);! G9 g0 {+ R5 H: h4 Y/ h7 F0 E
printf("\n");
) F% G3 ?" A6 t }
. m' A# M+ R5 H 来源:编程爱好者acm题库 |