本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 5 E2 ?: b2 ?3 ^2 j; u" ]9 q: [$ e
- T' s1 w9 o) ], e: Z+ y( a. W0 z$ BSorting It All OutDescription
" T: j; G. t9 A0 _. x6 ?) B: Z3 ]3 H4 |( Q7 z
An 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.
" @# {( m7 g1 Q- j5 H. J2 JInput
9 }& ^% N2 q" I! g2 ~ i% Q( ~: f2 G# u
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.. K2 y9 t5 ^" x* N
Output. T# ~# f; ]3 \( G# t
, o/ `2 G5 A/ ~$ |) [* u
For each problem instance, output consists of one line. This line should be one of the following three:
, i+ @1 P6 W* q6 F% Y' P( n8 a/ t5 P% J# B( x$ V* f8 R, M8 e1 T
Sorted sequence determined after ** relations: yyy...y.
* y6 A5 Y& I$ c9 e1 M* vSorted sequence cannot be determined. ) C) Q$ R2 U+ M- T, o
Inconsistency found after ** relations.
! f; L- n$ X# f7 l! a7 \" p+ p0 X# z0 i- g% ]
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. ' X' e1 i- u# |
Q0 b8 l. i+ E. O, F: t输入样例
+ A3 \: B7 Y* E% X4 6 n$ v! W+ n2 R7 Q
A<B
. E. \" C3 {2 `A<C
/ [1 I* m6 d* r/ ?8 m5 WB<C
( C2 N4 l5 }, ?- i" JC<D# v! |% N9 G% v1 k$ U+ s0 E
B<D5 ?$ L% m9 p9 g! D+ \9 W) r! B
A<B
; p& x% A0 q' B. I3 2
) o5 [; B& Z! C% T4 N8 O7 OA<B
* \! w/ d( @" ? f# ^ Z9 cB<A
' W2 O4 K( B% Q26 1
' u& O% Q* R1 I5 U2 f/ l- \& pA<Z
8 H+ f/ d F: s. C$ @- s F; Q0 03 V5 w- r M0 {0 h P' F# i: ~
* d5 O2 X- |) e$ [输出样例
- p' @2 ~ Q8 e1 nSorted sequence determined after 4 relations: ABCD.
6 ?5 M' \6 a' EInconsistency found after 2 relations.$ v/ }6 [" N6 |) k% m ]+ r
Sorted sequence cannot be determined.
J! g7 x, U- W6 \4 M, pSource
8 h# R6 }" i8 p! N7 z% C; z n( o6 D$ K. }( g, G3 g5 {
East Central North America 2001 % C, i) [; h! ?9 L+ R/ H- V, V3 z
程序解题1: # u4 M4 _1 `: [& L* G0 U9 G [6 Z
//By Jackie Cheung; ?( a4 `; n0 j6 e# @" B
#include <iostream>2 C1 p% H _/ f' u
#include <string>
( R8 r3 f; t+ l0 d2 h9 y#include <cassert>/ b+ Q1 R. `/ Z
typedef struct tagNODE # O" Z1 C7 M5 X" Z
{
; a$ O% e$ R$ \; N4 R/ u# n) M9 J char val;4 p" P e+ v# S8 ?8 H* M1 W6 q% y
struct tagNODE* link;
& v$ i! F, Y% N+ T' `' a, e2 ^}NODE;
2 `% j0 m% }$ g: {9 A0 \using namespace std;
' c, A) x9 R: Gvoid Marshall(bool** arrary,int n), f2 s' o" |0 M% P; a
{- N+ a0 i4 B& u" h6 M5 @
for(int i=0;i<n;i++)
& n8 Y& @* M4 N {; [2 a' P3 B4 w! g& w: ?
for (int j=0;j<n;j++)0 k9 x4 v3 C7 X
{( _- u, N0 q. q% F# w
if(true==arrary[j])# v: E( m' b; U' ^
for (int k=0;k<n;k++)
; R( S4 L' [" B0 a4 i {
! c4 c0 Z/ j4 @& s7 j& X& L arrary[j][k]=arrary[j][k] || arrary[k];
" v* E( u) E( ~6 |( { }
2 S8 |; Z! S% I9 }1 { }
% Z! \' I: R8 k& K' Y5 G: X }
0 `+ \" r0 \2 M};
. k- Z" s- r$ J5 s# g2 z6 l: U/ pbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
7 r. i) C% V, a" w) F{8 Q* z6 \! \' H/ A: d* D
Seq[n-nLeft]=static_cast<char>('a'+nIndex);$ O( ~5 f3 j9 ?) ~$ P5 c7 l
bool bFlag=false;; I6 v8 A. K; U
if(1==nLeft)# N" g* a! [2 N: B; O
{
7 \* E: v' I* x) ~0 [! e4 q Seq[n]='\0';6 \+ i' ]8 A, Z; `! \5 Z
return true;4 K% x% D! ]( u) S' e
}
# {0 i" K: G7 W6 Z for (int i=0;i<n;i++)
1 H4 y9 R& Z) h. l4 h/ [ {* q. B3 A. i/ s) H8 l7 m
if(true==array[nIndex])
: d+ x3 U5 G/ ]. G& ?/ ~) i0 {, W; \ {
- G6 g( C$ t/ d, `& a8 u * E# u9 Z9 f) l1 D& z
bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);0 I" `# ~' o# _. V- z
}$ T* N1 K: ~ f9 Y
if(true==bFlag)2 A5 w$ g; F; T8 L; ?: ~
return true;3 S, N1 U; T3 k( k5 ?
}
2 y/ m, [% a( d" a, H& X' ~ return false;
. i2 i! |' G) Q+ i6 m};
! U+ m( M' y* n! ~0 S$ _4 ]int main()
; L$ P, X* g `8 k( W2 L{7 u# m! {& F. k0 ~+ Q( N' z
int nSeqLen;4 f1 ?0 \: a2 e& c
int nRelNum;. r- E. m @ A5 X6 g
cout<<"Input the length of the sequence:"<<endl;
$ e+ r* k- y; S cin>>nSeqLen;. T7 e2 x' l$ R5 d. o6 ]" r
cout<<"Input the number of relations between the elements:"<<endl;+ T; A; T1 \2 K9 l! N
cin>>nRelNum;; c$ D' m3 t/ J& a+ J
//1:if nRelNum<nSeqLen-1,then the relation can not be determined!
, }# s' } X+ e3 Q* j9 S if(nRelNum<nSeqLen-1)$ m( \ j7 C0 u- O
{
3 o# m/ u8 D; D cout<<"The relation can not be determined!"<<endl;( R8 \; E9 _( [& G& L7 O& P8 M1 H, t$ J
return 0;
* M2 ^6 M9 G2 r# D, e$ O3 K }
1 x6 z& v& P8 k Q# R& y3 w1 `4 J8 y string* strRelations=new string[nRelNum];
) ~* p: Y' u& m R1 g/ U4 o char* Seq=new char[nSeqLen+1];
: b: {4 R3 f1 ?# u- E bool** array=new bool*[nSeqLen];
% z/ q ~' k) u+ H( s( b5 W" {+ f! N8 R" l7 ^5 |9 y
for(int i=0;i<nRelNum;i++)! \3 D9 z2 o8 |. Y1 C1 |
{. _3 i% e) D, w4 C3 y7 G5 f
cout<<"Input the "<<i+1<<"th relation:"<<endl;
* s2 G# W, T; F) e* W/ @. H- o( g9 K cin>>strRelations;
' x5 D4 a$ m8 Q6 A& k9 \ }8 v" S0 B$ v$ j8 e0 S8 Q4 G3 G) P
3 d, K/ w9 G% l3 X- [9 `2 | for (int i=0;i<nSeqLen;i++)
( j/ P3 ^& Z8 W9 w3 ~+ k {* u) h( e* P8 \5 _( Z9 X1 Z
array=new bool[nSeqLen];8 p6 Q! q; X! _, v) |7 i
for (int j=0;j<nSeqLen;j++)
- T$ q, D9 H6 i" B5 L6 d array[j]=false;4 g4 I" x2 c) b$ Z+ t
} \4 P& |0 }) ?, i0 B
//The main loop7 |: R- g, L' d$ e- g- |
for (int i=0;i<nRelNum;i++)
+ U: u) t1 X6 ?/ @ {5 l. M% B0 x% L; z
char a=strRelations[0];
9 ~0 i0 Q8 v& Y u9 X0 @2 ~ char b=strRelations[2];4 u7 |* @9 |; n: |6 {9 E: Q) Y; g
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
) [; R& X. W6 u array[a-'a'][b-'a']=true;
$ F3 f4 A6 P- y) ]0 V& S3 ^
( C7 U: F3 \6 G% K+ u1 A& X7 l* m* _ Marshall(array,nSeqLen);
W8 m5 ?9 b0 P/ ~
$ I6 R; [% x% ]2 k //Check for Inconsistency after every relation
/ a0 u) s& O% @2 _) Y" p for (int m=0;m<nSeqLen;m++)
I: i' O0 O7 F( v5 q {4 l7 Y" O+ Z; w$ E }& e4 b; p
if(true==array[m][m])
0 o" i+ v7 w$ P- B {
! z; I$ }# {0 u cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
9 V5 T6 w1 C q0 X+ o delete []strRelations;
6 l& }! u8 L. R6 |1 ]+ P0 V( c for(int k=0;k<nSeqLen;k++)
7 J9 l; _3 l( ? g# o0 E. k delete []array[k];5 y5 h2 a2 G( B: r, Y B$ A0 ~
delete []array;- X7 x* U' w0 L D: q2 j
delete []Seq;
; I ?1 o* J* Y" {* j2 I# Y return 0;+ j6 h$ V( @+ y0 G$ M; J
2 L/ W' l6 f. Y/ J5 t. F1 k2 r } ^% S1 y6 E' Y2 Q. F# l2 B/ u% e
}
; a$ c9 G+ o( q& I. }/ n7 H
3 W8 @' m/ R, w2 y& q! @9 | //Check for the determined sequence after every relation * }! ~0 B# z( k; r% ?0 E3 j) y
for (int j=0;j<nSeqLen;j++)
1 P6 F% l9 ^6 g, _4 S {
/ I d5 X3 t, d+ y8 O if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))3 D* I2 o0 L$ Y0 R* n
{% M3 l1 u$ N0 `; ?' P4 u( `
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
) R& z( h7 v6 l2 K9 ]0 P' D delete []strRelations;5 R" L8 M6 z& N6 }' I& m. @
for(int m=0;m<nSeqLen;m++)$ F, c+ b- B5 w! h! X2 J+ Z3 q
delete []array[m];. ]* k. i; y/ d& C3 x0 |1 `; E
delete []array;* W* y6 z( |# h4 O. W
delete []Seq;
$ C: s/ l8 z6 s1 ` return 0;
" [0 m c, s1 o* O# L; e9 z }
7 A2 g% T% r4 e; l' g, b$ O6 R }
( K+ k! N9 q! p& k9 h* ~2 I& ^1 u- \
4 R. R' o) e7 u }
. d2 K ]" z3 i/ w //If the program has come to here ,then the relationship hasn't been determined!!!
% d$ e- f5 | {# `4 ~6 d cout<<"The relation can not be determined!"<<endl;
- m9 ]" E& k( K# R- u delete []strRelations;
X3 b0 W1 G" o8 t for(int m=0;m<nSeqLen;m++)
! z; Q" m1 ~$ k" } delete []array[m];, A- m* C6 B4 q0 s
delete []array;
) y1 K: r5 c" I: b% }2 p7 L" Q: r delete []Seq;7 t* `+ {1 x1 a8 Y, f0 N
( d0 }0 s% h" k5 f( d
return 0;# D" P4 q- S) A$ l, Q' D
}
& w# I+ i6 L* F% n
+ Y: J2 K2 r! V程序解题二:#include"stdio.h"
% H& e7 D: \: q! `" o$ B, w/ avoid main()
7 |* d- A0 f1 G* v{6 F' c0 |: m2 l$ S9 O# ?1 o7 M
int n_m[100][2];
6 I, k4 z8 t0 W* l. P char re[1000][3],& |1 Y) f& O8 A5 L% J
temp[26];- K' D2 v7 [$ b( `6 Q |" n( `+ N6 v3 q
int i=0,j=0;* Y/ x# M& g8 z) u
scanf("%d%d",&n_m[0],&n_m[1]);5 ~8 [6 A) ?% ^ \/ v
for( ;j<n_m[1];j++)
1 C: {% a3 _( o! O6 }* n/ L scanf("%s",&re[j]);
- D7 I4 }: R/ l4 ?' }# x: o2 K& Q while(n_m[0]!=0&&n_m[1]!=0)
7 o9 c/ L/ W3 t8 s {6 C# f7 K' `$ Z {
i++;
& L8 Q3 ?0 o4 A# V8 |9 v0 K scanf("%d%d",&n_m[0],&n_m[1]);7 q8 y3 l' O" f5 f0 L
for( int f=0;f<n_m[1];f++,j++)+ k5 I( F x& _5 g
scanf("%s",&re[j]);
' {( n; x2 r* |2 o% o N( G }, b3 g* V$ z9 s7 D$ X
i=0;
% r% a+ _: X. a j=0;0 r# t4 X& g7 e% _) B; P9 P
for( ;n_m[0]!=0&&n_m[1]!=0;i++)! o/ ?1 C: | @8 ^6 g, ]0 Q
{5 Y/ @% J5 A/ {* A" I; K7 ~
int a=0,b=0,l=1;
0 i( B7 T: d" [# W# \8 P for(a=j;a<j+n_m[1]-1;a++)& ]7 T' A! `! I( }2 R
for(b=a+1;b<j+n_m[1];b++). M; q3 I8 K6 ]+ y( t9 U6 b
{, G( J9 H. n1 J6 l! 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])
( w* v9 a: f/ q w" j {& b3 ^4 l# D1 c: d+ M
l=0;& p, N6 q( }3 B [6 t4 Q7 l6 e& _( M
printf("Inconsistency found after %d relations.\n",n_m[1]);* r- @' a( Y2 G" X' o4 ?
break;7 Z' ]* H6 a9 `: s4 P% E
}9 A/ {. O: h' q! S8 a
}- t5 g1 _2 t4 `0 ^. i+ ? y9 s. W6 }
if(l==0)
; t/ ]7 I- Y# ~: S continue;//Inconsistency found after x relations.4 i2 |" f0 ~' t2 r- w
else{
. v. B; R; J1 R$ B# F if(n_m[1]<n_m[0]-1)
" V$ V; K3 J) p, z& I& L1 [ {
8 m/ Y- C9 _; |& j) s9 o! l" g printf("Sorted sequence cannot be determined.\n");( Y& o: ]# @9 @6 ]9 l% v
l=0;: d: X* o+ N5 B. t
}, J U& I2 P& [9 i7 K
else
3 |/ u+ ] j# G3 c5 V, N {; e; D6 i. S7 o
if(n_m[1]==n_m[0]-1)
$ z" [' W. u) c! t O { 5 r# X0 }0 p8 H2 b
int k=0,p=0;
6 r, z9 V" m* |# {! I5 U for( ;k<n_m[1]-1;k++)
; [8 B% ?8 g T5 G6 j for(p=k+1;p<n_m[1];p++)3 \4 m+ V& I& |+ \1 f2 p( F9 E
if(re[0]==re[0]||re[2]==re[2]), P) p, N( @% x) Z! M' R9 P6 ^
{
) m5 Y+ T' y6 i8 \ U8 Q0 ] printf("Sorted sequence cannot be determined.\n");
% l" S+ Q" l: H {, L break;
3 g" ^$ d! }& B% d6 P l=0;
8 a3 r, r6 I* M. b }
( W4 S# A7 e6 I: m2 [+ X W2 ? }
* J! \& p* p. \% Z& _/ l8 c }
2 j4 `! T" G1 H' D. V: x* M+ R# x: n) g if(l==0)
5 Z; e; i& W4 P5 G" h continue;//Sorted sequence cannot be determined./ f2 u" i1 x* T2 ?/ A) B
& F' z* a, U8 o( c else
8 g3 U2 E$ L4 s* t7 c. |; V3 i; H U {3 y5 X+ z- R }
E7 {* Q7 [- i( V$ r$ R" r" V
for(int k=0;k<n_m[0];k++)' V3 n7 f. C. x6 k1 M! t6 E
temp[k]=k+65;
: ^ R9 {$ T S* A5 Q" J* g for(k=0;k<n_m[1];k++,j++)0 O8 M; \ G( g+ `# {8 ~
{( L2 [. E( ~' l0 n; a# J
int t1=0,t2=0;
/ e" p! I3 j1 q for(int s=0;s<n_m[0];s++)
& [- v4 Y3 u- n% c% A {
/ q8 L3 k; E l) |- l if(temp==re[0])! f& y; O1 S9 s/ S
t1=s;
# m/ w( S4 B- {$ p5 ~- Y2 A if(temp==re[2])
7 k( W! l3 F& {7 q, O3 @2 n" Z+ Q t2=s;
) _2 ~8 J9 y; b7 |, Q: q5 x- ` }0 B# T: a k2 D- C8 M6 ^
if((t1>t2)&&re[1]=='<')
$ n6 r5 c0 t6 [ {9 i' r9 D4 g. |* ~- m j
char t3=temp[t1];
6 q- ^0 @' `, Z9 m2 d* N' _ temp[t1]=temp[t2];' c1 }* `, Q, r. p& X
temp[t2]=t3;
7 q# Y Z N6 b8 O6 [ } H2 U/ `& A: ?/ ^& G" U. g0 w
}0 ^, a8 l3 Y0 R
int count=0;8 q" K! K, O- d3 G% u. e6 ]. [
for(int s=0;s<n_m[0]-1;s++)7 L9 }$ j: M) T. S1 b
for(int d=j-n_m[1];d<j;d++)
6 m( R5 h8 e8 A/ v l if(re[d][0]==temp&&re[d][2]==temp[s+1])/ \# _+ P! O8 h; u+ C# @) Z
{3 G( H" t5 u% v
count++;+ O1 t' q4 m6 K5 v5 D
break;" Z/ g; q9 S4 i9 @0 h5 s" i# y
}/ E1 B6 j1 z! o( ]* c& n8 p
if(count==n_m[0]-1)7 v) I& }8 ^0 |/ D7 ], u4 k+ g
{
. F% ]; ]" i* Y& V printf("Sorted sequence determined after %d relations:",n_m[1]);
4 Q' a8 \: }, A+ H, ? for(int f=0;f<n_m[0];f++)8 _$ ?* ^: a3 K. }( l! `
printf("%c",temp[f]);- n$ R7 l w- H; Q/ e+ }
printf("\n");
J: ?1 g8 R% F; p7 |' x& B }
: p( N( W' d$ M' s+ c6 j$ M7 l else
8 t) _+ l7 ^: h printf("Sorted sequence cannot be determined.\n"); : E! f, _8 Z2 N& L+ p9 a, u
}: ]* w/ C5 s* f+ L. C6 k
}4 `7 H2 A6 C2 D$ s4 S$ f# O4 B
}
9 H5 {& D% T v$ l0 e}
; p$ w- w2 u) W/ s5 O/ Z- X/ l& o! A4 t$ e
; I! n1 Y! {0 w3 h3 u
7 _" M t* h* n
@( H1 {6 h1 m+ Z' \ R0 N; ~: l4 V6 p1 [
+ h' W5 ~' {$ R1 Q' @* Q6 H1 @0 U( j! C$ ^# ]! z. J/ P
2 w& p8 b7 S. L' R$ D来源:编程爱好者acm题库 |