在线时间 5024 小时 最后登录 2022-11-28 注册时间 2009-4-8 听众数 738 收听数 1 能力 23 分 体力 77461 点 威望 96 点 阅读权限 255 积分 27165 相册 1 日志 14 记录 36 帖子 4293 主题 1341 精华 15 分享 16 好友 1975
数学中国总编辑
TA的每日心情 衰 2016-11-18 10:46
签到天数: 206 天
[LV.7]常住居民III
超级版主
群组 : 2011年第一期数学建模
群组 : 第一期sas基础实训课堂
群组 : 第二届数模基础实训
群组 : 2012第二期MCM/ICM优秀
群组 : MCM优秀论文解析专题
Run Length EncodingDescription 8 G: q2 ^- [0 O
3 I2 h7 W$ ~ Z& L7 S& t
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
2 l7 H# w# R+ N ! m& ?0 f% x# o+ F8 q
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 X! r9 d; q* e' N
$ o/ y4 O3 _8 h& C o 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
1 f: |; T4 {' ?$ @/ h* r4 ~$ g sequence, it is escaped with a 1, thus two 1 characters are output. ' {- i+ }$ j9 w+ d& H
% P$ P7 @" i, ?, n" O Input 6 s/ m. E( l% r9 ?* M" H2 m
0 o. c N, g- F( z' D7 z 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. L0 I4 I/ \" ]# B! u$ t
& f' Y y [8 k9 r! y' u2 N
Output ' c4 ^( y/ o7 n9 r2 \
( u3 J# g) Y8 s& O( j
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.
; m5 c4 D& A2 x- j E : d8 n$ S4 } V( [% B [* j% W, s
输入样例 . k0 Y: |& a: |/ f% r
AAAAAABCCCC* L M9 _8 l$ l& u+ b' P1 }
12344% k- a- `: _" K5 X/ _- N
& c1 e- W: F' u" E0 S1 r% Y , B9 [1 q9 w, S8 C$ x% f9 G( n
输出样例
+ V# |; H' Q! H6 @ H 6A1B14C
3 ~3 a" {; v% e3 P" P$ L. p 11123124
2 \2 i% k/ H/ h+ n3 U( l8 v4 V
- ^+ Y" O3 c3 @/ _& _: }
0 u* M$ N8 b H* Q$ a* Z$ U7 f/ s Source
' Z- i- y. O* N0 Y1 ~1 a, J
7 u+ |7 i _" H. X$ P) x# q+ s Ulm Local 2004
$ T; A% n. _* v, z% z/ p/ D C + d4 O& S, Z+ o9 U4 O
example1:8 t! e3 }6 w' S- r/ ?
#include<stdio.h>
, e' E; s6 h" {6 ~! M, T0 I# _$ e #include<string.h>$ R3 T; c4 J$ P) a, A% F
void main()/ H1 x! w% @: h& i; i% h& r2 n
{ int i,j,k,n;8 _) U9 n% N3 v9 X# |& b
char a[50];
t( `/ `5 _, h5 P2 z gets(a);
9 M* C% [9 C& O2 g9 o6 L n=strlen(a);$ H s9 B7 y- g" f4 N; d
4 T: f9 ]& z- f/ f$ c6 M% V9 V for(i=0;i<n-1; )! ?* [& J7 Q) Z! f9 \0 ]* q
if(a==a[i+1])
2 U" V s2 v, L8 T4 C3 U$ L7 R { for(j=i+1;a[j]==a[j+1];j++);
* x9 f4 E$ x( ]& V$ }2 @ printf("%d%c",j-i+1,a);
) c7 Q' h- F- \, o, Y i=j+1;' D, @+ \$ `& P; w7 \) z1 }
}( @1 ~$ ~" e) q- E& T( n
else0 q. W+ t9 \! W* v6 R2 c% n+ N) [
{ if(a==1)
9 m, g3 d2 C0 V, p { printf("11");6 g0 b" Y8 n e$ e. X+ C
i++;
/ E( Y+ l% ?5 `0 U8 s6 U; e' g }
: @0 k* ^( O! P! Z* | else+ |& E" {8 v" E: ^: E
{ for(j=i+1;a[j]!=a[j+1];j++);- _. V( J. J! [+ H8 g4 G- _$ [
printf("1");: I& {: H f6 l3 o' k! l* o: k
if(j==n+1)
9 g- T& G$ y2 v* w: r: [ [ j--;8 y ^. F+ V& e2 o/ m
for(k=i;k<j;k++)2 {" I3 o& ^: N4 {+ z9 s
printf("%c",a[k]);7 u9 q# u( T7 w3 \# w* s+ J
printf("1");
& H# q! q* n: r Q- X4 H i=j;
0 X! [/ } O2 M$ z. m }
' D4 a0 L1 b: T# |! K }. s3 L* ?" b( a4 S! v5 i* D
if(n==1)
( Z/ f1 W, } h( L+ t if(a[0]=='1')/ b' j/ e8 `3 a3 ^) L
printf("11");
$ a# q# o3 o+ U2 t. o+ g else
- w; C- Y2 Y+ ^ printf("1%c1",a[0]);$ [, k( o8 i' t9 h, d$ v
printf("\n");/ V" G% E' R+ x; F1 i r. {- x
}
, L$ j# l. I: [4 J 评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>3 ]5 J p6 a: M+ p. o
#include<string.h>
7 }2 q. H% ^2 E+ T' O9 \/ E3 d; C' U void main()# }, s* a+ F1 x
{ int i,j,k,n;
& p% x7 r7 P) x" I ?+ }6 @: n a char a[50];9 @5 i: u8 N6 F4 R3 Y4 D- {
gets(a);
. Q! A4 X9 J( M/ g6 I3 ^ n=strlen(a);
# G6 F8 r# d4 n5 v! F! p
$ ? ?& k6 {$ h" `6 W* H" } for(i=0;i<n-1; )
2 @- C& M1 s# n9 \$ U3 l, r$ T if(a==a[i+1])+ X- |, j8 T# T) h( L; s- N6 d
{ for(j=i+1;a[j]==a[j+1];j++);
1 ~* s& Z5 Q3 c8 ^ printf("%d%c",j-i+1,a);
4 h8 X3 P8 V+ T3 i* P# C) X i=j+1;
6 z5 N* R5 i" ]5 p' M }1 g4 O5 S/ d, p: }/ K, [4 o+ I- t V
else: i& X$ S5 A( t8 x
{ if(a==1): w1 R D: B) a2 b! N! l3 S
{ printf("11");8 T5 E7 b* P* U2 {$ _3 k @7 w' U8 O
i++;
1 Q4 e2 E! @8 P7 b: V: P+ P }
& e# E) V Y" C& _9 Z) d5 ` else5 M4 ]$ _1 F; C5 p" e+ {5 H
{ for(j=i+1;a[j]!=a[j+1];j++);
" P" g4 R# K, q9 _; D# p$ D5 Q printf("1");
+ X8 H+ h1 _- M" V! i5 `5 F( j, g if(j==n+1)
4 A7 D4 c) l) `7 x* f j--;
5 n: N/ I. b! T6 s$ ^# J for(k=i;k<j;k++)
u, s' ]# a* ]1 Z( J' U1 u printf("%c",a[k]);$ ]1 S0 r9 A+ l/ r
printf("1");) s9 ]" o- p! d; J7 U/ }' f- F& A
i=j;
9 r# c; \* o8 \, r }8 ^. _; ^" ^5 G' c% S5 b; F0 U, `
}/ V6 c) }: j0 d% I
if(n==1). o: v- ?4 l9 ~. u. s4 Z
if(a[0]=='1')/ Y( T& H: t3 L4 n4 k; {
printf("11");
% q. l2 s& @* K8 E: Y3 E else/ m) J2 j) c& a
printf("1%c1",a[0]);
9 f. p. j. R9 q7 a) e* z4 s printf("\n");
5 V' t, l1 i* ~/ Y9 e3 z; k: z7 v. o } example2:#include<stdio.h>
p0 R/ A6 l3 S3 Y8 \( C0 C #include<string.h>
+ Q! ]8 B3 ?% ^+ F3 r void main()/ L& {+ y2 e* O9 s" S9 s
{ int i,j,k,n;
" x! o0 m) V/ i! t# j8 n1 P8 L char a[50];
# u0 O* q6 ]( d gets(a);
5 L/ w' D8 S6 E5 l* q8 W n=strlen(a);2 q1 \0 E9 P g& N; s
5 r* w; I# _4 m+ R# p
for(i=0;i<n-1; ): G# V7 r+ K7 e
if(a==a[i+1])$ D3 g/ m5 `; b
{ for(j=i+1;a[j]==a[j+1];j++);
! Q* G5 f1 a( ]) m& k% L printf("%d%c",j-i+1,a);
6 ]' L: N* l% J. v2 Y5 _ i=j+1;% p- p; R. [& D p+ s q: {5 g, x k: c
}9 Q" Q; `$ ~& P2 N8 m
else& t& X! e& t+ t f1 R* y
{ if(a==1)+ o. l8 X* r1 `4 D4 ~
{ printf("11");
" i: N" A, u8 K; G% ]7 G3 p i++;
9 E! P |) a0 C- X" K+ ]# f }+ _: {. A( S3 F' Q$ j! X
else: ?7 s5 M! y/ J3 u# o4 q' l) O
{ for(j=i+1;a[j]!=a[j+1];j++);& F7 l G K U" R+ ]1 Q+ h$ k0 a( f4 Y2 N
printf("1");4 n5 ?9 w7 E# O. M) c, a0 w
if(j==n+1)
1 H, h" K# x! W j--;; Z! [$ ?7 M$ q O' L u) W
for(k=i;k<j;k++)
$ p+ I" S8 d [) F3 r printf("%c",a[k]);
* v# `* Z8 y$ E+ D. t0 u& x+ S/ G printf("1");
, I n( @) W0 ~& @: u, D i=j;
1 ?" Z, s1 W; X- ?" { }
2 C$ h+ }! A- i }
5 B0 F3 |( @* \% x) u& \ if(n==1)$ ~0 i. ~: B+ j [* _! z; I
if(a[0]=='1')
/ \: N8 n$ A! c: ^( c9 T: C& } printf("11");
7 x4 }: y6 `' v else
! }: x% J# g, R3 _( W$ Q1 a printf("1%c1",a[0]);
4 y2 W6 K* R- f# _/ b* k printf("\n");
; r* ~3 R( Q/ i) B, D, w+ y }+ I. O+ R6 F+ }+ a7 N- A
example3:#include<stdio.h>; k. s6 X; Y9 \9 ]% D
#include<string.h>$ i& u( Z3 ^2 k+ z1 v* h0 |8 B
void main()
% }, v7 ?, }6 q# c8 y { int i,j,k,n;0 W% t2 B4 f" M9 q# v" B U2 x5 w! K
char a[50];
0 q& E$ a; Q" b, j+ p. C gets(a);$ }9 F/ d/ }% @
n=strlen(a);
% A0 _( y2 B0 l U
! c/ L2 O5 V- A for(i=0;i<n-1; )
Q5 l i4 i5 Y" H# }- I6 R if(a==a[i+1])
! Q5 e9 U! a7 B o( R2 M2 I" y/ R- ` { for(j=i+1;a[j]==a[j+1];j++);2 F% c. ~2 O3 L Z. l2 v* s8 G
printf("%d%c",j-i+1,a);
% z, E. m5 W0 K$ I i=j+1;5 Y, c& H) W3 Y1 {6 Y( | m
}& I4 T u- O9 F6 u% Z9 L
else
8 W8 C: h6 k' g" k4 d# Q { if(a==1)
; L$ K# q; `3 @; `7 f- K { printf("11");
" a$ `8 }9 B) c& _) x i++;
2 T' G1 {& O1 b5 s/ E% s( K- T }
( t' v" B3 k* w$ C else$ ^; n* H6 U2 n/ P( I9 k$ j- s) X
{ for(j=i+1;a[j]!=a[j+1];j++);% @% _- p/ E1 Y
printf("1");
& M* q0 _* r# ?9 n7 l& ^$ c" F if(j==n+1)1 ^6 ~9 Q' h; ]6 i/ @% a* i: e
j--;
# G& f. t9 T( f0 ~ for(k=i;k<j;k++)& }, ], o( W$ f! i* c
printf("%c",a[k]);
, ^$ `" T' {: E# d printf("1");
1 X* ^) p# ^9 I5 S8 Y/ C i=j;# Z' h$ l3 K- F% W ~- D) [
}+ g; A( {4 a* y. g0 v
}: y2 Y8 W) Q0 J" L4 K
if(n==1)
, z! x( ` L, Q if(a[0]=='1')
* S2 n; Z7 M. B5 s8 g5 d printf("11");9 o! ~8 ?' f* x; Y" j
else
. W* b7 X* l+ t6 g. F% { printf("1%c1",a[0]);
) e7 z, u' H, {* K& t/ } t2 z" J printf("\n");
( f" V' t1 t" \ s0 e }+ F. x0 Z, r& r9 x: t3 }
来源:编程爱好者acm题库
zan