在线时间 5024 小时 最后登录 2022-11-28 注册时间 2009-4-8 听众数 738 收听数 1 能力 23 分 体力 77429 点 威望 96 点 阅读权限 255 积分 27155 相册 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
( w0 ? |& s# f% G# e7 [- `3 d - Y# L# O e$ ^, Z" J7 ]% ]- J
Your task is to write a program that performs a ** form of run-length encoding, as described by the rules below.
& h1 V3 i$ l' t1 x
( I i, ` l4 J: P- ^0 |) A 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. . O, @& L" Y& a4 e. E
2 i2 }+ u" V0 C2 k) m% y; 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 8 H N: ^6 D2 b" W6 G" l* m
sequence, it is escaped with a 1, thus two 1 characters are output. 8 j. @+ ]/ H3 b5 c
( ]3 N0 ?+ V5 z, R% M Input $ G# N9 K6 S M! }6 z
" E* \! C1 t, _! [) `
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. , j. T7 M6 e9 }% p+ U
$ k# h* z" q! h3 u Output : j) Z1 R# L8 ^! k& v" L
) i% B/ E0 h x 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. : {! f/ Y: m& r0 m* r+ a
& u. i4 z( h+ D4 L; I
输入样例
8 F h0 a1 s3 t3 z4 C9 s AAAAAABCCCC$ J5 z; J" n8 s0 P5 F% g
12344
1 ]: z8 d5 [! q( @( E% h
9 b7 q) Y8 h* \, x# o1 K
" i7 X0 C" s& i, @2 b 输出样例
" v: _: J1 g" ?5 U/ Z 6A1B14C+ c% Q- X; p; t7 T0 e
11123124
+ K# F" c& J6 _+ R
* d# \3 o3 Y4 o2 p. o# H ; u( R! g5 R& B! {; n, g" H
Source6 X) R) [# O0 z6 S; K
X3 J* }/ O0 u1 r' Z9 b
Ulm Local 2004
7 T+ v: t* ?7 o3 h w; ]/ n( K + P1 U! g2 i6 Q( H* u6 j9 h( O9 d
example1:; m6 J4 o% o/ ?) A4 B6 h
#include<stdio.h>2 u2 L7 e; F: |9 y: R3 B$ I
#include<string.h>
' D, o. ?5 M T6 } void main()* G5 G( O, j4 f0 {
{ int i,j,k,n;% N% g& l9 A! C8 I8 B
char a[50];
0 P: `9 F. e% J9 y" n" T' L) q! a( v gets(a);
$ z0 n/ O5 S; f7 c n=strlen(a);
: [0 D8 l. N0 s9 X4 N% s; c* { 6 c4 `+ l( ]0 e5 _% B& m0 p& d
for(i=0;i<n-1; )
7 o5 H6 u U9 [( D; m0 @ if(a==a[i+1])& V0 h# u9 B7 B8 p
{ for(j=i+1;a[j]==a[j+1];j++);
; y2 L. v1 D P T printf("%d%c",j-i+1,a);( E" i @( g' ^3 F4 y% X5 f. `
i=j+1;
9 `. `2 D8 Z$ c: o" S }
) g" B% Q8 P$ N* R; x- ]! w0 x else
3 C. M- Y6 I+ {' }5 V/ G7 A A { if(a==1)
4 l7 Q" w( H2 K: I4 R! B { printf("11");
6 g1 P- H6 p7 d" [/ r! H0 f+ R i++;' o4 z1 S( n) ~& m2 g
}* v5 `2 }! R: |3 h+ W9 H: E& {) I
else, u: B6 r5 \2 K: c' z1 d
{ for(j=i+1;a[j]!=a[j+1];j++);+ K: o* A+ R% i5 d9 @
printf("1");
& z6 F5 y" z9 Z1 l1 O. J6 u# k& }7 r if(j==n+1)
x( R/ H: q5 s) m, Y* g j--;
3 l' ]2 n; @. E for(k=i;k<j;k++)
# T( c" e( A5 {5 c% I& O printf("%c",a[k]);
( U6 C$ v+ M! v0 } O! b printf("1");
; E2 v( W6 `( U" a2 X! ? i=j;
6 v5 L6 W0 k' o }$ {2 S0 T3 |) m0 \; A
}
: l/ [1 J2 y6 i) V8 X if(n==1)9 M/ z/ O6 z* B, d V
if(a[0]=='1'); S! V, n# F! A+ e4 t( H
printf("11");
5 `* X7 g# f$ ?3 x# S else* e$ z- O+ s% Y( ~( }1 ~& o7 t3 \
printf("1%c1",a[0]); S9 R! |2 L/ Z
printf("\n");7 N2 y( Q1 c- T/ I# r
}
( I! K! H7 x% ~# a 评论人: Colby 发布时间: 2010-3-2 12:04:06 #include<stdio.h>2 B1 M% z- O4 Q
#include<string.h>- `0 P# }" M' W8 V
void main()
* ?1 c, \- X$ z8 l { int i,j,k,n;
7 j4 Y$ E& B& k+ X6 Z2 ^9 { char a[50];
+ m+ a4 g$ ?0 | gets(a);
0 q5 h6 Y; y5 o+ A6 z! B n=strlen(a);
8 ~5 @- E9 {* J0 z; g
1 F. [! }) G6 `* \$ N9 B. f5 T- O for(i=0;i<n-1; )# I$ @& t$ }, G/ d
if(a==a[i+1]) y% ~3 |( P- v7 I! Y! L$ i
{ for(j=i+1;a[j]==a[j+1];j++);
+ V0 t2 Q9 l' c+ a6 u printf("%d%c",j-i+1,a);9 ^0 n' w( Z6 ^) ]
i=j+1;
, l4 a$ J' m. p( U8 L* @5 N% \ }
* p. P p0 D* w. s0 ^' _ else
1 i* E: B( K) g { if(a==1)8 [6 N: D7 m) Y4 H( q6 u6 H0 q
{ printf("11");$ q: d: ?0 j& U' D. e
i++;, i) j- x& P1 T. |* e: s
}/ Q( T% g) p8 k( g
else
/ S1 K7 l' Q8 l/ f' l: i { for(j=i+1;a[j]!=a[j+1];j++);
0 I6 O1 B7 Z, x I' H, N- }" w printf("1");8 T9 g3 Z8 y4 r$ F: R9 P9 W; C
if(j==n+1)/ [) t. M$ E) J c
j--;7 F- a H& ]: ?, ?: L
for(k=i;k<j;k++)
4 J/ b+ X" \8 s" R2 t0 Z- \ printf("%c",a[k]);1 E$ a) k$ G. z5 q/ R
printf("1");
' A% o2 u1 M! j! |+ ~1 n i=j;
" m# S: D! L# Z, _3 e }9 i3 ?1 x5 b. w& M, T- ^
}% U/ K( l( G5 }0 Z/ @' b
if(n==1)6 J0 `4 N9 n. J
if(a[0]=='1') X( w3 o1 k9 t) s8 c, ]
printf("11");
( v& i! o& M" r+ z D else
?; y/ J2 m5 a3 n. u% } printf("1%c1",a[0]);
/ I6 [, C. p/ W! A# J+ t- Z printf("\n");: C* i: S; ?$ ^) E
} example2:#include<stdio.h>( e0 p i1 G7 ?2 a
#include<string.h>! z x# k" l. u" h7 B0 T6 z. T3 p
void main()# I1 m- B/ U, e/ i* w( ~& P
{ int i,j,k,n;
* H+ ^6 c, M' Y: ?( m7 \ char a[50];* @# p1 o6 U9 q4 l
gets(a);: U( V9 y( L+ V& _/ X% k& R
n=strlen(a);
$ r, w8 i! s$ X( q4 P
: `5 F# c0 f3 N1 K* q for(i=0;i<n-1; )8 R& Z- s( `( d% J
if(a==a[i+1])3 A8 O/ W/ {4 L& W& z: f4 o; Y P2 M+ B
{ for(j=i+1;a[j]==a[j+1];j++);. P, Q+ Z( g$ {1 [: [. \7 l
printf("%d%c",j-i+1,a);/ x, x* {' f% s6 F
i=j+1;6 u; L' ]5 L. r: X
}! f9 r6 x j; d
else9 C+ e% J9 M- A% s# l* k
{ if(a==1)5 J6 Z, q/ j4 {2 {5 k/ _
{ printf("11");; ~0 v9 y8 j! a# f6 k& d0 u5 U
i++;; N4 k% S4 o0 s, W. s
}
4 i2 s# `2 i6 q% `0 F( ~3 U# A/ o4 H else) i9 v4 j1 ]" p6 f; S
{ for(j=i+1;a[j]!=a[j+1];j++);
- ]% g0 f$ l) e4 R printf("1");4 r+ u+ x! x! ?+ F# S( ]% Q l. ^# U
if(j==n+1)
% q6 A0 \+ U+ }# W% | j--;: G% S$ G i( h8 F2 }
for(k=i;k<j;k++)1 ]3 \ @- _- n
printf("%c",a[k]);3 O/ w$ N* ?& y, l- X( Q8 _
printf("1");
1 Q* f- u' E) H% V i=j;
2 @9 ^+ ^: {0 c. w6 W3 K }
5 s7 V& i; D, }% ?# q X }
1 F% d) H5 N, c3 b' i0 S3 t) B if(n==1)
/ y4 `% U! i) e" l- T if(a[0]=='1')' [' V. d- o. `: a* T
printf("11");
1 D7 n2 `: d$ T* _ else, A8 ^3 n6 p5 Y1 q6 b ^3 W) c0 y
printf("1%c1",a[0]);$ S$ M& y6 r1 Y1 C
printf("\n");( y/ k+ v6 [* y) U$ C, ~- d
}1 F$ Q, H A' t F1 D7 M
example3:#include<stdio.h>6 Y( ^) N, s& d |9 C4 ^
#include<string.h>
- f# K, L) {$ r8 ]( W void main()
0 O5 F- u( L1 `3 x( T, [ { int i,j,k,n;
7 A7 A2 A) Q: ?" T+ y) c char a[50];7 r4 n( V9 V6 R+ o- P
gets(a);
* W& v6 k2 c" Z7 Z6 A& M$ t% E' G n=strlen(a);( F! s( a# i- |0 G
; U; d* y# l# B$ `# ?
for(i=0;i<n-1; )
7 ?: l5 k8 e' R' j if(a==a[i+1])
" R0 j6 y; j; H8 w1 B$ r6 a& F3 Z { for(j=i+1;a[j]==a[j+1];j++);! y, W, f5 t) y6 z. W
printf("%d%c",j-i+1,a);
! I. C- n; }: D2 H: E i=j+1;
) F" y, G5 z& p5 j: b }
' K! N8 w' X, `! [: x* V, o$ I else
( q7 W! ^6 U* h2 R6 w { if(a==1)
" H1 J' y" r# g! l3 j# S' N, l5 l { printf("11");
- j" W9 D K7 ~' f" K4 J; s9 A4 V i++; j; J4 a* Q$ w9 O* \2 z' b
}
@+ k( y% i' D else
- D2 f, K( T4 D" _3 F { for(j=i+1;a[j]!=a[j+1];j++);
8 Z$ j9 I! q6 C% A printf("1");
' v. V: b/ H) \ if(j==n+1)* N4 z3 O, E- N3 `1 F/ d5 B1 ~0 x
j--;5 r- `! B* f- N' u0 x t6 y1 w. Q
for(k=i;k<j;k++)
+ v; W, f2 q- ^; d printf("%c",a[k]);: \# _% T' w+ s. k
printf("1");
) L7 s5 C5 D$ c) J. r) o0 ^% R# k' V3 V i=j;
$ L9 Y: G! w2 ^5 k, P }
) ?9 f* b1 U( t% B3 J$ z% T' ?- c }
% @! g4 t8 c/ Z9 |0 @- ~% g if(n==1)
) J) I1 J8 K+ D ] if(a[0]=='1')- a$ j" K: @1 Z( \9 ]3 D: [. h
printf("11");
2 s! r5 @7 q4 ]& O, M `. W else# c x0 B- `8 l/ J! |
printf("1%c1",a[0]);( h. z1 P$ d: _2 u* W/ y, S
printf("\n");
% o7 N2 `) G% v7 p4 _- @ }
) E# a3 M0 w6 M- A W. I 来源:编程爱好者acm题库
zan