数学建模社区-数学中国

标题: acm编程竞赛题目2(Run Length Encoding) [打印本页]

作者: 厚积薄发    时间: 2010-5-6 18:38
标题: acm编程竞赛题目2(Run Length Encoding)
Run Length EncodingDescription ' H7 T9 y( Y  V

3 S% D$ ^% m9 f. O! G! M3 o3 O0 z& PYour task is to write a program that performs a ** form of run-length encoding, as described by the rules below. 6 H" X9 {$ m. R" r& @$ _

7 g- `# o4 m4 t9 l. c5 R6 j- KAny 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.
5 }: a1 y( ^: C5 \4 c2 ^
% B  r7 n* m# c6 D$ |8 aAny 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
0 T6 a: z* e7 L5 m0 X/ Ysequence, it is escaped with a 1, thus two 1 characters are output. ) `3 f/ E; m$ M1 f* f' }, g

+ m3 Q( F8 Q, r0 kInput
5 n% r" O$ E8 T  a* j: Z. `: A2 `* K9 D/ K4 |5 c
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.
. Z: Y' V5 h* d2 ^4 G, k! o9 ~, ]7 }6 b
Output
5 t6 A& Y0 e0 L/ m
1 _/ ^3 s+ z6 E5 R/ v) s- p+ d; N2 dEach 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. 0 ?. @1 ~* V$ M6 z' T
' \/ Q% E- ~" o9 ?
输入样例


/ \3 h4 L! E* cAAAAAABCCCC
% M4 U- _% R. V, c6 T12344" w1 i6 ~8 p4 @4 Z0 a


& Y. G5 U! c" U( `$ g$ F! F( B& u* ?8 r0 ~
输出样例


5 R) n! y; C- H6A1B14C, F" D6 v+ G9 q" J# i
11123124$ p: }& i6 M+ ~$ ?; W

  f, R& D. L2 k1 o# w. j, E/ R
$ L( K) `) g+ k) o* \' s: L
Source6 r2 c; j$ O! u$ \; N
; B0 ^' J# h! Z
Ulm Local 2004
: l+ G/ b* T, H' s3 S/ F7 W; N, L
example1:
* I2 ?" D* P2 }#include<stdio.h>
- @0 l( ~- k5 z$ N6 O* j7 k#include<string.h>
- Y1 i8 E! e) A9 xvoid main()* Q4 n: }7 }/ @1 f
{  int i,j,k,n;+ H; n) e& E5 s9 ]7 {' Z: B; h
   char a[50];8 L+ b* ^8 _. }3 [. {* s
   gets(a);' o8 T3 p4 P) S" ^
   n=strlen(a);
# a7 i) }; a8 ^/ M; B5 Y9 @; V" G4 v" o8 o0 I0 s( `8 P3 B
   for(i=0;i<n-1; )! x: r/ T* F0 {1 h' I  n% ]. F
      if(a==a[i+1])
$ e3 v6 B( R9 B      {  for(j=i+1;a[j]==a[j+1];j++);
' ~4 R0 @" `- H, J3 S1 w+ i          printf("%d%c",j-i+1,a);
3 G' T8 L/ W6 X; H& E% Q1 l$ [          i=j+1;
+ Y: T0 k& z/ f/ Y* h      }
! R- O& R! j3 D8 p      else4 b( g- Y1 B- Y3 _
      {  if(a==1)" Z6 Q3 x+ f  }4 T& S
            { printf("11");! G* Z# q, Z& k; z- Z/ t
                 i++;
9 g5 E. I# n2 }9 m* e2 Q. o. B            }0 I$ O& C2 l5 i1 T0 v7 K% ?
          else* Q: z: \7 f  j# f
          {  for(j=i+1;a[j]!=a[j+1];j++);
0 S, X* g9 h1 s; _0 \/ `2 N              printf("1");; m" g1 `1 a" F& D5 D$ A$ W
              if(j==n+1)
8 U2 q  Z. B$ a: E8 z6 L                  j--;2 E! I- ]: ?) |8 {6 x* J
              for(k=i;k<j;k++)
7 R* O( }$ E# [( E5 c                  printf("%c",a[k]);
9 f6 v# ?" W7 i+ [2 \/ k              printf("1");8 a# f7 J8 j* r; _- O. @6 L
              i=j;
2 `( `: x- h" W9 r: r4 T7 H          }
3 D4 N! `1 O3 ^" k# \4 s  H5 h. t      }
- p9 G7 l6 b# D; W# s      if(n==1)3 }6 j1 D$ s9 S( U6 V
          if(a[0]=='1')
: {) B, r; N8 T1 w+ [              printf("11");
1 I% h4 K8 }- f( m9 M0 n          else  C: N, W8 D8 a* q; T4 m
              printf("1%c1",a[0]);
$ X5 s" x2 m* B2 w, [   printf("\n");# e/ A1 s$ n* p- e1 q
}
( z8 t+ a- v6 I0 ]: `: Q: C评论人: Colby  发布时间: 2010-3-2 12:04:06 #include<stdio.h>4 f& i+ S) V) v8 f2 M" t) h
#include<string.h>
0 I- s0 _* h, k" y8 uvoid main()4 u$ i1 n8 {4 j1 V( p) {9 y, w  X
{  int i,j,k,n;. S. _; N: U0 Q) x0 }  s  B# j# A
   char a[50];
6 _( ~7 V1 X  s$ y! z  C   gets(a);  Y2 a1 T% d4 V$ J  x
   n=strlen(a);4 v0 J$ b" F' L7 |4 Z; E7 U# J2 ^
2 Q/ m* U# t, i
   for(i=0;i<n-1; ): ^- J+ i: d) e! P
      if(a==a[i+1])
) Z8 Q- {& _! X: L' j  `      {  for(j=i+1;a[j]==a[j+1];j++);/ N) J& S; Q0 ^8 W2 d+ b
          printf("%d%c",j-i+1,a);8 J1 O' `6 z/ r1 `
          i=j+1;4 f( v3 f3 x  w4 D/ P+ Z
      }  v# ]7 O, E* ]0 W" N; C
      else* Q1 A  `/ ?% n+ L
      {  if(a==1)
9 ^, m$ Q' Q+ }8 y3 l& N: S* E' ?. _            { printf("11");
; O, {% w, A6 r) ~$ V! \                 i++;$ }. i6 l; s5 v% F
            }7 P3 m# {5 p8 i0 y! t' ?! r
          else
1 \" I: a! A( W- S          {  for(j=i+1;a[j]!=a[j+1];j++);
: A. Y. T- {5 ?5 R              printf("1");. B& D4 T- ~6 X' b. c) a, F6 @  l
              if(j==n+1)4 i1 m" _$ ]3 W% R+ J; {4 [2 {: w
                  j--;8 B! b) ?8 V' L- s5 X- Q
              for(k=i;k<j;k++)5 G2 ]  a2 C( O
                  printf("%c",a[k]);5 w& Z3 X0 g8 p3 j- k2 Z' C
              printf("1");
9 \0 C- x2 @) B              i=j;
+ Q) W3 P1 I2 F          }" w' F2 W5 F- n8 D3 W# C. {$ z
      }
  k6 l8 b5 Y* y  w: ?' i      if(n==1)6 O  D. x( A( d8 V2 O0 f6 O$ v
          if(a[0]=='1')5 {" h4 l  d& Y4 `% R3 B% G; l
              printf("11");
  L$ b: z: Z7 }/ D+ B' J$ I8 f/ b          else
7 c0 Q) x  }* j1 @# H% c              printf("1%c1",a[0]);& J3 j9 ^9 E4 c3 t# @6 G7 l" _
   printf("\n");% B. B$ b0 \) F$ i- O3 g& F* I3 H( K
}    example2:#include<stdio.h>
* {9 T% R3 W* ?# r3 b1 K: A9 S. W! [#include<string.h>
7 q* t+ N' r7 L7 K; V) k6 D7 Mvoid main()$ h7 I) c' s7 \7 C5 o8 Z) S6 a
{  int i,j,k,n;" e0 _; }  B, X
   char a[50];
" u7 _3 j; p9 ?% m% i   gets(a);: |3 y/ ^- Q2 f  w- ?* D& o$ n2 [
   n=strlen(a);
8 |' ~; W7 b, v5 M8 m1 v
/ Q: B) i* q! T4 G   for(i=0;i<n-1; )
; n- p: i/ _2 Z/ u% r8 Y, c( u      if(a==a[i+1])
2 r3 x1 p' N5 t      {  for(j=i+1;a[j]==a[j+1];j++);
0 H( {; w8 J1 H& O6 K2 k- ]: ?8 o$ O: O          printf("%d%c",j-i+1,a);' i( X. t* x8 L/ H
          i=j+1;
) S- A0 j- Q$ @% i) m      }" X3 L5 z; ^& l  k4 ~% Q: z; m
      else
8 N: R5 }2 H5 X; a  i' ^1 v      {  if(a==1)
( H; C) t# B0 o: h            { printf("11");
: ?$ u4 U0 M" M- N1 }/ r) Z0 x  o                 i++;
3 W4 A7 E: r: Y7 v            }
1 f# n' M  z5 u" D4 _: j! A9 {+ l          else
( K+ s. p; ~7 I9 B" P! r8 h! l' W, j          {  for(j=i+1;a[j]!=a[j+1];j++);2 i6 G2 p: t( g2 l: ^' M! c
              printf("1");; z4 p6 r8 B$ a
              if(j==n+1)
! d8 \1 B# C6 u7 ~, S7 j7 l                  j--;
3 {. J6 K4 @& l/ @7 \& P              for(k=i;k<j;k++), P" `5 j- Q2 m$ ?5 P9 V+ S
                  printf("%c",a[k]);
: P# a8 W* t4 x% a4 r              printf("1");, v$ p8 E8 e8 |' \: Y5 K
              i=j;  A5 r: S' }/ A" F7 h& `% R2 D
          }
7 T2 v0 y  {5 x3 Q: b" v3 x      }8 a/ ~$ Y4 f8 a* R
      if(n==1)5 L/ M6 l  w* x$ [5 B
          if(a[0]=='1')
' R. [: A7 ]7 ~. X) w( i1 ^              printf("11");
) ~) x! G0 n2 r+ {8 Z6 \          else( b8 ?9 l$ W2 p+ `9 @: _2 W
              printf("1%c1",a[0]);: t" v# |3 ?/ L( c7 S
   printf("\n");+ o* l' y, V% G1 y/ m( P
}
0 b' G/ J5 {  n+ Z/ X, K+ E   example3:#include<stdio.h>
) X+ V; T+ ~( Y#include<string.h>& Y3 ?/ L4 @. r7 O! x
void main()
* \0 F. p% X# _; ~' {{  int i,j,k,n;( X" c' v$ G. Y0 ^: B
   char a[50];
) o) m9 v/ R9 ], c# V  E- M   gets(a);$ Z& H8 i9 p( j0 Q. I
   n=strlen(a);% x. n  n1 w2 r# [- m0 |% `

( U( y  A1 R, V+ z& w7 S   for(i=0;i<n-1; )
8 F5 Z0 O: c" o& j      if(a==a[i+1])
7 [$ B, s) `8 g* R7 `" ?      {  for(j=i+1;a[j]==a[j+1];j++);, e6 @4 @% B3 s+ x/ F. G
          printf("%d%c",j-i+1,a);7 \0 o3 z# N! x: |* Z! ]
          i=j+1;
1 i1 f. u/ U7 N& g& P      }
& U& Z, F  N2 m      else) x# x; A0 G2 I4 m( |7 D
      {  if(a==1); f: P  d* D6 c& e
            { printf("11");  s" p  J+ Y) T: f2 }5 q1 `
                 i++;  o) j; h; [  I! y- |; h
            }
& q6 K, \) b; Q) L          else; a% k6 o" M0 s3 n' V
          {  for(j=i+1;a[j]!=a[j+1];j++);
4 ^  V% D; `  J2 S9 ^              printf("1");
7 O7 y# K0 Z! z: e8 v' P              if(j==n+1)8 Y; \. |. \+ J/ N5 V; O
                  j--;8 f; B" {6 z. G. v- h6 r
              for(k=i;k<j;k++)2 x, N  a8 G0 `
                  printf("%c",a[k]);& h6 P/ R7 n1 Z3 _' J* A
              printf("1");
- e# @! v* q# R5 r  y              i=j;
1 S+ S, f1 M  Q: w6 E3 ]          }
: u7 v* k: r3 H5 [; A4 A      }
3 p4 V- \  m/ E, g. S1 {      if(n==1)& J* [; q/ m- t' w, Y  w# d4 I
          if(a[0]=='1')
$ p0 I9 V  c" U8 k              printf("11");  x$ P% e5 t# T
          else
7 m) A! ?9 N/ C  z              printf("1%c1",a[0]);
- ]9 V$ e9 }' F' ?& |   printf("\n");$ j! X! r3 e/ ~  }
}
5 M& ]7 V0 ]# Z- T3 E      来源:编程爱好者acm题库
作者: qnbs1    时间: 2010-5-15 17:58
最好附上中文翻译嘛....很不起  难看
作者: Jackge    时间: 2011-11-20 21:48
顶楼上……
作者: dahai1990    时间: 2012-2-5 15:38
虽然没看懂,,,,
作者: qazwer168    时间: 2012-2-6 10:23
关注中!感兴趣的朋友都来说说




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5