本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 ; b6 h# X1 G; u3 N$ S
, f& J% v" D; a% A
Sorting It All OutDescription
% z" g! J2 E+ g9 l5 b6 q# c
' W2 [( u' x+ K. i% m! kAn 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.
: T* z) o K& CInput
- K" [8 z! ]" n0 W
7 \/ r s. S# _5 F0 |3 @( v/ CInput 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.- a. q! [9 F- N& d
Output
0 V: B7 p! {: Q
$ f/ d- d: {% ]: x; N# w3 nFor each problem instance, output consists of one line. This line should be one of the following three: & Z; _- b. C& P% s: ]
; O& i% c J7 c- m# b4 x" _ g
Sorted sequence determined after ** relations: yyy...y. 3 z( l9 C9 D6 z$ N* D
Sorted sequence cannot be determined. 4 @0 F" q u1 Q
Inconsistency found after ** relations. - W8 ]- m8 R1 g5 n2 c3 t" N5 G3 V% I
4 P4 o& _# P1 |5 u: cwhere ** 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.
7 Z% L q$ _4 R- U2 K+ T* K7 w& L$ v5 Y
输入样例 ) J6 \- D& u; R y
4 6
% U! k/ i3 g) u- m( V: _9 WA<B
4 r9 b; X: A( TA<C H, i; m2 {, ]4 Z6 [
B<C
4 y x, M) \- ]9 e; g: l7 u% uC<D z2 g6 A& E% t# {6 e
B<D1 \* _0 m9 x! W3 m/ H
A<B$ R8 L+ `/ |3 t" h
3 2* ^2 l, ^5 d! s: F: Y' ^) g6 _1 S" Z5 [
A<B( i: T ^8 r! F" b O
B<A
# x6 t+ ~: x; R+ A. M9 }26 1
6 h, Y; `) n8 c& sA<Z- c/ U3 I4 u' P5 h/ P! S6 W
0 0
M$ I6 _% V( C. E 3 v+ g1 q! r! `& k! ~
输出样例 , c! N* u1 H8 W, \4 }) x) s
Sorted sequence determined after 4 relations: ABCD.
9 m2 p/ s: b% b, A$ {1 T* KInconsistency found after 2 relations.
% ~% U8 I* H! ISorted sequence cannot be determined. 1 R9 n- z, u8 b6 B/ h
Source1 l3 F0 T/ u2 y+ R0 z- V7 B6 ?
* U! W; l8 z. f. L3 v2 c3 E2 q6 C9 f/ U
East Central North America 2001 6 d/ q I2 Q- H {" R7 a
程序解题1:
3 w4 Y; i4 b) A//By Jackie Cheung; M/ t, }# C1 ]/ P- J2 }
#include <iostream>" O$ p2 m" [, {( ]+ _
#include <string>
; t) V1 t: y7 `7 v#include <cassert>1 L) k) e2 `/ d* f$ ~) r) ^( B5 F% \% ^
typedef struct tagNODE 7 y1 v$ Y: a3 R4 B! f/ E
{
( T, M, Z* r% m) Z' P char val;0 y9 D7 A+ P, H) s- {$ k6 I/ t
struct tagNODE* link;
" M! j( ?6 e* W+ w6 B}NODE;
9 M$ s* K! R; F% _7 Rusing namespace std;
: _+ Q% Y" ]* v+ ^3 z3 _void Marshall(bool** arrary,int n)
0 O7 o- }( y7 T' G2 L6 D C{: d2 G" l" \# R6 r. \; p
for(int i=0;i<n;i++)3 t; Y2 d7 H( g) @5 ~; i. C; b/ A
{
3 b! \: q7 ~2 T! t1 f- n for (int j=0;j<n;j++); f7 ?5 \8 m6 o Z& T3 {
{; F' Z& r7 X- q! G: d' q4 z
if(true==arrary[j])
% _1 f( e, p7 A2 O) d) T* t for (int k=0;k<n;k++)
+ s) w9 m- Y6 _" ?- H- k, e {. c9 q7 r; X) v# F r- ^2 Q
arrary[j][k]=arrary[j][k] || arrary[k];
8 M. {' T8 S* G% e, C8 M4 U, N7 i }
6 V& w. G% e/ T7 L# O3 p4 l }) ]5 a7 n' g1 V) T' r
}8 m" ~: d3 H9 a2 O+ H" U- h
};
3 c2 w8 a+ O+ p8 e8 Bbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
. ]: S1 v/ j* B6 M* V8 A" P{
$ W r @6 ]+ A/ {; g3 u6 h' l+ w# h Seq[n-nLeft]=static_cast<char>('a'+nIndex);- C2 p0 @1 l+ j/ [& I1 Z
bool bFlag=false;
4 s* _& y3 t0 S7 E" T( ` if(1==nLeft)' R- p) K" D9 E; {9 ^
{1 k) N% D' Q: @0 B+ l Y
Seq[n]='\0';+ J" u4 ?* _7 a* u4 H3 L
return true;) J9 C' s7 r0 n( `5 J
}+ M! A1 L3 B& r
for (int i=0;i<n;i++)
$ k# U- y- l; n' J% Q {/ w) Z0 Y. P+ G* I" ~
if(true==array[nIndex])3 O' `$ H7 T$ s% B# T
{
. ]# X4 u/ u; H4 A7 h
5 L8 J! K! q2 V7 N( w' X. d* w0 q9 ` bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
1 ~) m- M5 _. Q) F9 G6 p }
) F7 C* A6 B! N+ t7 G) r+ | if(true==bFlag)9 y- G7 @) ~7 y3 H0 M
return true;4 j$ f* \# y7 L' J4 h* e3 o% P
}% X9 _/ p. {+ Z6 q1 h4 _& O! Y
return false;& k( Z w, a) a2 W; V: _
};
* b6 Y: x8 k+ f4 `int main()
. o( w( p/ G i& H2 @{
. d- L1 d4 ?5 m3 i, h* A9 r6 h5 f int nSeqLen;9 x3 i: U! {8 w2 Q
int nRelNum;( K$ Z8 @2 V$ x/ I9 ~0 c% {% D8 y, N& H% F
cout<<"Input the length of the sequence:"<<endl;4 \ c& R& ~% a" ~9 M, K
cin>>nSeqLen;0 b4 A4 Q. C$ U' M! e5 i
cout<<"Input the number of relations between the elements:"<<endl;
2 a2 K, W% |7 A2 d cin>>nRelNum;
( V. r( }; D6 W5 w5 F _6 R$ R //1:if nRelNum<nSeqLen-1,then the relation can not be determined!. k" w" m: y2 q+ a( Y. b1 L( ^ I
if(nRelNum<nSeqLen-1)9 R, l3 f* x! x/ u
{
6 A8 D# p u& n cout<<"The relation can not be determined!"<<endl;2 ]& ~! }# V' S7 X i4 H
return 0;
' q' b. Y. O8 O; m E }
! _) W( R. N }& V; i: @1 X, _( u string* strRelations=new string[nRelNum];1 _& \; l0 s: @
char* Seq=new char[nSeqLen+1];. i' Z3 _* S( t- F; L: s! d: t- z
bool** array=new bool*[nSeqLen];
, {" [; U8 `; U4 N8 }- D1 h$ {+ A2 b. v9 O9 ?; J* U+ C; ]% b
for(int i=0;i<nRelNum;i++): }5 s. V: m: V; u1 m+ X
{; d+ h: H" e9 R. u! B! |% q
cout<<"Input the "<<i+1<<"th relation:"<<endl;
2 {4 d4 K' a, }* |$ ~ cin>>strRelations;
0 ^, p- A$ F b% m- T }
( H+ \5 I2 t; x3 `1 g0 J 0 o) y! x3 T. |; J
for (int i=0;i<nSeqLen;i++)
/ {6 ~4 S1 O/ E {. P6 l, V: d, @
array=new bool[nSeqLen];5 [$ \2 P; v4 d, `
for (int j=0;j<nSeqLen;j++); ]: K4 P' l5 p, Y
array[j]=false;" k! H- K% I& H9 ?& v4 C
} G( W+ t7 f6 }, E! I
//The main loop
* F" D6 ^' n4 d* `: h6 t8 L) K A, l3 F) Q for (int i=0;i<nRelNum;i++)$ A5 N" @. l! n
{+ i' g. K5 O3 ^" x* z0 u* f* T
char a=strRelations[0];
/ S4 v" s; w7 C Q* x char b=strRelations[2]; e2 c5 f% `: g: p% l
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);9 Y% Q4 y; U. h% ~9 z7 T
array[a-'a'][b-'a']=true;
# I* W, c* W: I2 |9 Y- @5 }
0 g& E0 m6 O* j" U$ D Marshall(array,nSeqLen);
- G* w: d. G8 q/ m* m" t) b( i% Z, v
% i: v) Z* |/ W8 q3 t# a, @3 V //Check for Inconsistency after every relation3 e' ?$ s; Z4 e$ B# I1 T+ h
for (int m=0;m<nSeqLen;m++)' U0 ?& n2 j" Z& i" e+ C6 J9 q! _
{) z' A0 v+ }: |8 y9 Z( C
if(true==array[m][m])$ H! {' ?& G0 p4 |* q
{+ [$ B3 v+ G/ `
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;5 U! d. _. A2 N& f2 R
delete []strRelations;
: f2 O( l/ {! ~# j( A for(int k=0;k<nSeqLen;k++) Q8 h' ~( U; I$ o+ Y
delete []array[k];
$ t+ x2 F. V7 [0 s z: P delete []array;
; e2 x2 r7 O/ r: h9 c* w delete []Seq;8 J N V9 p8 a( b# \) | u
return 0;$ R7 K$ _1 f5 C+ f# R
1 Y- g {4 y; @! U# \: `1 z }
& h: x/ o2 U9 U5 w5 R% f* E }! M2 n( F- h/ f" I# V2 J. |; S9 k8 b% E
& s5 S0 ?( I5 d8 _8 x' L5 Q //Check for the determined sequence after every relation
: y1 S/ O. x- N+ F for (int j=0;j<nSeqLen;j++)
* o- l, s- d" ^* F/ V/ y {, |' M# p L# i
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))$ d; N: M6 M" M; R
{5 [0 `6 k1 A$ V) W- ]
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
; C$ [: t4 n, u. t; @8 w" F delete []strRelations;
7 Y' f' s4 m+ O, G7 n( m2 b for(int m=0;m<nSeqLen;m++)
( \) `/ x% [3 E- Y; T5 a delete []array[m];
8 Z# {( ~$ p& L delete []array;" d1 j% ~3 s2 @, W1 j
delete []Seq;# U% e+ M4 N' T& b
return 0;
1 k" b9 G( A/ |' G3 I2 q }7 i, b; r. k# g R- A9 ]1 C
}# j2 V% K" m+ D+ e w3 b. P% w
: E# q) J/ W; T1 t3 v
V; ^) c$ l8 k }+ v. J4 o* `1 f9 \+ x* ?& H
//If the program has come to here ,then the relationship hasn't been determined!!!/ b9 r& }% H' ^3 Z
cout<<"The relation can not be determined!"<<endl;
7 ~# L0 E% R i1 L. J delete []strRelations;3 I V! A+ N, F3 `. P* S( Y
for(int m=0;m<nSeqLen;m++)# `, e8 n. h9 z
delete []array[m];) {. P5 b- @ e) Y' M( h
delete []array;+ p2 C2 [) R, \; ]% S) Z; [3 Y) z
delete []Seq;" `$ s# y4 G; K3 ~' h; t7 o
) H7 X( @3 `# o8 T/ a1 p; E return 0;
4 X0 z9 e* ]4 m" ~- k& S}3 p- Z% o' v$ K
( _7 h5 u; k, F" ^程序解题二:#include"stdio.h". K% p: J+ t1 g. n$ p; L6 z0 i
void main()
9 }. |! C. K) g8 m$ ^& z( {{9 A8 p/ X. S4 M1 e6 U, J+ d! j- s
int n_m[100][2];1 q! N$ {/ P/ O* g
char re[1000][3],
1 t6 ?- ?3 w0 L' U temp[26];0 c7 t! ?+ z1 z4 t! W$ ~$ V/ W
int i=0,j=0;7 W7 a2 E; q, F
scanf("%d%d",&n_m[0],&n_m[1]);( [5 F- F, e% X+ Q
for( ;j<n_m[1];j++)
/ {8 K3 ^5 [# b scanf("%s",&re[j]);" t. D- S0 X8 a
while(n_m[0]!=0&&n_m[1]!=0)
' H$ z W- p3 y' g" B6 K {, V, S! m4 Y0 R" B3 J
i++;( N8 S# P% R6 W- I" @
scanf("%d%d",&n_m[0],&n_m[1]);6 f2 v; H! R! I! l
for( int f=0;f<n_m[1];f++,j++)! R. R; S8 k# X7 f: y$ z
scanf("%s",&re[j]);# D$ f3 s* }* A Y
}+ S/ o8 Z. ~$ s2 d$ |
i=0;' ^1 y$ |7 B% d6 K3 t. @
j=0;
) D0 A: W$ \9 |8 }8 f$ e for( ;n_m[0]!=0&&n_m[1]!=0;i++)
B- @- ~) z& Z* J& K" m1 m# J" S {; ]# `/ g" \% D' h7 l- g) }
int a=0,b=0,l=1;
3 J& N' M: K7 i6 [ for(a=j;a<j+n_m[1]-1;a++), A9 @% V; Q/ a% V
for(b=a+1;b<j+n_m[1];b++)
$ a* A/ t( r/ G: f {2 E% |6 ~$ ^" i1 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])* _- g) r4 S3 Y1 a- v( N' m
{
- I$ h6 L; S) `* I l=0;
% R$ R7 I: p: k2 K8 E printf("Inconsistency found after %d relations.\n",n_m[1]);: G/ h: ^- |. J) P8 {) P% b
break;
7 h/ ^& x8 q$ U. ?9 Q4 R }' r* c' z3 Y6 b \
}
& n! c* t( `) R5 m: C4 P" \ if(l==0)
) L4 H9 N; F5 F' |9 X& u; U continue;//Inconsistency found after x relations.
# @7 z+ C( p/ E, F% {2 } m, J$ d! y else{9 h7 E2 o* U* x) L, V
if(n_m[1]<n_m[0]-1)2 C& q& y" q- S: X9 [# g. m
{. y) A5 @) c6 R* l+ s/ y
printf("Sorted sequence cannot be determined.\n");
' P! z$ W4 N: T+ v+ {( j3 } l=0;
3 x8 X+ f9 S( \+ c/ G }% H7 I% G5 N, C' }' r* Q- d
else
( e/ `7 z# b: K( X {) C' W+ z c4 O& O
if(n_m[1]==n_m[0]-1)4 q) j) @0 Q3 b
{
' V& E* ?% U k. n' s2 D' D9 ? int k=0,p=0;) K. o+ S8 M4 m
for( ;k<n_m[1]-1;k++)5 J$ h) o1 t" f4 q2 O) V; S d
for(p=k+1;p<n_m[1];p++)
* ~0 h1 P, @- v; `/ z* W if(re[0]==re[0]||re[2]==re[2])
1 a2 a& F; Z, {# H3 Y, W; L* o/ Q {
; M3 `3 z0 Y6 s G printf("Sorted sequence cannot be determined.\n");
. m/ R& T+ _! @5 E break;- t+ G3 r, V2 ^" G; j# }& N1 ~# |
l=0;
; y, I2 `6 g+ U4 T6 N- c+ G }. H, ~& N% B6 H _
}9 x$ ?! r- R' `1 B6 m
}
8 o' N4 }- y9 n1 Z. ~4 r* K9 j if(l==0)
3 o4 F% @+ e4 T' n3 o+ X( y/ Y continue;//Sorted sequence cannot be determined.
) L* e3 V$ [$ f: v1 O9 _1 @
4 y. d+ [! _ P h$ s2 D else
6 R8 {+ K5 @; u5 x. U$ ]# d. } {8 {4 f( x$ a+ _* w2 l
5 j0 k ] R. d! j* L: G
for(int k=0;k<n_m[0];k++)
, k; O( }/ y+ z7 i3 `3 L) z* A+ ? temp[k]=k+65;
3 o5 A" ]! d/ k3 B: @# p2 o: q$ O for(k=0;k<n_m[1];k++,j++)- _. G' T8 I5 k1 G* X
{
0 k5 U. h& ]1 ?5 I$ { int t1=0,t2=0;
* I6 S: t5 \/ C+ @ for(int s=0;s<n_m[0];s++)
C" h4 y! y" @% ~* R. F" a2 k( ^6 W {6 {# q( l- ]' R# f% Q
if(temp==re[0])3 M { Y& S; Y0 O
t1=s;
5 f1 w! c" ^: L4 T- Q& K if(temp==re[2]); h( x/ F. M" \( T' Q+ H
t2=s;: r4 H' g$ p4 o$ J4 p B) W
}
* ?% P. p& Q$ b1 W1 g1 [8 X if((t1>t2)&&re[1]=='<')
$ [; k( f& q5 J# |# z" N {
" ~" q2 x U4 q' {% n7 Y char t3=temp[t1];" S/ }6 ^9 G( R! V4 C8 I0 [9 b
temp[t1]=temp[t2];
6 z9 a- r7 V( B8 l- U/ S temp[t2]=t3;
( ?+ B) S2 u# R# L8 ^. I/ ] }$ ~' \7 A1 H! |+ J- y
}+ o1 X& H5 v6 }2 l& x J# f
int count=0;/ [3 E! v+ Z \ H
for(int s=0;s<n_m[0]-1;s++)' j8 b* J. q( d* N6 l3 h9 _
for(int d=j-n_m[1];d<j;d++)
1 D7 Z, c) X) D8 O2 C$ W if(re[d][0]==temp&&re[d][2]==temp[s+1])
: Z- O+ g+ @* A+ t$ X1 {" c0 T {" J8 z+ O0 y% @5 I$ _) \5 i
count++;: K' h$ X" d' V% g
break;
# M8 H7 ~; F3 ?+ m& h- h* G3 w }* V% P1 u# j: q2 l2 {! _
if(count==n_m[0]-1)
" f7 d) i$ V; T1 p0 w V- L {
- H8 _/ }& ?7 s4 H2 C4 ] printf("Sorted sequence determined after %d relations:",n_m[1]);
( W7 A% L8 u% A* d% V0 v7 F for(int f=0;f<n_m[0];f++)
. \: v5 ?7 N; @5 ?& S4 W printf("%c",temp[f]);8 R9 C; R. p+ }3 I7 R- j
printf("\n");
6 ^/ h: m. D# F0 q# h8 ` }
8 B- t( r t; r) c& r6 V else
7 E% [7 D' J5 M b0 p4 }! s, [& b printf("Sorted sequence cannot be determined.\n");
& S& Q" S/ h. L }. r; i& O: d& ~& Q" H- G% A" e: P$ _
}
' j; _( ^$ |9 z I } ^) k1 Y5 M0 a: n; M$ R) ~
}9 s/ z) V" G* y# ]4 i! q6 M
7 K2 @2 t% V9 t
* ~7 U: b/ g) ]& ^- _' B! t
: B( n2 `; q2 `5 P8 [0 ?$ b; B7 h. W; ]2 j2 J
% G/ ?' n/ [$ h4 X* O" {* d
1 G( I* ~2 [5 j' V( |
5 S4 \4 v0 ^ h5 Q9 R1 \0 \: O% n% ^( G1 h# `& r; L- r w
来源:编程爱好者acm题库 |