数学建模社区-数学中国

标题: 编程竞赛题目(一) [打印本页]

作者: 厚积薄发    时间: 2010-5-6 18:35
标题: 编程竞赛题目(一)
本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
* R/ O. O  y2 ~' x! C8 t
5 N. ^; I9 `; `/ p3 d" z: \Sorting It All OutDescription
& C6 K: B3 \5 W9 S. N9 j9 t
# e3 X& n; t! k. e5 w4 l; MAn ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
2 {! X1 n: W% v. `) I4 L/ |7 NInput" ]5 g$ S* `1 q
% ~. _6 r7 ?$ K) R/ C' O
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.5 L+ L7 E; D3 A$ p! Q
Output: v0 T9 q5 a, _0 S7 p

9 K, V  P1 a9 B1 SFor each problem instance, output consists of one line. This line should be one of the following three:
. o) n5 @" b  e  o% p8 ^$ i- s
, D" R* j* Z& {9 b0 M' ^  N( N) Y# SSorted sequence determined after ** relations: yyy...y. 9 N( \( t$ a! q* H% i  }
Sorted sequence cannot be determined.
/ y2 c3 m0 a( E6 n' l5 [Inconsistency found after ** relations.
, q: T' R' K$ x, E! g' g+ @, ?2 V5 W% b, B$ s: H$ G7 q
where ** is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.
; z! d7 G) D/ q9 [8 h+ Q/ u" x
7 Z* ?! D+ G9 T4 z输入样例


9 u7 \5 D5 {* ]) o4 6
8 X, E% R, o# }. ]8 \A<B
  S1 `% |$ U/ J! F1 P* XA<C
+ v% i4 F2 A6 p% ?' g( b5 j, sB<C0 J7 S+ @3 s$ K  P4 n4 i
C<D! U- H* l/ g* j$ [: |
B<D
: p& v6 c% X; f9 W" w* a+ GA<B* v7 y) ^' c4 E* _. P7 O0 w
3 2
  \7 c& i2 c2 y# WA<B( C7 ]( [3 |/ Y9 a8 W7 s
B<A
/ C, e$ S  o0 L5 k9 l9 L26 10 x6 K: a$ I9 T: a
A<Z5 v) c) c6 B) s) z& K" ~$ {
0 00 V# H4 j0 ^: P- h: H8 t4 e

6 T. s2 o, o; l) U; [4 z
输出样例


( S  r6 W; }' T* N. X4 b/ cSorted sequence determined after 4 relations: ABCD.
% A4 p5 n# T0 B! WInconsistency found after 2 relations.( p& y& Q9 m! ~" I
Sorted sequence cannot be determined.

4 q1 h4 A4 `# Y3 u3 O$ P% u3 s) B

Source5 e7 N1 E' d9 h- Z+ X
! [9 @+ f; I5 g7 x$ N' z
East Central North America 2001

: K& V, a  \; w5 P5 ~' b; O

程序解题1:


/ W( q5 n0 \- U- c//By Jackie Cheung9 Y3 v* M- n; Q, G. ~
#include <iostream>
6 f5 |% c& m5 B- ]#include <string>" m. d! r5 z9 z% J2 S2 r5 D: y' K. m
#include <cassert>
! `, u. M4 W7 B, f5 a3 f3 @typedef  struct tagNODE 0 F( K4 s: x0 L# z4 i& c1 c" W
{
6 \, B0 N+ ^" y, \4 }    char val;
' G7 ~5 U: b& ^  _2 b    struct tagNODE* link;
$ Y8 Q" {0 j/ ?}NODE;% j& X4 l, @2 U
using namespace std;2 y( Z  \  k! y& f- @) F% j" R$ h
void Marshall(bool** arrary,int n)' k0 V4 z: I2 b& s0 U
{
5 i* U8 r! Y; q" \! L- O; y% S$ H    for(int i=0;i<n;i++)
- f* ]' E! L' m" b; Q' j    {
& D% y9 \/ p( M+ W# _3 g        for (int j=0;j<n;j++)
4 j8 Y1 y/ c6 M7 ]  c+ w        {
  F$ Q: g/ ]# Y  r            if(true==arrary[j])
6 C! t" E; ~) A- O2 T                for (int k=0;k<n;k++)8 ~- v6 r% s" O8 w2 ^4 O
                {
0 U' ^+ g1 r9 p6 C' @                    arrary[j][k]=arrary[j][k] || arrary[k];
$ Y( v' _5 e& C5 A/ n$ V6 b                }9 W) O, M( @4 R7 P
        }! m& I( o, Y! i( t2 U( ~* ~
    }
: n5 T) C+ [9 Z- b: Q) y( g8 v+ T};
2 f; i; |( U3 ^, |  }7 ?" n( T- [bool  SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
5 ]" G1 M* L* H% s{
- j9 |9 y3 b& `8 i& V9 x( Y0 X    Seq[n-nLeft]=static_cast<char>('a'+nIndex);3 R, W$ i: C/ q5 S6 x( T
    bool bFlag=false;
& j2 x/ c" l$ N6 s3 |    if(1==nLeft)  d/ N7 q, |. R" o
    {
' y7 R4 E' h- A& y        Seq[n]='\0';4 f: N# o- a8 z6 H
        return true;) X! [2 l5 y; u& `6 v
    }- d3 z2 y4 c9 ^7 \0 v
    for (int i=0;i<n;i++)0 Z0 I; X( M! q" R( h5 B: `7 X
    {) N% I+ x  O1 ~- x
        if(true==array[nIndex])
( U) k2 H8 t3 G8 \        {
6 \1 m- E: d' m1 K            ; O) k# Y4 a( v1 }
            bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);/ R# _" [8 X, L& T0 x
        }* S* o: _8 ~! i# `6 r
        if(true==bFlag)' z9 f0 |' Y6 p' n8 k$ b; V) u
            return true;# \2 e+ I) Q  p, b. }
    }
$ C( \$ s, V7 t9 h4 c# G+ v$ o    return false;- E2 `! ~# O' Q0 W8 e7 L
};
4 Q& T0 {. ?  |5 j/ n) hint main()
; V8 }, L# r0 N) K/ Y& D8 y$ r4 X{
9 V9 _4 X4 t9 ~# d1 x    int nSeqLen;5 W4 @% ~! v, Q  v1 C
    int nRelNum;% B5 p, L! j) A+ N) E
    cout<<"Input the length of the sequence:"<<endl;
5 K2 e  `1 A! w$ n9 c    cin>>nSeqLen;
8 S7 ?$ O" S0 U) Y- [    cout<<"Input the number of relations between the elements:"<<endl;& H& B8 D$ h9 j. Q3 Z
    cin>>nRelNum;7 x6 V# M% o3 v1 S, b$ R* H5 `
    //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
. C; U/ o3 V0 l4 A    if(nRelNum<nSeqLen-1)* k. N& h2 w/ f% _6 r
    {
: n' _+ x7 k) O8 U1 q4 p; }) b9 {% X        cout<<"The relation can not be determined!"<<endl;
0 W$ Q3 i. ]' a# L0 A        return 0;% A7 x; `5 m4 A2 p. S3 C8 c3 z  |
    }
- w* n; I' z* b3 E( I3 c( ]    string* strRelations=new string[nRelNum];
- C( p" |# t0 m4 M  A. C    char* Seq=new char[nSeqLen+1];
2 X6 W3 v6 t6 B4 S$ i8 U    bool** array=new bool*[nSeqLen];
6 J% Y, ]! R. g7 s2 ^; M. k, A
5 I; O& r" e( N/ ~    for(int i=0;i<nRelNum;i++)* E; X+ ~3 G! v" I. b7 {
    {( E3 s7 C( a& u- d) V9 g
        cout<<"Input the "<<i+1<<"th relation:"<<endl;% r8 w( o2 n0 d  \& r/ K
        cin>>strRelations;7 b( s5 l7 |( K- t% J
    }
2 [% x/ V4 j2 z3 ?! A4 M1 Z    9 n0 m0 ]; F$ @! b* _' b+ A
    for (int i=0;i<nSeqLen;i++)
/ V5 O$ n( c% X1 u2 G+ c    {
0 R8 i+ i% x- d$ [        array=new bool[nSeqLen];
/ i8 g. g; ]6 }: M. Y        for (int j=0;j<nSeqLen;j++)' [- A, U6 Z/ S/ V
            array[j]=false;- W) c( k% I) s% h. x7 t
    }
6 P; Y# M4 S; j    //The main loop# N( S4 T: t& s: b$ _% r% T
    for (int i=0;i<nRelNum;i++)2 N- O( [4 M, ?2 ]% _
    {
: ^1 ?( X+ O$ ]+ N5 y+ i0 y        char a=strRelations[0];
! I' T( ]1 O7 ]5 Y2 Z        char b=strRelations[2];
6 o5 Y, H! I7 n7 H' ?; B        assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
1 C. g1 u7 r  r) B1 g+ I, d6 G        array[a-'a'][b-'a']=true;
+ ^3 s! v- h0 D4 E. w. _! p& P* c7 [  J! @+ h3 x
        Marshall(array,nSeqLen);
$ B4 p' Z2 p( T; u7 }  j/ w+ x! \5 {: q$ q
        //Check for Inconsistency after  every relation
: c4 T& x& ^( y* s4 o7 |! ^        for (int m=0;m<nSeqLen;m++); k9 ^/ g6 S# v2 w
        {) B  }1 c6 H( T2 |+ N- ?
            if(true==array[m][m]); _4 D  R% F6 P7 D4 r
            {5 ]5 y- J' ?# U, R# ^! j) N6 ^
                cout<<"Inconsistency found after"<<i+1<<"th  relation!"<<endl;
3 }/ K+ x. [: ~8 w  {) ~; |                    delete []strRelations;9 V' V3 m0 W  f$ O6 c% F4 J
                    for(int k=0;k<nSeqLen;k++)
2 X! S( q3 m- e* f8 g                        delete []array[k];
( D0 Q' V3 H3 }8 A$ Z                    delete []array;
1 s0 M# M, ]/ O9 N" O7 |, O                    delete []Seq;- Q1 U  j+ {( ~; {) J4 D
                    return 0;
9 ~, O0 q9 R. H" t. |; i/ n! v" B, d9 j" D0 |  D
            }
# v/ o; b; D+ s. H) m' k8 ]( w! Q        }9 a  g' f7 F1 \& t/ a# o1 z9 X) {; h

! X9 b" e, o' _        //Check for the determined sequence after  every relation    4 ^! |9 D0 B* c4 G8 n' J( t5 c2 y
        for (int j=0;j<nSeqLen;j++)
3 h# m# C, W6 Y/ A        {
+ O0 E. q  W. R$ j            if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
6 N3 {) ]" [4 w7 `            {) `/ Z0 `7 ?6 U9 |0 j" Y; l
                cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
- n7 _) i! t# T& s% o+ x                delete []strRelations;
$ M3 Q( O* L9 S9 Q, c                for(int m=0;m<nSeqLen;m++)$ s7 g8 ?1 {  G
                    delete []array[m];
% s5 N6 s& J9 T+ {$ Q                delete []array;
' a& H1 R3 P+ T9 a# t) p                delete []Seq;
5 S$ f8 J4 b  w                return 0;. }. J  E' D1 M5 w
            }- W9 F* ~' `  q. y: e4 u1 b: e7 {
        }
% K. Q8 P  d, |
/ ]  U( o3 U. k  ~+ A, K1 I# S' G9 z0 j
    }
; {+ m  p, C( f7 I- |    //If the program has come to here ,then the relationship hasn't been determined!!!# I% A. p! a' f1 |
    cout<<"The relation can not be determined!"<<endl;
6 H  G! s% `! V, `0 D    delete []strRelations;- y4 e9 u( b( _' W& G. y! S
    for(int m=0;m<nSeqLen;m++)
9 A" M2 ]9 }9 E$ ^& U        delete []array[m];1 a4 u  |  b, ]! W) d, S
    delete []array;5 o% ^* L7 S$ W: d' p/ Y, V
    delete []Seq;: [0 Q3 b$ H9 d% G7 J1 a( C
    * Y# U- [- c6 p' f+ {; }5 y
    return 0;
7 c+ k: x' ~& k% O  K2 [}
% D3 Z! ?5 H- X  o% n; v0 V( N1 Z0 W  }" a' P. n( V( a5 U
程序解题二:#include"stdio.h"
% `8 |  k8 g: Xvoid main()
+ P! S( i- u- f+ d6 ?' P: T2 ]6 \; G{+ O; |$ d6 \7 c, o
    int n_m[100][2];- v9 w9 ]- Z) _& I% I9 J* P, k' w
    char re[1000][3],
0 h; o6 r/ m1 w! D' g7 t. Q' Y    temp[26];
, v+ W" ]& @, F, ?" E' U' n; s" H    int i=0,j=0;
# r; |( X7 k7 W# q/ t    scanf("%d%d",&n_m[0],&n_m[1]);
! ]6 w$ G3 I8 y5 P3 s1 Y9 H- [, P    for( ;j<n_m[1];j++)$ x7 ^, b# w8 w& d* Q4 b+ K* h( F- L
    scanf("%s",&re[j]);
: g: @1 M2 k) V    while(n_m[0]!=0&&n_m[1]!=0)! n% Q7 Q4 P( R
    {# M2 m5 \" _  m9 t$ p, Y
    i++;
) o9 J- Z, ]  f/ q% j  Q    scanf("%d%d",&n_m[0],&n_m[1]);  v+ Z" d0 n7 @6 i. H7 u7 R
    for( int f=0;f<n_m[1];f++,j++)6 o* S: J" k0 p
    scanf("%s",&re[j]);
& k, e& V# q! Y  Z    }
/ n- H- t4 E; Y    i=0;
$ M- h# W" _( p1 L, I  V5 y. p    j=0;% w: \  ?/ N1 L9 q7 s
   for( ;n_m[0]!=0&&n_m[1]!=0;i++): F  Y7 r$ R$ G( s
    {
% V; v) w4 w$ _; h       int a=0,b=0,l=1;" p# O! X% W* H' d& s
       for(a=j;a<j+n_m[1]-1;a++)
* q4 `6 v- w1 @+ N& ^4 L         for(b=a+1;b<j+n_m[1];b++)' H8 Q  h, @. k0 W# M8 w
         {
6 d$ a0 r7 C; o" d  [              if(re[a][0]==re[0]&&re[a][2]==re[2]&&re[a][1]!=re[1]||re[a][0]==re[2]&&re[a][2]==re[0]&&re[a][1]==re[1])3 ?" A: t) q/ a
            {
- [1 X7 S2 V+ {8 W            l=0;
! f4 q9 w0 B4 Y0 j# B            printf("Inconsistency found after %d relations.\n",n_m[1]);; ?) W1 I* \5 K/ V
            break;! S9 ^! `2 y) V2 G, i7 _
            }9 y% C- d) C4 ~" \0 Y
         }% y8 ~4 T3 J  z9 ^
      if(l==0) 9 H$ T: j1 i+ `2 |4 D
          continue;//Inconsistency found after x relations.
3 n/ G" a4 X+ L% W6 x3 G1 p) D    else{
+ x4 u2 D$ b2 Q/ \7 K$ c           if(n_m[1]<n_m[0]-1)$ D% D  @; z. J. \
        {
% R0 T7 `1 k, |/ G            printf("Sorted sequence cannot be determined.\n");& ?' F4 f6 P3 @4 d5 F
            l=0;% m3 q1 m1 A7 E3 r" J% Z' ]
        }
6 Q3 p/ g" p% l6 r* Y        else( E3 ]* Z- S. t7 {, `3 y
        {
6 F; _2 d  Y/ }5 m5 ?) @2 L' @! j          if(n_m[1]==n_m[0]-1)0 d1 N4 \& k: N/ M& g
          {   
" `) j4 k) l& N$ A2 s! z/ G              int k=0,p=0;
, c% t( n. o: m5 t+ m* y5 D4 F              for( ;k<n_m[1]-1;k++)
& o( c, q$ Z5 i5 o, H1 w' Z, ^: `                  for(p=k+1;p<n_m[1];p++)& V. q& b: I- v* y( d: P) @( h
                      if(re[0]==re[0]||re[2]==re[2])( m, v  q6 d  y" q+ m3 d
                      {
( I+ |  Y, h/ H% V* c8 r* t: d                      printf("Sorted sequence cannot be determined.\n");( @3 h/ L0 \; U- F5 k& ^9 l( j8 a
                      break;; T1 n' f. L5 u: T4 E
                      l=0;* K; F/ }9 s$ H1 l$ L8 N
                      }$ B1 K6 [" ^" |- Z
          }$ c& Y1 {5 G6 T4 n* |
        }
8 h. ~3 R3 x* V" u        if(l==0)
4 p9 E- M, I) h) K/ `        continue;//Sorted sequence cannot be determined.3 |4 D, h) f6 I0 N4 h

1 X  e$ p" Z! ?, f- N+ F        else. F; u  y$ I1 a' y" y
         {
7 E' Z5 C' ?4 f/ K8 y           
) I5 V* N; s. o) ?6 O            for(int k=0;k<n_m[0];k++)
, e$ s4 F  D2 E             temp[k]=k+65;
. _$ L7 A  H: h            for(k=0;k<n_m[1];k++,j++)7 Z" l3 K, z- _2 c6 B/ U- M) c) Q
            {, l) r. b) N7 i! G+ ^& N( C* |+ M
                int t1=0,t2=0;: {! u& M3 S9 K" N2 P$ W
              for(int s=0;s<n_m[0];s++)
$ s3 f( H8 {8 q, n              {8 M+ }2 I3 t: o- L, _
               if(temp==re[0]); V/ n, `# m. d2 o+ S7 l# i
                   t1=s;7 ^/ I6 Q4 m6 Q5 M
                      if(temp==re[2])
" j7 G9 R5 X4 {                   t2=s;( o  d0 r- n3 w$ \& a0 m
              }
: d6 [4 b1 v2 d! S              if((t1>t2)&&re[1]=='<')5 |/ f3 r) L) ^9 R3 z0 q* @
              {# c  n5 N! Y- O, u, m" \  c
                char t3=temp[t1];
0 s) j& M% U2 `0 e, }. r, q                temp[t1]=temp[t2];4 ~- K% ~8 ^) a4 J2 O
                temp[t2]=t3;# U: I* ?3 Q; F9 o; e
              }0 c# T, o: @) K( o! e: c
            }
; C! X. r' W$ J) \6 B3 P' j        int count=0;$ L$ X: L5 u! G6 ?0 z* K0 r
        for(int s=0;s<n_m[0]-1;s++)) E+ h# T2 {' d$ \  _1 z
        for(int d=j-n_m[1];d<j;d++)
, E/ E9 r  R; ~1 X( I/ W7 C) f3 s* l            if(re[d][0]==temp&&re[d][2]==temp[s+1])/ y6 f" P0 N" @
            {. v: O+ N- h7 d; U6 D6 |/ Q
                count++;6 J2 S6 n! o. J% b% p' }1 M4 I' s
                break;7 W* e0 H2 v4 A
            }
' M- A9 k9 L* X) e2 B4 U- d            if(count==n_m[0]-1)+ k$ X/ }" d" Z: ]+ }6 k
            {
& A0 Y, o* K) R4 D* K                printf("Sorted sequence determined after %d relations:",n_m[1]);
4 k) i0 A0 Z7 i8 N                for(int f=0;f<n_m[0];f++)
' r& E- C3 I0 b8 J0 l                  printf("%c",temp[f]);) h+ \/ [7 c4 g' N6 ]' h& y) y! W
                 printf("\n");
5 o4 C. G; d. }1 T            }5 l8 {' d3 U( Y$ n
            else
' J0 O' ]2 f. S: J! }# k, ~# o/ z* K               printf("Sorted sequence cannot be determined.\n"); # ?  \! ?/ d' r" Y5 Z1 O' C
        }9 }' u- F, [0 W& h+ W' L
    }2 L  S8 d; K% h2 c) E( `
    }
/ t7 Z: t2 m6 r7 X}
- {3 t/ Z) Y: L! ^- G/ t1 O  n/ C
" Q% [8 @7 m& a8 M8 j  H& n- e+ J5 e% D
+ X7 M" H5 j% n4 g
: l" ^" x2 g, f2 f# `

* M2 d% z" \7 F+ W( L
  u: ~/ p/ w* H% H( l0 U4 o
; u6 z6 o6 U5 s0 X* v" I- |% m% w2 I. F. w# s9 I1 i- V* s  h

来源:编程爱好者acm题库






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