在线时间 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 0 g6 B$ h7 D# { r4 X7 b, {* P
+ a A$ v C3 ^# |
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below. . z4 u8 F/ m1 M9 U) ^. c
8 C/ z3 U( M/ ~2 V3 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. 6 y% @7 j: W, p) ^) N: c
$ {: y2 P. r8 J7 O/ d 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 ! W/ |# o! p. b7 z9 w' ^, Q: [# E: T
sequence, it is escaped with a 1, thus two 1 characters are output. # G" x: S% H# M: {4 A8 m
6 Z) n6 J# o' ~
Input
, H' f- D* o( ?9 L7 n% }
5 h/ Q) H+ l3 \8 g/ U O 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. 4 m2 E, m1 B q: ?; C p& A9 Z& v" P, v
}2 R# t4 M8 A Output
0 ]% [) a' C. P6 h6 _
3 t/ N( Z- X9 v' c9 z6 g* c 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. 2 v2 v! T- C* y
2 b% [4 Z, a2 Q' Z7 x
输入样例
0 i* F( w6 F' U- F6 L AAAAAABCCCC0 _' |' V$ t7 `! ] j. i- ]
12344
& x. N+ g2 V0 u9 ? g" P
0 e. Z% {! B0 o/ w9 r
8 |" o' v* H( u4 k7 F
输出样例
" X# n: |% P9 O- u6 c/ g0 J/ L 6A1B14C8 u7 C$ H( V" U7 _
11123124
6 C6 ^/ H& U0 w2 K( `' b
9 s8 x/ X( Q/ C2 T' V# z % H* X* r5 I& ?0 E O8 Y
Source
2 `) B) ~; L: m3 Q5 ?# K* t , ]7 M, {# m, E5 i2 {
Ulm Local 2004
2 T, ^, u5 Z6 n6 w & u2 i' Z9 g, S4 X( v
example1:- z+ Q5 i' {, J$ J; f+ ~3 u
#include<stdio.h>/ A8 T7 h& U' P( X: x) W. i
#include<string.h>
& [0 A3 q; A+ w( v$ c void main()
: n- _6 \6 t4 |: v { int i,j,k,n;
( b8 L+ a9 d# b- \ char a[50];
* v& q* e) {) [& z1 g5 S, N& E2 \! D gets(a);( Z5 x9 p2 r5 h- \+ D) ?
n=strlen(a);
/ o1 Q+ d0 G' o; y 0 Z% T. t6 c+ o3 h! B
for(i=0;i<n-1; )/ a& O$ Y M r% z& l3 s5 x: F. Y8 ^
if(a==a[i+1])
) i$ C* C9 r) [- F V: U6 G. L { for(j=i+1;a[j]==a[j+1];j++);
! M$ F1 N D. o9 d* C$ [6 k6 j# h5 \ printf("%d%c",j-i+1,a);
. y/ j' X4 U" l! _, b i=j+1;
2 `( f- C5 A! d, ]9 C9 ` }1 P1 I$ @! n9 C. S) n8 e
else1 `6 m, v; b- i4 w, H5 q+ A! c5 B
{ if(a==1)
& i4 V: F5 ]2 B2 b { printf("11");
$ T7 H2 a2 M0 B& j7 _ G6 Y. R i++;
' j5 Q' F/ S. m D1 [$ h }
; a! b# H" O8 o6 } else
4 E5 t) V. G R, @- ]4 s: j( w { for(j=i+1;a[j]!=a[j+1];j++);- G' g0 s! d/ m6 ]! q6 }; W
printf("1");
8 U5 R( ?, R* L4 l if(j==n+1); W9 ^2 O9 w, M2 S/ M* ?# j
j--;5 d0 E5 z/ j9 h8 J# b, P- b- N
for(k=i;k<j;k++)4 a4 v9 h( ^$ s& u
printf("%c",a[k]);
; m' |+ U' S$ H* k3 g printf("1");
3 O8 ]6 r y X8 F: e i=j;
: ~" J; g2 f) D* a- p }% B' d# y: |2 k5 q# p& u
}5 @( S1 U* c$ G" V7 x( \
if(n==1)8 V+ t% N% e' N9 b- _
if(a[0]=='1')
* G' o9 j6 b/ f' R' u; \ printf("11");
: }3 u% L R% M else
* _5 h! Z& M' E printf("1%c1",a[0]);
% w+ j9 L+ ~6 D1 O! U printf("\n");
9 N b Y8 Y8 d- L# R) J+ M& Z }0 w# C3 }; {9 z3 Y) \* j2 i- w1 z0 D
评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>' M5 R# P# P$ n. y( f1 A
#include<string.h>
, ?* z2 ~2 P$ y+ W: O void main()
) `+ } K5 z& y: i1 O* Q { int i,j,k,n;+ D% ?9 g5 i& ^6 z$ A
char a[50];
) W- m7 M- L. R gets(a);
/ R5 ~: H$ _* r; b! D0 N n=strlen(a);
% o3 Z% P5 B2 o/ D4 f4 v. j
8 c! c8 n/ ]5 m for(i=0;i<n-1; )# B% E) A$ v7 D+ m. H
if(a==a[i+1])
% Y1 ^5 [5 L1 c+ ^8 G { for(j=i+1;a[j]==a[j+1];j++);
2 g% V; X2 a2 q6 b printf("%d%c",j-i+1,a);$ K- j! }$ \+ a
i=j+1;6 r& _: o) r" P2 A, i+ X9 g
}6 _1 W9 W! I6 m) O/ z# h/ r% b; C
else( `7 G6 H ]5 k* w9 \8 n( s4 b
{ if(a==1)& T% J q3 M* x+ d# W2 s
{ printf("11");& R/ M! K( ]: K
i++;
3 V4 ~2 F! [( P( O. x }. t D& }) E. S( c. \
else
: u* z9 r9 @+ b# O! ?+ c7 M { for(j=i+1;a[j]!=a[j+1];j++);, H& o' s- J @
printf("1");3 G4 b% t- T( ]. j8 h+ A4 ~
if(j==n+1)3 c7 q2 W8 b: T: d7 m# ?4 a
j--;
/ _8 Z' o1 @$ N: A. o for(k=i;k<j;k++)* w n9 B* w6 y2 ^. A/ t' q
printf("%c",a[k]);1 h/ ?0 R) L9 b2 P* L, c4 t
printf("1");
7 Q& d* \5 W8 R6 ]5 R& r8 {: r i=j;0 ^' s: J2 p' G6 n f; K
}2 h, o* Z0 o3 k3 g% f; m
}, _3 c0 i$ d+ P% I
if(n==1)
( x! b6 G" s% T9 [0 e7 _. u% B( E if(a[0]=='1')5 w3 q2 N5 M |0 y$ _' E
printf("11");0 k- d/ R) c# L
else% \( x( W* }/ V6 B$ T6 y& ^0 |" g
printf("1%c1",a[0]);
, t* _1 y! l, S. e% }8 C printf("\n");% ]0 b" u4 g+ G5 i2 D, V) c
} example2:#include<stdio.h>- s/ S9 n6 S) \6 u/ l; d* c# x$ j
#include<string.h>5 k q1 P$ O+ ?7 [: c1 ]% E
void main()
$ }8 f' ]4 i, C+ ^6 G6 s8 K { int i,j,k,n;7 ?7 [( M8 U" Z8 y. y) p4 T
char a[50];6 `5 s( X$ E% ]0 \2 P1 s/ k
gets(a);
0 [5 m& z1 j" P! m+ p n=strlen(a);/ ]& o* N% I# f0 f0 e7 `
7 S" y0 G: A- g% G6 Y$ w4 y" ?+ ?4 K
for(i=0;i<n-1; )1 L9 g4 U8 {3 o2 V
if(a==a[i+1])
$ V8 s. O7 ?& r4 a) d4 w, ~1 L { for(j=i+1;a[j]==a[j+1];j++);
6 {9 g' y$ p' w; K printf("%d%c",j-i+1,a);: E0 ^" ]. r0 \7 Y0 a
i=j+1;
6 ~) b Z4 o) ?' l2 d$ z4 M' y, J }6 P9 g$ _6 E- f! [
else1 ]0 \! f- g" J: `' B4 o$ N
{ if(a==1)
1 A0 f' Q D" `8 x* d, }& e* O4 I5 R" [ { printf("11");
8 t! t( r" S; p- m; I: |. M i++;
8 i0 e0 U- H+ o }+ k0 S0 I! a9 E+ s1 D* S5 v9 e
else
- M6 N% A2 ~. T( d/ v3 } { for(j=i+1;a[j]!=a[j+1];j++);$ K7 [) ^8 m5 [: O% d+ U3 N# t- E
printf("1");4 `1 m( n0 E1 c. l6 _6 l
if(j==n+1), D1 V2 @# e* U. Q* |
j--;- S. Z' D$ Q; t( o. G9 `) E4 A \& [
for(k=i;k<j;k++)
) z& W# Q0 J: O: v7 i printf("%c",a[k]);! T( S1 L. c9 `* w: X
printf("1");
1 i8 D; [' M1 L* |1 N' R i=j;# H, B; I- m! g% _1 f
}+ m+ p% h, x6 I/ n4 u
}8 g! ?5 d+ i1 n) S& t& b o
if(n==1)/ {: Y- b5 k6 K' z4 D
if(a[0]=='1'): N; l6 ?; K& f: h
printf("11");
. w8 N& K& j) ^( \ else$ ?1 x6 g8 P" T+ ?& D8 n$ I O& y
printf("1%c1",a[0]);
9 o* e Y& n- V printf("\n");
2 D2 ]! I5 h0 \6 \# O7 d* ^ }; {; }0 f/ C ^! v/ ~' Y' {5 Z" k
example3:#include<stdio.h>
. w6 y2 l( Y5 ^0 ?) R" ^ #include<string.h>
. L7 U( L: H7 u) G, D2 t) o! p% X void main()7 A; ~4 ?" v5 b9 c
{ int i,j,k,n;
) D: w) J2 p: R j# Z4 w char a[50];& z- P9 u9 W" G, x6 f1 X
gets(a);+ F* r5 G; p) }$ R, t2 W
n=strlen(a);) I& R% E0 o( E- p: F4 f6 }) {( l
8 h; R, E: V- q" |: U. N- I$ e
for(i=0;i<n-1; )1 a) r) J0 Y, D4 l/ n& u
if(a==a[i+1])
1 T9 V* V+ ~( x' M* C/ G! e { for(j=i+1;a[j]==a[j+1];j++);
4 X- G ]# g* h6 G D: l4 }# | printf("%d%c",j-i+1,a);8 u i1 ^; k' s/ y
i=j+1;1 F I6 o" j$ \9 J4 O
}1 a- H8 ]& a4 P3 _0 [
else# m5 P& |3 `# S4 Y3 \5 N! k
{ if(a==1)
2 u8 P2 ~& M" j3 E( U { printf("11");' s5 y# ~; p! D5 r3 u8 o' ^6 X% H
i++;
" y2 {7 {2 d0 j }( f( n' y% ~! L% p! { x+ X
else
# I* g% J4 Z* p4 b6 J$ ~( V { for(j=i+1;a[j]!=a[j+1];j++);
: t9 \3 Z( k" X* j4 g5 z. H printf("1");
% b7 f' a* c$ B' |$ N/ i, u% t if(j==n+1)% Z* N+ ^7 x% [
j--;! d9 O4 |$ p% E/ F' t# Y
for(k=i;k<j;k++)
0 C* X, S% W0 C! N& M, n/ J printf("%c",a[k]);
! r+ {$ \# p- I# F, j8 h9 X1 W8 [: K, [ printf("1");* `/ ]$ F" n9 `, e3 p( a- ]
i=j;! _5 P; X4 H6 t& d
}
! q' o6 g4 e+ d }
* o7 N' M( g6 q5 x if(n==1)
5 k$ `0 k8 u8 O' [. b* O* n. k; I* x if(a[0]=='1')" A' j- R5 i9 W) _2 j, Q
printf("11");
5 m. I7 d& Y0 L1 w6 K8 I9 D else( `1 L7 g$ t0 T. y {: C' i+ n
printf("1%c1",a[0]);
; M5 }, s, Z( D9 X+ J, ^ printf("\n");! a3 g& x4 B: Y/ N! W$ X
}3 q( F3 X* L! `6 F, a
来源:编程爱好者acm题库
zan