数学建模社区-数学中国
标题: 编程竞赛题目(一) [打印本页]
作者: 厚积薄发 时间: 2010-5-6 18:35
标题: 编程竞赛题目(一)
本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
4 t. ]4 u: F! |# |; M& U& }9 x8 y# B/ y- Q' q) B2 c4 h& `6 q% B
Sorting It All OutDescription+ v9 M6 p! ?4 ^$ p
( x! F9 \ ]( Q( U$ H' v$ Z' M2 PAn 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.
1 d6 `0 m. S, S* I( B) LInput
; q: a2 i2 E2 b4 n5 V2 v4 N
) y& x; i q! `9 ^/ x5 YInput 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.- v8 a% t4 I) I: t1 @; ^( ]
Output- h7 r6 M' w& ^) a5 V) Q
& y* f2 ^$ y9 {( k$ kFor each problem instance, output consists of one line. This line should be one of the following three: + o* h% O8 \: {# N& p
' n) F1 p# _3 w( E7 g' VSorted sequence determined after ** relations: yyy...y. / c7 n; Y8 M0 V1 d7 r
Sorted sequence cannot be determined. 6 H2 [# @) k# _, L% f K
Inconsistency found after ** relations.
+ h0 [. ^ P& M: o3 R7 h7 ]$ N0 {+ z* T7 y
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. % q3 |8 g/ i9 ~' e9 R7 q/ C& v
2 y% v" |& c# }$ Q/ N
输入样例
* x2 ]2 J F$ B! ]7 G, F% y: y4 6
! d2 i& Q4 O4 c' Z' bA<B
1 k6 o: a, |, \( t8 @A<C: A9 w F# ~( W5 s5 m. ]" [
B<C
) Q: L3 i8 b* sC<D+ y2 D4 s( U# N% |8 u
B<D
% X R0 q+ ?0 {$ FA<B
2 t9 K- g/ ^* |3 2
/ h+ a. e0 P& T. |" B/ D6 gA<B
+ K" w- y0 ?6 GB<A7 b+ Y- J; y1 ]& u$ t% R5 f
26 1
; f. a) S8 q7 ^4 uA<Z8 B& I5 w" M* H f
0 0% e' u# @: z# `
* y) p4 L4 l7 j3 Q/ E
输出样例 & c7 i q! _5 }; I# y: d. [) e
Sorted sequence determined after 4 relations: ABCD. z- R+ W+ ^, ?# F
Inconsistency found after 2 relations.
4 d# r7 [- A% C$ l6 a1 FSorted sequence cannot be determined.
) U/ I& e' y% F8 f
Source
9 d' X9 i/ j/ |% T4 @6 G, C8 a P) ^! w2 f
East Central North America 2001
n- W! U* K+ Z; u4 J* c" N7 [) Q" o% U程序解题1:
: z6 u/ I% L6 }2 ^
//By Jackie Cheung3 p$ {2 s# f! |/ `8 V( P
#include <iostream>8 D$ t, c2 H+ T# o
#include <string>( M% a5 i1 k/ E4 Q5 U2 f
#include <cassert># o2 J# i2 T4 V1 O9 v3 G! k5 A: K7 M* \% S
typedef struct tagNODE
; n! j) U5 l" s0 ]{2 J0 W: u a! R2 x; c' I; j. U
char val;
) Q: a4 V1 Y5 t6 |9 T- j struct tagNODE* link;
" x. ^. g {& Y* b: S$ j- s}NODE;
; o y6 t, T; c2 |using namespace std;
4 j& T- r4 G2 v- d9 F" yvoid Marshall(bool** arrary,int n)( w* c& c7 V% j" n, |8 c# p
{
" M8 z9 @# D" S' f' k' |4 @" C1 g for(int i=0;i<n;i++); m4 ?: s% f5 [
{
" T& B! N6 A6 B: X7 U& x for (int j=0;j<n;j++)! v# a+ ^! {7 {$ q
{
5 I; f5 \ _, A, F if(true==arrary[j])4 k1 N; ~ s$ H$ o1 `; R
for (int k=0;k<n;k++)
2 |4 d: y; H( Q A {
" C+ q, a/ O# ^4 W- ^$ t( S arrary[j][k]=arrary[j][k] || arrary[k];
) p' U, g2 j" }) E& I }( w% W" Y9 t | N m# x4 X3 M
}
% V( H, r/ ?- }# a }; G8 f! k! R# ~2 S
};
) m* g: z) o; p/ @bool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)( B) E0 T6 p+ B7 U, |
{6 \+ t. ^; @: i/ g5 D; E% t9 G+ x
Seq[n-nLeft]=static_cast<char>('a'+nIndex);! _- n1 Z1 [& U: E4 o# `
bool bFlag=false;
/ J6 d, `3 c: V9 h/ ^, e if(1==nLeft)
5 r. U* q3 d8 {$ _0 I9 V1 N {+ j7 @! n& K" D$ E4 |
Seq[n]='\0';
) c/ i5 I0 c- O \. D' q return true;
8 C" d2 P8 a1 K' h8 @- U }
+ p( b5 O4 z% c3 j* C0 ~8 F5 s" B* s for (int i=0;i<n;i++)
/ n! R3 ?7 V Q {* s, P5 O' F- U+ Z- m2 w+ S& Z
if(true==array[nIndex])
$ R# y0 ^7 C) Y" h& u& t {( ~$ {8 w3 M: u
: f5 v. N& s7 x/ a; K: ?! X2 J1 P# @ bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
7 I* a4 f" W& n# y3 i }8 u+ c$ d0 Z+ ?8 K7 g7 c
if(true==bFlag)) m2 z+ z6 ?) ]2 ]3 h
return true;/ Y7 t$ a: B7 C& Z( A) _
}
) d% \# k8 k: u return false;) E2 x8 @4 }% E
};2 b2 d3 \/ ]4 q M3 e
int main()) Q! A7 F* t1 H# M4 e* i: S
{
% L/ ~6 \% B' J" |0 J int nSeqLen;
6 m0 }8 m1 }3 l, W2 B+ R: v int nRelNum;& o$ Y$ t; e2 H) {# {" |3 y7 Z5 L
cout<<"Input the length of the sequence:"<<endl;, J( Q5 u$ y( b/ c. W7 f5 \
cin>>nSeqLen;' q8 o/ ]5 [+ T$ F
cout<<"Input the number of relations between the elements:"<<endl;9 A% u& ^3 r& |
cin>>nRelNum;
: z) Q: u! I9 ^ K. X$ G, I* A //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
7 B z3 G9 H7 f if(nRelNum<nSeqLen-1)8 P: [' d' V) M9 c: X
{
7 T1 u+ G+ C+ m' m- b8 I cout<<"The relation can not be determined!"<<endl;
1 w8 U/ N# _+ ^; e$ k return 0;
7 x/ F9 D @: Z- v }' x" j+ N7 u) q
string* strRelations=new string[nRelNum];* A) F+ g9 q& s) J) w; u1 E( B: z) U
char* Seq=new char[nSeqLen+1];
7 `$ X* ?0 J' [9 B, H bool** array=new bool*[nSeqLen];. D% g$ C A1 _) t: O
9 @2 C m0 ?# m. } j. K+ h8 D
for(int i=0;i<nRelNum;i++)
4 H8 E/ ^! f( J4 y! ^# |+ Y {
7 d4 u" H$ J, l& Y$ x5 ` cout<<"Input the "<<i+1<<"th relation:"<<endl;
' \3 a" p5 h& m1 A/ X' ~" u cin>>strRelations;6 @3 d- z, L+ X
}. j( j, U8 f/ Y/ Q) K, A- {" ]
5 O2 M7 |" |+ ^( [7 [6 U: c& X8 V for (int i=0;i<nSeqLen;i++)8 [" k* G5 _9 S2 h) Q' z
{& e4 w$ ^( w2 @/ L, @
array=new bool[nSeqLen];
# Z0 ~, r5 U$ s1 k for (int j=0;j<nSeqLen;j++)
) F8 e2 y* e( y* ^9 T9 I3 _& p: s array[j]=false;
- I9 x: B. E8 j4 K: `- b0 k( O# r }
/ o. T+ q# c( c/ J //The main loop! N1 ]& L8 S. C6 i: M: t O
for (int i=0;i<nRelNum;i++)
1 `; }3 x# J% E/ ^3 y {
7 U* M P/ b8 J( _$ } char a=strRelations[0];
0 U& e |1 O0 [3 \; c6 D+ E char b=strRelations[2];
2 }( `4 H+ I/ T$ Z x/ { assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);9 H- r1 U% s8 c- |7 r
array[a-'a'][b-'a']=true;
( W/ u, } _4 Z( B5 x5 O# G! @% b4 {5 M; P8 I9 G: b) ?8 d4 `% O5 }
Marshall(array,nSeqLen);9 h$ v Y' z9 r1 V$ D# P; }$ X
$ R, f2 P1 W& s. } //Check for Inconsistency after every relation/ @& K' u: M9 P* e1 v
for (int m=0;m<nSeqLen;m++)
7 B6 |) i' x. | F' L& [' F# o {. K+ h' t! H4 h4 \! o. I
if(true==array[m][m])7 R1 ^& B5 { Z
{- T8 Q2 \& o& N5 N R
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
5 A2 M6 H. E' |& D m, O5 J p delete []strRelations;7 F- M! v+ \* Y7 n1 ^% m. a9 f
for(int k=0;k<nSeqLen;k++) G- Q5 i4 _6 ?4 e) ?3 V
delete []array[k];/ b: y; w {2 s* ]: l$ N) E) m
delete []array;4 j- ^% _, r. U, b# F& ?
delete []Seq;/ H" S$ ]- q" D; ?% q9 ~8 K
return 0;
# Z8 I" b. S! I; A2 H) c+ Z/ n* T, L `3 [
}
/ f% o, n+ X5 E4 S* J }( U& D9 x- h% ]" F
) D* h1 G# Z# P7 B3 {- G; u
//Check for the determined sequence after every relation
9 Q# ? ]) q7 }0 j- i# q/ W' H0 ]( I for (int j=0;j<nSeqLen;j++)
7 d; ]3 H' D# F: x: l. ?& p0 h' b" n {0 }) f/ F2 R+ F# a! B* [
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
: [) {9 l _4 E6 s' O. _0 }+ y {
( d; \5 o" K/ K5 m; Z9 W cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
, i6 m9 K @5 h( J% A delete []strRelations;
+ Z# A8 Q1 y# y- K5 \ for(int m=0;m<nSeqLen;m++): }3 G2 e+ Z* J6 B6 n
delete []array[m];# d5 T5 a' A, x/ n4 l
delete []array;
- a* v" V& K: O/ a& [0 D7 `7 @ delete []Seq;
8 B" C; i6 p7 x1 V return 0;" F# G' c d9 F' U0 A2 L6 _! |
}
, ^% `7 F, H2 S }2 N9 D% x1 l; Q9 d9 D1 H- E
) s8 [* t) N! i
4 W9 W$ d! q6 |$ o
}
- L: I# F- @2 z0 S( s; q3 w //If the program has come to here ,then the relationship hasn't been determined!!!
9 p. H* o: M2 o5 x4 d8 k cout<<"The relation can not be determined!"<<endl;( m1 e( \) Z, i* p
delete []strRelations;, m. l0 x s0 A) G; B5 u" L
for(int m=0;m<nSeqLen;m++)
! Y7 W H" M2 F; }% _9 F delete []array[m];5 R8 }/ G8 g+ f: k5 {+ b, p
delete []array;; ]) V& C3 f+ X7 S3 ~( z) e' R2 q
delete []Seq;, F& {0 I6 t ^8 N- ^
! n) }) e+ I& ]" f
return 0;
& ^- H! }- O! S) g9 e. T" ?}/ P1 e& I" K \) t5 G J t
5 ~- G: P- p( F* F. y0 [
程序解题二:#include"stdio.h"2 k; y2 H1 h' x) d7 p6 _- C8 f
void main()
3 x- Y- E+ o: V( k; |+ J$ d{/ w( B/ E* M8 {8 q" A9 d. A4 X8 d1 V, f
int n_m[100][2];1 j \' J) C: y0 j. S% l6 f
char re[1000][3],
! W- i* y9 W" _! k" k temp[26];
7 q+ f! J2 G& U; ? int i=0,j=0;- Z: r; J- j1 O0 T# Y- a1 R. l( `
scanf("%d%d",&n_m[0],&n_m[1]);$ Q+ B; L8 S3 z' r, p
for( ;j<n_m[1];j++)1 ?4 s2 ^5 j$ ^/ k9 k
scanf("%s",&re[j]);6 K7 e/ Q) u8 A9 Q+ i! ~
while(n_m[0]!=0&&n_m[1]!=0)! W; E+ @# v. f) x( m# l- d$ B/ v+ e, ^. q
{* I) d: p0 x+ b8 _2 O2 @
i++;' d5 @( e! n9 L8 x: g
scanf("%d%d",&n_m[0],&n_m[1]);
6 V' i+ _, a' W! J3 ] for( int f=0;f<n_m[1];f++,j++)
# N4 W3 @9 k& Z' I/ I scanf("%s",&re[j]);, X8 M# `/ w* f$ c+ m: V# u% k
}$ H4 D' H9 O9 x2 g- z
i=0;4 ?3 f' t% v6 j& z. e; B
j=0;
0 o S) C5 [$ N: X7 G& u/ f" w for( ;n_m[0]!=0&&n_m[1]!=0;i++)
4 w L! _7 S: L; ]/ K0 D {
, s7 h. R9 n6 H( L) x0 p/ ~ int a=0,b=0,l=1;
$ O& |: o! T. J7 }9 x6 R8 i for(a=j;a<j+n_m[1]-1;a++)
4 ?# F8 r! C& {$ W! m" U4 }* e for(b=a+1;b<j+n_m[1];b++)
" S* ]2 @1 d7 ?: ~. E {
+ j" T; `4 E- t$ [. N 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])0 I1 T) Q% n% R7 x
{; k3 l- E4 V( X- A6 o2 ~6 I$ \
l=0;
* Y# L4 e' U/ u' B printf("Inconsistency found after %d relations.\n",n_m[1]);; h! W, c6 D* h' }
break;" p7 o/ O, ~: |( \5 z/ |% r/ K& s, e
}4 ~7 x j5 y% J
}# ?+ l( d" Z% Q8 Z I4 V
if(l==0) 0 ?' q9 i& G8 N2 X5 h: u. w p
continue;//Inconsistency found after x relations.
: n4 R% ~5 n- d" C5 `4 s else{
3 w: M* i7 y+ i6 a8 R if(n_m[1]<n_m[0]-1)
3 \. v9 @4 Z& M {2 n. S! A- W$ K# u1 I- d( [* ^
printf("Sorted sequence cannot be determined.\n");
* ~" [1 X! V' Q% h4 m4 ] l=0;) R" l# @& D1 R, S$ k/ R1 C
}- p0 d/ [4 l- ]" I' |0 U
else
) ~0 F. G! W- U- w( p {
0 Q* H+ ^, [3 C+ Q if(n_m[1]==n_m[0]-1)
! g) C* b, z9 w( J {
$ M) K% v$ Z/ k; F9 T, T int k=0,p=0;9 l& h) _3 r& i! r! u1 z
for( ;k<n_m[1]-1;k++)& e% M0 q7 m$ @
for(p=k+1;p<n_m[1];p++)
+ ` ~4 ]7 R. c- r% E; ] if(re[0]==re[0]||re[2]==re[2])
" I( e3 _' A. A2 _; H {
9 j* H) c* v% p1 W( Z9 s% i- ^ printf("Sorted sequence cannot be determined.\n");
" V2 v5 O5 w2 i7 v# m& ^ break;& Z3 v4 m* K5 l. a% k
l=0;( e& i6 o* p& n A+ w* y
}
% ]: ^$ m; e& E" @0 ?$ v& f }
4 h" c# H: W! n }/ M( w6 Q' J, t y( u
if(l==0) - [0 ~2 G' c' h% _) O/ n
continue;//Sorted sequence cannot be determined.
4 S, [/ u, g. r- u, U8 J
2 j; r5 @! g0 w9 z$ H7 u" s: s5 \ else
# `) x8 c4 h9 P0 n3 O2 M+ p {
) v- [9 N; O) a' O
- O3 }# G! ]$ d6 J, T( I for(int k=0;k<n_m[0];k++)
$ ]" t, V- {. W" T5 c2 i) I' j+ ] temp[k]=k+65;" ~' M: [/ }8 [2 j( ?
for(k=0;k<n_m[1];k++,j++)8 N" a/ V; [1 W* ^) r9 r
{
6 A' f5 }( m: P J7 l* A; I int t1=0,t2=0;
( L9 q* H4 P! A: s& } for(int s=0;s<n_m[0];s++)6 u1 i, S- p' E9 Q {$ O
{- g+ i; N z* h
if(temp==re[0])
* \" B- ]9 e# y* v* _ t1=s;* S/ Q( g3 `# l. a' ?' T
if(temp==re[2])
+ n @8 L: p+ x: [ t2=s;
5 L N6 G6 @+ N$ b$ Z% b$ N9 G }8 \1 |. {% {" N8 R/ v9 {, w; |5 n/ E
if((t1>t2)&&re[1]=='<')
2 }* _. c& ^$ Z& ^/ _, i {
6 a; F; H0 J9 J3 n- o# d# A. w# b char t3=temp[t1];1 b0 k0 l# G C
temp[t1]=temp[t2];
) i1 g/ T4 z- e0 c' Z& I temp[t2]=t3;9 S2 | V% a3 o2 s
}2 t, |* R% b6 ?; t& x' L/ Z
}
' ~ T0 g: S. g int count=0;
& k+ c! ?: q" t/ L9 P for(int s=0;s<n_m[0]-1;s++)
& l& w1 e3 p( J for(int d=j-n_m[1];d<j;d++)
6 o; r' Q; p; Y1 U if(re[d][0]==temp&&re[d][2]==temp[s+1])2 J- a, r- [* ]/ q7 r
{
! m: h3 P" v" T% u- q. y count++;
' o) [! |- r/ R" [# P6 |$ | break;
0 }0 F6 i& A3 W9 q* K% u1 G }, j- b% c" {% \+ [1 Z! }
if(count==n_m[0]-1)! v# c* F0 F% g$ F* S) T( k
{
2 b7 g, b0 I0 z printf("Sorted sequence determined after %d relations:",n_m[1]);: m7 i6 j5 H- r6 Q2 @
for(int f=0;f<n_m[0];f++)+ a' u/ u5 V8 `
printf("%c",temp[f]);: d- A# V! i6 S- ]3 D* e
printf("\n");
1 E2 f& `0 z* A S% U8 v }$ t/ U6 e6 V0 V0 Q
else1 X0 c# T& h/ u2 U
printf("Sorted sequence cannot be determined.\n"); 9 G; R2 i. l6 W3 S( T# J
}
, E g% L& K0 U- v; _; g }6 a) L# q+ {+ Z4 L2 P# G7 C
}
8 o4 ^; A0 ^8 v/ S}
6 k1 W7 v& n8 [- K6 }" |
3 L ]5 K5 r4 o
3 ~- b1 p: p4 Z9 R( l2 g
( B0 w: t9 b; ^* A3 L; W& R7 q# }) i
3 Z6 h4 E; z+ w+ X/ m& y2 m
, n9 L% }! _& X$ _8 N- L
1 K/ W2 a1 n9 k3 R+ I" Z
5 v8 L5 G) W9 e8 Z来源:编程爱好者acm题库
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) |
Powered by Discuz! X2.5 |