本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 1 V# q) Z& A+ N& Z. L
; R3 X S- B9 j7 M& T6 JSorting It All OutDescription1 @, ?; h/ E$ S4 l
& w5 A5 e, C% i S' U3 R1 O0 OAn 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.
6 K& m; g) p$ q# l7 }* X9 `8 p4 RInput' e$ B) O: e: n/ c9 N M$ \0 L
x2 e; l/ L+ `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.
. a7 e/ M8 l/ j& k3 h8 [Output( x$ @) i3 c9 h7 o/ F- u
3 W. D' m* a2 e! }
For each problem instance, output consists of one line. This line should be one of the following three:
; N! [- [; g5 y0 y1 u/ W0 V9 V- M- P9 M" V7 L% k0 o
Sorted sequence determined after ** relations: yyy...y. N0 p. @1 Q5 N) H9 ~7 n1 T) @
Sorted sequence cannot be determined.
" t% Y( v' B [7 r0 nInconsistency found after ** relations.
9 M% i1 R. h7 w; O! ]; t( \5 Z) a- e6 ?8 `
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. " r5 w: b7 d4 E! g3 q
( f1 C! F u5 N
输入样例
) M3 T% s" t' a. u3 y4 6
1 ^! y+ V6 B. o+ PA<B
. I9 a: t' g, ~9 d' N! d0 Z$ pA<C
5 t5 J! w. }% f9 g, ~6 b; HB<C1 ^: E: K) h. A
C<D; f0 c$ A+ ?; x" Q, I7 L
B<D
9 e' w5 n4 R0 o# p5 K4 m( _7 g' J! CA<B/ \) T& P+ n- G! `9 B! q
3 2
; }9 h# \4 N6 T8 c3 TA<B' p$ p% b K; Z% s2 U! C2 z
B<A
4 I, u2 t" k% j- U) @9 Q26 1
5 t$ M' ~. k( G, R, @A<Z; i' J$ ^5 E" K4 R4 v4 R) S
0 0
2 R% j' A, @& j5 P; |, w 0 k: a2 J' r' d4 y) S8 _
输出样例
0 i( M+ r" f' _. ^: F1 \) YSorted sequence determined after 4 relations: ABCD.2 h. H3 ^; K/ k8 b2 A# [7 t' w: O
Inconsistency found after 2 relations.
$ A6 ^7 u7 ?. q4 z* \( iSorted sequence cannot be determined. * y* s+ ?) W! j* y
Source6 ~5 q+ l* \; a, d8 C
) T" G: J/ R" }7 e& AEast Central North America 2001 * A; i7 t4 g6 q/ F- n
程序解题1:
: d6 J' O6 y- G5 W2 }0 ]8 J//By Jackie Cheung4 r+ m0 J& O& F1 t5 m
#include <iostream>
4 r- b5 d. U6 T# ]% Z4 ~2 J#include <string>
" l" X/ j' v+ R#include <cassert>
F9 x" ~# B$ R* b/ ]8 Ltypedef struct tagNODE 2 m; s; K3 @6 Q& }% \
{
o6 ]7 }& i* y3 h4 z9 O% z; g char val;
. I6 z+ w; y2 F! Y/ q- Y5 z5 r struct tagNODE* link;
) B$ Z! E) O5 @3 W2 s n8 W}NODE;
8 l. y; w6 A0 D8 _using namespace std;+ }0 O* K+ a+ y: l
void Marshall(bool** arrary,int n)+ V% Y7 t% K" y5 K) T7 p
{
% P2 Y: e# c4 I& c; W0 o( t for(int i=0;i<n;i++)
# P b. I7 b1 }1 E2 U! s& K {3 s9 r( o/ |* A. J
for (int j=0;j<n;j++): q& G& f! Z: s% R
{4 E" C% w1 u6 o+ j7 k8 p; q
if(true==arrary[j])
+ U8 M: W1 |+ K# K% P for (int k=0;k<n;k++)7 a: E3 N0 D; ~/ n
{
& Z) f& j2 k9 ?: W. z N arrary[j][k]=arrary[j][k] || arrary[k];4 c* b, q* ?9 n* f2 g$ A
}
/ x* j% w; S! d) ^7 n# j, B }1 w: R' ^ {4 v; D$ R
}) Z7 s7 g L0 d, x: ]' ~
};7 a d; X. @3 L4 M3 F8 m
bool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n); `% T/ s2 f5 ]$ c/ I: s
{+ t/ y1 L+ e1 }. `
Seq[n-nLeft]=static_cast<char>('a'+nIndex);
2 n- V/ y0 Y, E% L/ T+ b# L* a bool bFlag=false;' Z' h a: P& Z. }
if(1==nLeft)9 b0 m& f9 w6 y) W3 u
{
) g! K0 t" e2 L. ~0 c8 h6 s Seq[n]='\0';, i; n+ H5 Y/ y, o7 N
return true;5 \2 l$ X6 o. B$ b8 `
}
Y" Z2 z0 ~7 f: c' w for (int i=0;i<n;i++)
( A# y9 s% P: X8 `2 T( m: O {/ s; c( r5 b- X6 S4 @
if(true==array[nIndex])
; ?; a7 C7 p+ v' ~# I {
$ t# K) R5 S: |9 y, G, K
$ h1 `9 c6 h9 }9 k" C8 U bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
- [) I& k7 G5 | }
: h* \, n) v1 C3 Q+ }1 \" e7 q3 T if(true==bFlag)* G& t0 n" f- s2 r
return true;5 u# q# F4 e$ B& l; T. W
}6 h A1 e/ l7 X0 l/ _9 c8 x" d; U9 B
return false;
" O6 j( y+ Y- x/ N3 K* _* G4 h};
7 n+ r* a) }' L6 }int main()1 t8 b4 L7 R7 x3 z' x
{ h& R& _# V0 ]2 m4 u. j* Z ?/ x+ Q
int nSeqLen;
' }2 e! b7 ?" [- z- T# @/ x) {4 j int nRelNum;% P) L+ ^; E- U% h
cout<<"Input the length of the sequence:"<<endl;5 s. F, e8 H( _7 i+ v7 @
cin>>nSeqLen;- a8 u3 k* O1 U( m6 d# W
cout<<"Input the number of relations between the elements:"<<endl;+ E1 x& s7 d/ {8 i) S& ~2 a
cin>>nRelNum;
2 e) _4 |: c4 k1 Z# E3 b //1:if nRelNum<nSeqLen-1,then the relation can not be determined!' N' S* U v; v; N' ~( C
if(nRelNum<nSeqLen-1)
8 {+ b4 @$ t3 y7 U5 h6 q {% `* X. ]. T" Y5 [1 e
cout<<"The relation can not be determined!"<<endl;
! X+ C5 D( r$ I2 p return 0;% w1 F8 D6 k! G- D, H( [$ [$ _
}
9 C/ y/ G8 L X1 a string* strRelations=new string[nRelNum];$ \1 x) J/ g5 @' }, `: j
char* Seq=new char[nSeqLen+1];! P7 O: f) [. Q9 {+ q0 ]
bool** array=new bool*[nSeqLen];
6 k0 K. F! y7 V3 B: L1 c
, W: a# l1 Y7 g9 ?) N for(int i=0;i<nRelNum;i++)
! _( R) p8 s+ E+ I7 i: ` {1 H- Q/ [# c: B) ?5 F: k3 I
cout<<"Input the "<<i+1<<"th relation:"<<endl;7 p% X6 \6 b2 U& s
cin>>strRelations;5 ~ n$ U" [) N9 m0 |: v- D" U2 w
}% M; s5 {' x7 b* V8 h
+ n5 {1 C h x for (int i=0;i<nSeqLen;i++)
7 D/ U5 k+ p l7 Q2 F {- D8 ^% R& @' o
array=new bool[nSeqLen];
& E$ a* ]/ o2 G$ n/ | for (int j=0;j<nSeqLen;j++)- m6 k- Z! m7 ]3 d7 H7 M$ ?/ @9 r
array[j]=false;
2 D; X, \. f2 |8 p6 a0 i }) l3 S4 G7 p* x& F( y8 c* w
//The main loop
; J8 J1 z% b8 |- L1 |' Y- R, w0 J for (int i=0;i<nRelNum;i++)
$ {# `/ l; {" ~6 X, h {$ r" ?: ^1 P! _) s9 F
char a=strRelations[0];
, z8 Z# o3 a* T4 c4 v) N char b=strRelations[2];
& ?2 O! {* j: B! \ assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);8 o P, |2 Z3 n- |1 w
array[a-'a'][b-'a']=true;
8 R( K) a/ Q1 z' S
+ w: `4 K2 t/ J2 x Marshall(array,nSeqLen);8 L3 e8 \; y2 b
/ U( h7 E8 T" i* M: g
//Check for Inconsistency after every relation( q8 h! K5 y! k/ U
for (int m=0;m<nSeqLen;m++)
) N( L, Q. m2 O: ] {
2 A. V; k: p$ u" w' ^( _3 T. d if(true==array[m][m])
, _9 n5 N( j4 i9 c4 u {" s; B6 Z7 {) s/ `- y) p
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
! M4 m* f( V3 u# W# p5 K delete []strRelations;
. K' Y7 V' H, h Y* }! [* `' k for(int k=0;k<nSeqLen;k++)
( [; H4 L. B; ]3 P( y) Y9 D$ a delete []array[k];
' Y5 |7 T8 A% F) Q% Y* h delete []array;. Q- X: j A, _- i i$ O$ J
delete []Seq;- i; j# j( K5 `) X
return 0;3 E3 ]$ z0 d' _2 L. n6 ]+ e3 y
# L9 I `. u! O5 F
}
+ {7 F ~- Q, h5 V( L6 v+ ~0 d, ^ }+ y7 y7 k+ _( a' N
9 D" o) }; X6 {. }4 ]! B //Check for the determined sequence after every relation
) H8 h; q2 n& c, g for (int j=0;j<nSeqLen;j++)1 A% x: t4 v. {' o# q$ M
{1 N$ L. `/ I) J# p! ?
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
3 V- v7 h8 }6 r( {% u9 J {
& ]; I, m6 P; w* ~' H cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
! y) l1 f% \" M2 ~5 \) |* S* A delete []strRelations; g3 M. S: K$ Y3 Q1 S
for(int m=0;m<nSeqLen;m++)2 ^, s# X; S1 v, c5 Q
delete []array[m];4 {* m% h$ |& L3 u
delete []array;
) ]) Z* _3 n8 S, Q delete []Seq;7 o* F7 ?/ S' O: n/ S m
return 0;) D- m# ?$ i3 S) p0 {( K
}1 W9 \: {& ~5 y$ \6 P c. W
}
; V. D2 G" B7 b$ a5 b8 V/ f) d% W# N( g5 u: I/ g
5 ?. K; G& B5 f9 F
}6 R% S b, Q6 X8 _: b1 a& q+ R
//If the program has come to here ,then the relationship hasn't been determined!!!) v& `3 _% k4 l @
cout<<"The relation can not be determined!"<<endl;
) N% S: ?6 j, u$ _; M delete []strRelations;7 K: O' y T, v; ]# D
for(int m=0;m<nSeqLen;m++)
. P+ R6 x. F7 k" a7 C/ T% B& G delete []array[m];
4 w2 V, D6 R' U6 ^( ]* [* r) t$ V delete []array;. N, E( |( n" u5 R# v% t% r0 P7 |
delete []Seq;) \* a$ e' X/ g6 y8 m8 K' t+ t2 {& ~* p
7 g0 ~7 o1 o& _& p: ] return 0;# A7 s1 q0 D6 l% z
}
+ p6 u E4 Z% B1 l r" S' M5 `" @' ?5 Q+ s
程序解题二:#include"stdio.h"
! M1 d. C1 [1 hvoid main()
) M" u- ] _4 V9 [+ C( {{" j- r; f5 p" E/ F6 k
int n_m[100][2];: C c7 m: l5 G; C$ D2 r9 u
char re[1000][3],2 C7 F% `# V! T$ ?( E
temp[26];
2 C: t! Q: L( q int i=0,j=0;
5 U' ^0 `" p$ ^; Y4 Q3 h" X scanf("%d%d",&n_m[0],&n_m[1]);+ u+ r" x1 j( o& v5 E
for( ;j<n_m[1];j++)
K0 P4 N2 v" c0 b2 { scanf("%s",&re[j]);' ?- r& y! a) r2 ]4 q1 u7 \$ ~
while(n_m[0]!=0&&n_m[1]!=0)
4 y& u8 O# x. Y+ S4 L {" H, i+ J- P( n- _. Q# e" y
i++;3 V0 k. k8 v9 @: i3 i k$ f4 M2 q
scanf("%d%d",&n_m[0],&n_m[1]);% K, ^3 ^6 O/ C0 S2 ?3 h
for( int f=0;f<n_m[1];f++,j++)
, F8 D, p* P" R; s9 Z scanf("%s",&re[j]);
0 r- Z' Z# f4 K- _& N }
/ v9 h k: H2 C- c0 @/ s4 W4 v i=0;/ y/ `; M h j, R; t$ n/ J2 [+ R
j=0;
- D2 |7 ^! J" |" I for( ;n_m[0]!=0&&n_m[1]!=0;i++)
+ N) ^9 R; b0 o" W n! F( Q {
' b- ?# A% c9 @9 r/ k Y int a=0,b=0,l=1;
6 q2 Q3 o3 k( ? for(a=j;a<j+n_m[1]-1;a++)/ G/ ~/ K& C! W; `: \- s
for(b=a+1;b<j+n_m[1];b++); [$ B/ C2 h8 `
{5 N/ N8 u- p! Z0 W: Y, k6 L
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])
- ~! r9 e& |- g {
* T% S% }# _6 G; v- X9 } l=0;
7 j* K) Z4 y C( ?. y8 W. i" q printf("Inconsistency found after %d relations.\n",n_m[1]);+ r4 l- g) x3 y6 p- X* N
break;
4 C4 i* ]0 p( a t* ` }
9 m0 Y" i& K. r$ z9 }, _) x }2 v4 Z# y5 c3 r1 G0 z
if(l==0)
2 s5 r8 W) f, ?# M9 R continue;//Inconsistency found after x relations.& e. y% {! `: u9 G! L5 K6 \
else{' G }7 k3 u* ^
if(n_m[1]<n_m[0]-1)' a7 G' i& q% V4 I4 q
{4 {7 V7 ^- J0 @, Q0 W
printf("Sorted sequence cannot be determined.\n");
$ k9 o! m+ b/ g! H l=0; r& ?" g% ] D2 W+ P0 Y; w
}
. r; N5 I/ v# H) F! A. v else, Q1 y% i ~' o% B9 |* I* @; k S# G
{4 x- T9 E# Y) u( n. G4 x
if(n_m[1]==n_m[0]-1), i/ ]8 x2 f6 v# j
{
9 ^7 K% q1 z9 U* l, B int k=0,p=0;! e4 M' b! b- @9 g, l" u8 G, f
for( ;k<n_m[1]-1;k++)) X8 U3 e1 |6 b$ f- F
for(p=k+1;p<n_m[1];p++)8 j, v% O$ g7 s+ X/ R- I5 F' [
if(re[0]==re[0]||re[2]==re[2])
( D. N& r% r; ]+ U0 H0 S {6 \& E) H5 e; }* ?5 i
printf("Sorted sequence cannot be determined.\n");
! D4 _. e, l. P$ W break;! X) e4 Z- d) R0 |
l=0;
/ E! B( O0 I# G }
( G+ Q* [/ V% |+ e" q }
9 I; d$ \) v; Y2 ~ }
# j! h0 t. K' t3 e0 h- V if(l==0)
9 @; R' D) C7 | continue;//Sorted sequence cannot be determined.
. Y. j5 W+ | W! W1 Z N$ e7 G' l% r2 A
else
5 ?# | d" k- U+ D' t {4 i3 Q) T5 o3 f8 I( E% g
$ i6 J7 ?4 K4 D' o. a5 q for(int k=0;k<n_m[0];k++)7 I; D9 I6 ^( U. G
temp[k]=k+65;# c& @# R- s0 W
for(k=0;k<n_m[1];k++,j++)# ]# @1 N" J0 f6 K+ h$ k' ^
{4 R7 g- S; l% p7 S( w
int t1=0,t2=0;: u6 K Q$ U) O( H7 J
for(int s=0;s<n_m[0];s++)% L: T7 s. ]* w/ H, `/ f
{
2 A5 P l: z0 W6 Y3 b# V if(temp==re[0])
- M* Q% C; n8 l1 `0 O: G' H/ {, R t1=s;# V, s$ f! G/ n5 g, ^
if(temp==re[2])
$ a6 [- r5 v* Q$ u2 G; A8 [ t2=s;# v$ w1 H: K _3 }
}8 v* U: R o# E6 Q; }3 x
if((t1>t2)&&re[1]=='<')
. F, n7 [; y# ]8 o4 z# y( d9 x. Y {+ {% U3 G3 [; y) \( @' m- k9 h
char t3=temp[t1];
% \# J: j3 {- T$ r$ c5 z temp[t1]=temp[t2];3 G" X1 j5 ~8 l1 E; j; Q
temp[t2]=t3;0 ]! k1 O1 }) G& [; n
}+ i% p* I& }' p# d7 f: }3 [
}* i h' W [5 g0 m- Y
int count=0;0 Y" V* d) b: z1 ^
for(int s=0;s<n_m[0]-1;s++)
* A3 _$ I) ?8 n2 u' g8 M for(int d=j-n_m[1];d<j;d++)
- `0 c+ F+ {+ c* N! _$ P if(re[d][0]==temp&&re[d][2]==temp[s+1])
, |" G9 D* W& j {+ \5 U. `6 G D4 l% }
count++;
5 N% A+ u; ^# _5 x break;+ L( z- V+ c( J
}
1 l p/ H, r* m, d$ H# K if(count==n_m[0]-1)
V+ n& Y5 F4 U- |0 V {
1 h) ~- x; s: ]& O printf("Sorted sequence determined after %d relations:",n_m[1]);9 M( b* q* [6 D7 [! E
for(int f=0;f<n_m[0];f++)
: d, v2 |% C) x6 S printf("%c",temp[f]);# W6 D2 v) { ?1 ^
printf("\n");
! i: _! k7 h+ P. K4 d, m }4 ^. f7 N0 J5 w6 `$ }
else
3 M# q$ `( T. W' @' r+ x printf("Sorted sequence cannot be determined.\n"); 2 t1 Q, _9 C: `- X1 n1 @
}8 ~0 @' \0 m! D9 S9 v) V2 h6 ^
}& V/ K5 E& c$ K
}8 [6 |9 g5 F7 @' ~) z
}
W/ I( ^# f& L% O' H! C, F9 t5 y$ v
. y. g9 a6 ~* i7 z/ o8 D0 G0 y) G- e
3 O; n2 y* y9 _7 Z0 h: P# h8 W; a/ \9 x5 c/ {: {6 o* J
4 Q+ ~' P( f7 w% U8 ^
5 s* N+ f/ M3 F7 u% X# G6 }9 F' ~% s) G/ M6 P. _
1 m! ~ G, L% Q1 {7 t6 R0 Y2 |4 v- F5 q
来源:编程爱好者acm题库 |