在线时间 5024 小时 最后登录 2022-11-28 注册时间 2009-4-8 听众数 738 收听数 1 能力 23 分 体力 77293 点 威望 96 点 阅读权限 255 积分 27114 相册 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
a" o. D( A* X
/ ^% E# s& Y/ W: ~0 p8 M# R Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. ) L" N% U6 W$ Y0 B% t' e& c
' A9 G5 I: j4 i) `/ S; W {2 ]" s 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.
; E) |! k3 N$ s3 V5 w8 h9 J " e) p6 V" ~+ w# y& x
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% V/ V" z sequence, it is escaped with a 1, thus two 1 characters are output. * }: w3 V+ G6 n1 t |
( l1 i2 ~; k# H4 [6 \ Input
/ ?' [, h8 l h7 ?
: v0 p' r. o: J 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/ ^* V" v# k9 ` 1 O( W$ J7 g0 \& e) A/ T
Output
7 g* M) V- l. F+ W5 K: `) U + A1 P7 [& f5 m9 n7 H7 `4 Z2 K
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.
3 ]. v6 B# y+ R a
3 s5 I+ E/ {9 S 输入样例
0 ~: x& S' w: C5 e J( l AAAAAABCCCC: }9 Y: p9 t8 R8 t
12344
3 B F# x7 s( @2 s; i
0 t5 C" m; Z5 y, k% ^* X: }
3 `; ]0 c" a6 U4 i3 A# {0 m 输出样例 . \4 x4 j4 a4 ?
6A1B14C
& i( E' Y7 j2 z3 P( i! J( ^# ?2 z7 }: H# | 11123124
) k8 M2 T- |; x1 j& A
. i8 v* w0 z+ Z: c- i
& f( @- ]3 E) n: O) r' W& K Source
$ ^+ c2 ~$ K5 R) t1 ^6 `8 @
5 J9 i1 L b- s* N0 x5 ?8 C! u Ulm Local 2004* o. g/ u, [# G; u$ p, R$ S9 z
4 F) c$ q( `3 l: g% B2 v6 m example1:) i- `& Q( J" H( C
#include<stdio.h>
# ]# U$ v/ E# y, P* o+ d* Z# f7 d #include<string.h>
# n" c7 |! h4 j+ E) [( J void main()
& n' Q- H8 `1 C# ^( |& { { int i,j,k,n;6 K5 L( \6 B6 e$ F2 b- z
char a[50];5 i3 g& h( b, g6 [5 X
gets(a);
4 u! j- J- I; X5 }2 F n=strlen(a);- _5 |" R% D4 ^0 N c: s
- n- T" x, i/ ~" { for(i=0;i<n-1; )
M. P }) N- w9 E8 j) g if(a==a[i+1])
* O5 z1 b$ @( {# e0 v* \; H% T { for(j=i+1;a[j]==a[j+1];j++);
% O5 o* J1 E, X4 N5 I' p6 T; Q printf("%d%c",j-i+1,a);! ~5 L3 x! w0 M
i=j+1;0 g H# M/ u9 `) S+ o; }% `
}: B- m% J3 U% G
else1 [+ p- Z- }2 w+ Z5 G) Q8 ]$ ^6 i
{ if(a==1)
% _: g6 e+ u {; E2 ~( x: Y { printf("11");* a5 h; y; u: c0 O$ V
i++;1 J o; E n& h5 K4 n: U: H
}
1 w$ z# I/ c; ~/ H else6 k3 ^+ j- r# B: `8 D4 P4 D Z
{ for(j=i+1;a[j]!=a[j+1];j++);
- e: L D$ a9 I+ y' b printf("1");0 U& W' I2 A+ }. F p! j6 E
if(j==n+1)( ]! V* J" |2 u$ a9 j; o) g% f
j--;
" a) O1 j. k0 ]8 \ for(k=i;k<j;k++)- W5 K+ r1 Q7 q0 u' h7 J
printf("%c",a[k]);
5 F7 S$ i$ D/ s6 F' u printf("1");/ K& d' y3 W" a4 _. C8 T
i=j;( S$ O7 y& Z6 {; {/ @: ~1 U3 g
}7 A3 D% f5 }, K3 d' `7 b, h
}! ~" [+ x& ^4 i5 W5 z, l+ d% V( i
if(n==1)$ j& }% n7 h; S9 {; K Y
if(a[0]=='1')3 c! T, h3 }3 I; v- v8 P$ u+ e3 x
printf("11");5 D# k+ c3 w0 n3 C
else+ w+ e# [! t, k5 V1 P, t
printf("1%c1",a[0]);
2 @ h f$ w; e- i9 K printf("\n");
1 s+ E6 u. g* U! m0 P- S }
) |7 t1 T* ?, C! E+ c8 M5 C 评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>
c, K1 z# U% F x4 g2 Q3 T$ `; p& O #include<string.h>* d7 l: r. W' `
void main()5 b0 D+ U; ^2 [: X- _5 G
{ int i,j,k,n;
% R; Y ^0 g6 r% e- z3 z3 M char a[50];+ y( {6 u3 g ?( P+ r/ c
gets(a);8 x8 p) L- o% ]' m" }1 n
n=strlen(a); p8 p, P. W( |" U9 V% f
7 v) U5 a2 j* i
for(i=0;i<n-1; )) u! E% V- H2 ^# H
if(a==a[i+1])) @' E W: u3 t- X) N
{ for(j=i+1;a[j]==a[j+1];j++);- p2 \) U7 }4 h4 L6 H6 t. Q
printf("%d%c",j-i+1,a);/ ~! k; n8 A5 `) b0 v- u( ?' Q
i=j+1;# x* m+ ^, _8 U- U! _2 K
}, f! x! p7 p7 s: u
else
: q! E# f* ^: C2 T' G/ h { if(a==1)
0 X; `9 L9 t7 q# W$ a { printf("11");
7 i. s7 w3 m- V7 \" N i++;. Q3 W- r/ b, s
}
0 S( ~7 _5 M9 | else
. ]; {9 f# t8 u% G3 ~/ Q/ x: e { for(j=i+1;a[j]!=a[j+1];j++);
$ \ Y6 V' t( e$ [. y6 w( D. | printf("1");& G" `/ q) W, D' C
if(j==n+1)' Y" x$ {" D7 T0 f4 t( _* H' {) p
j--;
: l' p7 m0 A4 o& R! r6 N$ t for(k=i;k<j;k++) h' @: p6 }) N0 @) W
printf("%c",a[k]);, u) w; c# U; `' |; H9 }) ^5 w
printf("1");
) ~) I. a; r& r i=j;
% u8 a% M- G+ n5 @3 _6 D1 A. n }
: G) ~0 J/ o! r8 W }
( o. \5 ?" T- j4 N! h if(n==1)! }) ^- A2 `5 k$ L; U8 t9 D
if(a[0]=='1')
. H1 X z u1 Y9 ]1 M printf("11");
* T# `7 r" \2 Y2 H9 o else
: T- D) }: ~2 Q2 s2 `) N1 s printf("1%c1",a[0]);
+ g4 [ W7 v7 v7 K4 G printf("\n");$ y/ S+ R+ E# ~
} example2:#include<stdio.h>% J( u. t, }. H* Y$ a0 t
#include<string.h> {! C: V5 o4 t2 z( U* o9 ~! ]7 a
void main()
3 q6 h; V9 m. A7 o1 s { int i,j,k,n;5 g! o Q6 S" g
char a[50];
4 r# A% G: n9 _" O' i8 q' Y gets(a);
9 M' ?- u2 v; B n=strlen(a);
6 |% R& N! p1 N% s$ Y 1 q9 J4 }. w) f( Y$ s8 Q2 p
for(i=0;i<n-1; )9 o* C) }. v( h# f/ i
if(a==a[i+1])
, p; u. X0 s- X% U4 y% j% O" L- o { for(j=i+1;a[j]==a[j+1];j++);/ |! `* H' b( b' {# N
printf("%d%c",j-i+1,a);6 M& _/ \0 m3 R! s h
i=j+1;
. ?+ P( T1 P" a }
) G- y4 d% Z* c0 Z: S! ~% x$ y else ?7 U; A: ~% v: `' g7 z
{ if(a==1)) Q$ }3 }' K* z) e: W3 M# C9 n2 S
{ printf("11");
4 N4 W! F0 m6 k) w1 J* b9 x% Z: b i++;; P) l! i; H6 C0 Y. ?+ F
}- f6 o1 d7 }+ e7 r6 D4 z. y) F* c
else( W! J5 c- N T& ^) B8 z2 ]% e
{ for(j=i+1;a[j]!=a[j+1];j++);. H6 |( U4 q1 |% B6 v3 N/ B
printf("1");
J( d0 T4 s1 Q! o if(j==n+1)
& A3 ?9 a3 i% b( B( ?5 ~* L j--;. y: T8 }' |% l
for(k=i;k<j;k++)) c4 ^* i1 F3 t9 p9 D2 n# M7 p. |
printf("%c",a[k]);
' K/ Y, K) G& r a9 e9 o printf("1");
% k6 y2 y# d8 n" g, r: I i=j;
& W/ b' x$ i7 B% c$ [# k* Q6 x }7 h+ K& ~) Q# B
}
( y- k ^. C1 n6 V& M# p* R if(n==1) ?, ]8 i! V# \! {
if(a[0]=='1')8 Q: \) B% k+ H
printf("11");
( Y n: K, u7 e: X* m$ n else
1 @2 U/ s. O% T/ p printf("1%c1",a[0]);
G8 R' C/ C" x2 V) R$ a printf("\n");/ H7 r& j% Q8 U8 {* p( I; m
}
D& Z7 L. Z8 R: r example3:#include<stdio.h>
2 K0 q, @2 x8 ]# e s% {6 g #include<string.h>
, D0 x! I) G4 \2 u void main()9 L9 q, M; s0 c n& _
{ int i,j,k,n;( y, F U. p7 E2 ~& j7 i$ c
char a[50];9 [. E1 T( [5 n1 \) W
gets(a);
8 h4 i; }1 E3 m) \* M, [/ Y8 f( h- k n=strlen(a);
. v& E- q) q% f
/ A+ {1 x' N+ R3 N3 i6 _ for(i=0;i<n-1; )& R) V: \: |& b: s
if(a==a[i+1])0 ?/ h! T8 [ |. B% |- S
{ for(j=i+1;a[j]==a[j+1];j++);
% E3 n; ?3 c5 b1 K# R0 q printf("%d%c",j-i+1,a);
# O8 [ Y5 Z4 K' E i=j+1;1 B& D9 Y& ]1 l3 m
}
~' Y. F u( ~, R: S else
; |% }) A, Q0 ^" ^ { if(a==1)# E" j. R0 s7 }7 E& c7 c
{ printf("11");: b0 n8 D+ [ N8 w' G! l
i++;
* O1 X/ s( T1 Y0 N }
2 N. M$ J5 _ l. O, X, F else
c- f6 l, j& ?$ b2 N( F1 ^, y { for(j=i+1;a[j]!=a[j+1];j++);. K+ a1 B$ F A$ K
printf("1");: m- I+ x/ S! F0 v+ T8 s* `
if(j==n+1)) M1 o6 O+ {9 f- k, T- g
j--;; f* x$ h/ U0 t) w v
for(k=i;k<j;k++)1 x+ `4 K4 u. O8 b" E8 v0 ?
printf("%c",a[k]);
4 X3 g# n1 c( _: J J; X printf("1");
w; R6 m5 h! ]5 J i=j;# [2 a$ Z' g* z y. N$ M& |
}
, @3 ~5 P3 w# _8 v! J1 v0 i }
. M( @( @1 l% V( ^ if(n==1)
+ O% A5 v' T. J, D& z S8 ^, y N* t if(a[0]=='1'). o6 b2 w' w H" ^& h
printf("11");
9 E; _1 b' \. ]2 K6 { else
5 u6 \3 [8 H l7 s, j& K/ H# M9 b printf("1%c1",a[0]);
2 u& k9 Z, O% N' g7 d printf("\n");
+ X9 ~7 ~. y# z7 O }& S7 Y9 F# K3 s1 u. D
来源:编程爱好者acm题库
zan