数学建模社区-数学中国
标题: 编程竞赛题目(一) [打印本页]
作者: 厚积薄发 时间: 2010-5-6 18:35
标题: 编程竞赛题目(一)
本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
8 Q& w) x `/ M8 K, J% W
+ Y5 S$ B* m6 TSorting It All OutDescription
% B" R8 H% z! |3 i4 q' B
: Q [$ V( v# R7 B% 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.
' @* N; ~7 Y% Z4 ]3 MInput* \! e. q9 ?3 V( g. E
' Q! ]- m! I; }& W; {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.2 U K9 e. |. b* D3 b5 j& H; @
Output
: A! Y- [, {. K* d: M3 a Y: r& x$ Z/ R
For each problem instance, output consists of one line. This line should be one of the following three: 6 R7 [$ H3 q8 Y3 W$ X& Q7 o m
: e3 c5 k4 G5 v; [# LSorted sequence determined after ** relations: yyy...y.
0 j/ m. w, C; ]9 y2 ^Sorted sequence cannot be determined. , b1 c0 F! y: u( u9 o, }; h
Inconsistency found after ** relations. ) u, [2 S; x; u4 {2 y5 A: ~: T9 X
- c( p: a ?# Y3 T
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. # w6 ~5 [" [# q# r9 f0 M
8 d+ \' a4 M7 H; r2 J7 T4 E* j9 y# |0 l输入样例
. ~3 S7 ]/ D6 ?' C# h5 u4 6
+ z( Z. g4 f/ SA<B
, {' O; u9 l8 f* BA<C
. f6 @! p; s9 ^) u, KB<C
0 z/ |4 K" H$ t! i$ B" U$ vC<D
+ u! M5 @ d/ Q& f. FB<D" d: Y1 B9 c& E+ h9 w' Z
A<B! D3 y Z% U- Q0 m8 f, t7 |
3 26 ~/ h7 K5 }7 @
A<B
5 {, y& o" O0 X6 Y) v5 q3 BB<A
1 L" V- j0 a! o0 k1 i1 ?2 I26 1
( n9 H1 o. P' Y+ G: RA<Z
* w. E7 M* k, F3 j0 0
/ r5 t3 H: p& z) U, ?; }* e
/ D4 d0 d* Z5 t6 D+ @& d
输出样例 * ^1 R; }. c+ F, |) B
Sorted sequence determined after 4 relations: ABCD.
: C- d" S& ^! IInconsistency found after 2 relations.
" }1 |, g) ~5 u2 ?' {1 kSorted sequence cannot be determined.
: x P# k2 e- l9 b. j
Source8 `: [, I* v$ [6 Z! j# C8 S
) F$ ^; L( |/ c* }. k) QEast Central North America 2001
* z$ W7 t6 E$ h6 E+ b
程序解题1:
0 T( X0 k& g, q/ L& B- `4 ^& b8 W0 \//By Jackie Cheung- d8 M3 g1 x1 T3 l- Q+ _- I; D( I
#include <iostream>
- i0 L0 p* M. l/ l: R#include <string>5 \/ H$ N' _& h4 S
#include <cassert>
- Z0 W* R: U5 q6 Z1 Q& ktypedef struct tagNODE
6 l8 M& W7 w+ x! K+ M{* N6 ]: _2 S7 ?# z9 {! w, S
char val;
% c! t4 [+ L; I% T: j% N4 ^ struct tagNODE* link;
- u$ A; [! z: H8 l( c) C}NODE;5 S, r2 m5 D3 M M: p2 `
using namespace std;% ^ z6 H* d1 ]$ p, C
void Marshall(bool** arrary,int n)
1 k0 `0 ~8 T; k& V/ z9 ~{
G; D# v( G$ P* H for(int i=0;i<n;i++)& i, {: Q& K3 C2 i& x' g
{5 g4 Y' e* L c4 h" a2 J0 d
for (int j=0;j<n;j++)
9 u% G }. {& l5 h; Z( s6 h {/ H. n6 T6 L8 w
if(true==arrary[j])
" ~* h/ E3 z$ I+ B+ t M, \ for (int k=0;k<n;k++)
s0 R# s( C8 I {
+ D! `% X8 F' ^- Z# n arrary[j][k]=arrary[j][k] || arrary[k];* y# |) i4 s' j; z4 t3 K! Y& p/ O& X( Q
}
( o; y f* |9 q" ? }* w/ ~; @9 m- Z& e9 T
}" w g) F! s7 A8 `% ^
};
) `$ k: B. I9 X# a; l( }, }1 obool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)1 R9 M. G. x) Q- N/ r, _7 J" X. U
{& s+ q& G" ]/ C& [( X+ L: r3 w
Seq[n-nLeft]=static_cast<char>('a'+nIndex);
3 h) ?2 ^* \) Z4 D6 ^# {3 ~ bool bFlag=false; B' b6 R! l6 z
if(1==nLeft)
; I4 r8 i7 t, M* P6 M5 s& S8 y$ N {
) w! B- r+ S" f F1 J+ U( l+ [ Seq[n]='\0';" s/ `9 r9 U% k+ @/ v3 j$ G( U
return true;8 f. b' }* Y# W* a: o% b
}1 j1 O4 N! n# [1 k' ~- L+ ]" C
for (int i=0;i<n;i++)
+ M) R9 ~6 \5 L8 ^3 K& \ {
" e+ I4 M' B, H8 a% U- N# d2 _9 h% d if(true==array[nIndex])# M6 {; {( A j( D
{' p7 S9 L2 Q' n# G
# `8 C7 |- x# @) p3 g; F
bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
9 B0 ?- i1 ~) z& `$ P2 { }8 C6 u) Y- A1 t) w+ B# K# F) d
if(true==bFlag): f" ?0 \- T2 X% l9 ^4 b( B
return true;5 I3 B/ c4 B l; z1 J' X
}
- P# m5 K0 N: ]7 I$ q: t return false;- N& a. H+ ?1 f6 S8 g, b# ?- d
};
3 v2 j7 y) m5 sint main() N6 i4 L7 Q2 W, c: I
{( X5 A; a+ s( p
int nSeqLen;3 X+ w. P, c6 H6 F
int nRelNum;
: Q. q' Z! ~2 h, a5 v0 [2 R cout<<"Input the length of the sequence:"<<endl;! @# [( O% o4 [. t9 [
cin>>nSeqLen;
! O$ ]" n6 r1 O2 I5 G. K, N cout<<"Input the number of relations between the elements:"<<endl;7 X) H+ i0 B2 C$ y
cin>>nRelNum;
, F+ g( W6 f, T5 V! Z; j //1:if nRelNum<nSeqLen-1,then the relation can not be determined!9 u1 ~ h! \" U
if(nRelNum<nSeqLen-1)3 O3 ]8 G4 [+ [! q/ q
{
. E2 X7 E2 {' y cout<<"The relation can not be determined!"<<endl;0 p! f7 F* g! Q9 |: @" w7 \- e; l, w1 r
return 0;% k) R" C# u" ^1 L3 \1 l
}9 `* N& N: t" z: |3 z. l
string* strRelations=new string[nRelNum];
: n; V Z& X) T7 } char* Seq=new char[nSeqLen+1];
8 h! O" B1 s+ ~ bool** array=new bool*[nSeqLen];
' ?4 n7 E" U- |5 f
& D7 q) r. y. Z/ \. o3 c for(int i=0;i<nRelNum;i++)
: V; t ^& y- u1 P/ P: E& U {
# z3 @2 \' j7 l {) b; p cout<<"Input the "<<i+1<<"th relation:"<<endl;9 m E3 I- |5 T' P
cin>>strRelations;
5 U. b g/ }! c0 i1 p9 A }) x; Z' p4 T/ I$ [) y
% T+ o/ J" J, k
for (int i=0;i<nSeqLen;i++)
! n$ v/ F3 z I/ t8 d3 N3 q {
: V# J/ R- C( I, ^ array=new bool[nSeqLen];
: F6 c9 [; Z2 v for (int j=0;j<nSeqLen;j++)# m9 p! r& ^% y" ?0 _; g* Q7 x
array[j]=false;
y6 N1 n6 }5 O+ C; t }
1 L& B8 X& S) T3 n7 e //The main loop, X+ l* t l' G" G* ?4 }& x
for (int i=0;i<nRelNum;i++); G# y/ U6 g5 f) @/ \2 f
{
) w+ X# a3 I. l/ `: _ char a=strRelations[0];0 v, `' t" o3 t" q( Q" p
char b=strRelations[2];
$ d; J7 @) @" `7 A4 j' { assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
4 `, ~" w- U/ d8 j1 B array[a-'a'][b-'a']=true;5 m/ R# g( Q' P. e" ]( q; r3 v0 b
( t( }" q u0 P' M/ n% @ Marshall(array,nSeqLen);3 v% o( l0 C3 U7 X
% |) A. Z$ U: j //Check for Inconsistency after every relation
& U" `3 U5 ]! z, L( w( N for (int m=0;m<nSeqLen;m++)
2 \6 R$ `- o* @6 r% o) q8 r% X3 Q+ l+ W {) ~7 j' ~* l5 z A9 d* h6 V E9 l
if(true==array[m][m])
+ f% D6 m) w. E3 f2 s) O6 ~ {# ^1 A2 v( @6 J6 I
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
# C" a; e1 H7 x2 o9 @ delete []strRelations;
- c$ ~6 A$ [& g+ L9 @ for(int k=0;k<nSeqLen;k++), i1 o9 B4 }/ x$ I
delete []array[k];
7 ]4 U( [' v K delete []array;2 |+ L2 O: y" O) y6 C
delete []Seq;1 M4 Y! c% s0 W. D- c
return 0;
% v" j# Q& e% g2 q
/ U6 {& B' g6 Q5 e: z: H8 g' j, E0 s }
+ o& d! u; g( U/ m" `% W% W }: S( L- V* T& J7 ^ h4 `/ }7 i
1 |$ F! w: X& ?# h5 m" a4 a5 B //Check for the determined sequence after every relation 7 E6 i) u6 B+ j) c2 E6 o v
for (int j=0;j<nSeqLen;j++)
- Y) e3 R9 T! M* B {
$ m2 u& _7 E2 n% S9 h1 D if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))# k; j; }: G, x( g* {1 v- i8 C# ~
{ `" Z' d/ p! t1 r: e9 l0 M
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;; x6 r, t! Y o% E7 ]( C, ?
delete []strRelations;
( w: C* N0 j& h) X) z3 I. j- ] for(int m=0;m<nSeqLen;m++)
" y. h! {3 A, X- H, D* \ delete []array[m];9 D+ {, j; Y' _" U6 l- L( I+ F1 B
delete []array;
1 V) G) Y2 F- `' q delete []Seq;
% x- D. [, x9 c0 p$ a return 0;: f% P. L, L2 b# F& q
}
# M, `* Q; `- @8 |0 A) I& V }
) p# }; W; V2 P6 I7 }: T u) w. f2 s; P6 T$ C
6 D1 \7 a* B7 ?/ @
}, ]3 G* Y3 U7 a9 p" S5 i y$ n
//If the program has come to here ,then the relationship hasn't been determined!!!5 }' ?9 l5 ]/ P
cout<<"The relation can not be determined!"<<endl;
5 s+ Q$ r4 b( u delete []strRelations;( D: V( ]6 g6 o4 q( l8 |
for(int m=0;m<nSeqLen;m++)
5 N( L6 M z3 B- p w delete []array[m];3 X8 y* B0 N2 o8 x5 I
delete []array;
7 H+ H$ b( i' q" y" ]2 Z) R i( ^" ^ delete []Seq;4 p) u1 \+ d5 F4 }
3 _/ a; _5 Z; A" k
return 0;
7 Z! j# O2 k R2 D# G s$ \9 s& O}5 x/ S& ]$ \+ s* U( ~( s
5 d3 ]8 A, ]; O0 Z) ~程序解题二:#include"stdio.h", z7 k7 ]5 i) U J. M C' O. z7 }
void main()' ?; S) g- F+ d/ D" c6 B$ F
{5 W- x1 N! E0 M( {
int n_m[100][2];& h9 }1 e% P& ~, }- X
char re[1000][3],
: [( j' c' [8 M, N* i4 A temp[26];9 e0 U- F9 e' x) Y* b1 s2 S
int i=0,j=0;
! D1 l: T" F6 t) w( ~/ E scanf("%d%d",&n_m[0],&n_m[1]); s% u2 f* e" k7 S$ M. n
for( ;j<n_m[1];j++)
' j2 |. m+ i4 W5 y( R scanf("%s",&re[j]);8 I$ @0 _& C/ o% T& k& @
while(n_m[0]!=0&&n_m[1]!=0)# r* |' q+ y9 c1 z( }- J) t6 ]
{ x8 [5 w q6 n
i++;9 P0 v! y/ L. K
scanf("%d%d",&n_m[0],&n_m[1]);, `! q% T9 f+ \5 q
for( int f=0;f<n_m[1];f++,j++)( k/ z( ?3 G7 ^
scanf("%s",&re[j]);. `) U# Q' i0 a
}
5 n# {4 M7 D# r/ T% J i=0;4 Z* h" {8 |- [9 M; `' H- \
j=0;7 S9 \2 t3 P" P; ?5 N
for( ;n_m[0]!=0&&n_m[1]!=0;i++); p# Q% T L3 Z2 w8 @
{
0 y6 w1 Z$ z% E4 B int a=0,b=0,l=1;
* i2 d% w) ^& t/ l4 ^% c- N) e for(a=j;a<j+n_m[1]-1;a++)
3 ]' T; [" x$ ^: J for(b=a+1;b<j+n_m[1];b++)
( u* A+ N8 }) s1 f {2 A; p5 h0 o6 D% ^2 G+ u/ E
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]), N4 _- q5 U. E4 f0 E* g
{
4 R$ H0 e- D" W. Y9 Y) a$ u l=0;. p7 g# {; O0 A9 S6 [! a& j
printf("Inconsistency found after %d relations.\n",n_m[1]);0 M Y0 z H/ R5 E; t
break;
3 L4 M7 V6 [0 d2 Q }7 z9 P3 n. D$ N- V2 n3 u
}
: V& V( E1 z1 L& X7 q; C4 d V if(l==0)
: \/ E& Y _" @9 y continue;//Inconsistency found after x relations.8 Z/ v# W- B+ }) b+ `3 D# ~5 U" F+ s
else{0 Y7 l/ O- Q: G% y+ N6 z
if(n_m[1]<n_m[0]-1)
+ [" u0 y0 o. d4 z) r0 Q$ p {
q7 {. ]: k0 R/ a+ B. F6 E( i printf("Sorted sequence cannot be determined.\n");# }; T5 N) v4 h& U( O7 F! q
l=0;
0 j% y) U$ j3 e( Y8 G* H7 C, s- W }
2 Z+ ~2 r1 @& ~1 c! }, g% l1 Z else
1 z! l8 W1 S$ o {0 A; H+ | `- c8 P
if(n_m[1]==n_m[0]-1)
! b1 `. s; b( a" j7 z$ ^( Y { ! c5 \) M1 W# P8 ]7 ~) d0 O1 _+ |
int k=0,p=0;, Z0 a: \+ r5 b; k2 T; V3 i1 Y
for( ;k<n_m[1]-1;k++)
" O" ?5 D; r s5 F for(p=k+1;p<n_m[1];p++)7 J% ^& n. V" \ T
if(re[0]==re[0]||re[2]==re[2])- u w, { Z5 N. U" Q2 B
{
+ ?* w$ ?, ?* ^: [$ [ printf("Sorted sequence cannot be determined.\n");
1 t1 V: ]$ S% r5 c$ X0 E break;9 j: `- Y' ?8 m2 y
l=0;# B0 b' G+ m. h: z Y3 m& M
}6 @1 U0 X1 B5 |. i* [2 q
}, r2 K @. t3 v. k; ?* a
}
- ~1 y: x. W9 ^* ?2 x+ _4 g0 l* k& e if(l==0) 7 D6 d- ?. D v/ s' r" |
continue;//Sorted sequence cannot be determined.
5 t. w- m& C, S, H$ w
: t* J3 p2 e/ p( o% t; _" y# z( f! V else
; R6 ^! x P' H7 x {
5 G5 p- j7 G5 G- o5 J( s
$ L9 K9 P- F0 y8 `2 }! | for(int k=0;k<n_m[0];k++)
% s' |! s: {2 `- } temp[k]=k+65;. |1 k+ A5 P: w1 M
for(k=0;k<n_m[1];k++,j++)' a9 C! E. k6 d+ ?. t
{
- e. I/ y" S2 _" }1 I int t1=0,t2=0;: F* L8 }/ `+ ^& a
for(int s=0;s<n_m[0];s++)4 x/ h- @: A; g' V) x
{
7 q3 S& q. @( I: }" Q7 b" t if(temp==re[0])
/ _! z: \) I' z/ Y t1=s;4 L |- `5 k) g! Q5 {! k
if(temp==re[2])
2 W( C; c5 r% ^( z# H! \ t2=s;. X* O7 J. O( b9 l- v
}
0 a* O( Z& M8 ~+ k8 c5 K if((t1>t2)&&re[1]=='<')
* Z# |( g- X4 B( N' ~# j4 ` {
$ H7 J: E% e* v! k5 w4 _7 @$ A char t3=temp[t1];5 V( ?& {$ O) {/ T/ o
temp[t1]=temp[t2];" g) p$ T+ e8 C! q
temp[t2]=t3;" T( _- W x9 C7 ]
}
% M2 p0 h! |3 y }
* U0 u% q; Q, J1 x9 G int count=0;
) b& Y- K9 l- ` for(int s=0;s<n_m[0]-1;s++)
G. ~% U- }- K1 \+ ` for(int d=j-n_m[1];d<j;d++)
" t, i2 m# \+ M# H, u) | if(re[d][0]==temp&&re[d][2]==temp[s+1])3 M4 @0 z8 M- i; {1 L
{* C' q5 T3 l' S( j- v6 ^- G: t
count++;+ N0 d8 k9 }0 C! T* p$ G6 h
break;
# y2 ?5 E2 a/ u5 g* { }+ H1 d# k4 r. _6 Z% {! t& a: A4 x
if(count==n_m[0]-1)
4 h+ _- j$ E* F; y4 B5 Q {: L" `/ z( k: |4 o& }$ K5 q
printf("Sorted sequence determined after %d relations:",n_m[1]);
" z+ p5 r2 \3 t/ a for(int f=0;f<n_m[0];f++)% b+ w" d; n8 w, i2 E& ~
printf("%c",temp[f]);
: A; b: c# S7 O5 W* B printf("\n");6 M) p4 n3 K4 A8 K# a% f: N
}" U9 T6 `% r$ y% G3 ^% H
else
/ B' L5 S% v* B" P- F9 [( B. l printf("Sorted sequence cannot be determined.\n");
5 O0 C3 n2 n, s }4 m" x! J9 b, @& a, \
}( u" I8 g+ M2 x ^/ _
} t4 C+ X' B5 O3 I+ m9 O
}
8 x) K2 D% S8 s/ Y) O2 c
: L1 i% P& Z- c" x8 f% B' ]7 Z3 ?. ? h/ q2 [+ G0 c$ q! X8 o
+ S H* j, D- e' R! _
. u3 h/ Y8 b8 h* B+ s, J9 M7 ?
# U9 A+ Q h7 Y) L3 \# e- I" k4 \$ |% m8 W# [' f) ?
# H+ q! r, N9 R. q& V; I
0 t" _0 I( ], g) u2 k& B来源:编程爱好者acm题库
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) |
Powered by Discuz! X2.5 |