本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 0 F* |0 W; v! |1 m# U' t
! Y- D6 V& b0 m2 a" q
Sorting It All OutDescription
4 g. r, J" E5 o3 A% o. q5 G8 t8 j, ~0 I g# D
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.
" \$ R+ _& {$ qInput
, A) s# x( M8 p' Z+ Z L' E' T5 S/ X5 N3 ]
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.
o! P, Q; @/ I- d1 TOutput4 P/ O$ \* J1 V, d3 H) Q; Q
6 e7 j% i# f5 [! ]" E& H
For each problem instance, output consists of one line. This line should be one of the following three: * F* I5 P* d1 D+ v; h
8 c$ x. H" d8 u; e" Y8 |! \Sorted sequence determined after ** relations: yyy...y.
8 z$ n, C; n; i5 s' I* u; }0 uSorted sequence cannot be determined. * j3 e6 \4 |; q8 f r
Inconsistency found after ** relations. & Y. Y2 g: V! i$ d/ {- ^
7 Q3 i- D% P/ ~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. 3 }5 Y4 `' R9 v5 z9 J
8 P1 B4 t W: C输入样例 8 j% m2 X0 U* [
4 6! \# x, B7 ~1 e$ L: Y
A<B5 n! t7 U9 L+ ]! X
A<C
5 z ?+ s/ H. u+ W \6 `9 Z6 Z) l A8 IB<C
" R2 f4 @/ h O- z( t5 KC<D
" l0 }5 s+ u( eB<D
( g7 \" |# c6 R/ o" Y. s4 o$ F8 d9 }A<B& h+ q- [- Q, w
3 2
+ b, s& {! J8 c4 n0 cA<B
+ N% W) ]9 r( G3 x6 A; } NB<A; R( P5 j6 e3 F1 g- N" n6 W
26 1: Z; M( A- P- {! X
A<Z+ T3 A; N4 m1 x1 K, A
0 0, d$ \. v( \3 [ O. K
# B% Y% u$ n4 P输出样例
# N4 Y' ^' e7 p/ ESorted sequence determined after 4 relations: ABCD.8 c# z$ z+ N( Y. h: R8 @7 S+ }
Inconsistency found after 2 relations.( }9 H' \$ |% b: Q6 `! r" ~1 A/ c
Sorted sequence cannot be determined.
9 B2 }+ R$ {! Y0 q* ~; ZSource% d8 x+ U0 o6 a, W, D/ Q
! m/ a$ t: g5 h. z# C
East Central North America 2001
0 F- z7 W" p6 |9 ?" J* p5 V$ ^程序解题1: ; {- T$ j' i. E) {3 n& o
//By Jackie Cheung
2 Z, ~3 m0 g# B1 { L#include <iostream>
8 V! f: h) n$ N ^4 S, F E3 q( q6 g#include <string>- N: M9 a! Y: Y4 n9 X P: P1 n
#include <cassert>4 a% d9 |# ?. K6 ^( _* J
typedef struct tagNODE 1 y7 [( _, U- C. O9 C( l+ F
{: J3 V/ B, u E( q% e
char val;
& u/ d+ a$ i! S+ `* Y struct tagNODE* link;
* i' L. O1 ?. }0 V}NODE;6 k. }. R2 f+ W* w1 f3 o" u: t: [
using namespace std;, A7 k3 K! b3 Y0 g3 J
void Marshall(bool** arrary,int n)# ~! ]2 \+ I4 I# M' K5 u! j
{
/ T5 q) s* e6 S& l2 O for(int i=0;i<n;i++)* E* F! B5 V; n; y- j. n
{
4 s4 x5 L+ @+ B C7 K for (int j=0;j<n;j++), t0 ?: G2 c! r: G" R7 q( H
{0 i" a: F: W1 U; H3 v/ I
if(true==arrary[j])
4 N/ k/ q" U6 [6 M: ]1 {' ]2 u/ b for (int k=0;k<n;k++)
+ t- ?+ G) u" D& S* K) y# R0 ]! v {
( _/ k" Q4 B ]* {2 m4 `' b( l2 u arrary[j][k]=arrary[j][k] || arrary[k];
+ N- l: C5 `$ H/ C }7 z9 w: \. ~' o
}
0 p# g6 g9 h6 w9 ^ }2 i1 b; V0 {( P! d0 `
};
/ U3 v! T! ~; v3 U: obool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)* }; y/ Z$ s$ b) a6 v0 n
{
/ ]' J _ G+ e/ f Seq[n-nLeft]=static_cast<char>('a'+nIndex);, X* r4 m$ s" |& O
bool bFlag=false;0 p. x3 \& i$ [2 U5 q
if(1==nLeft)6 h# P0 p. u7 J9 E* d
{
- s) @- t. L% T T+ N: @ Seq[n]='\0';
* U7 p( s! u# A( R* C+ u/ R: D return true;
0 |0 b: z4 ~' a# M0 X" q }- a/ O7 \2 m, O: E' M, I* R0 D$ `
for (int i=0;i<n;i++)5 c& U7 }6 q/ s/ }# r) F+ w
{
2 q0 z5 T9 d0 z9 B P! b. s0 ^ if(true==array[nIndex]) {8 ` Q' Y8 l- Z0 d
{
: D F$ B: H* G0 k4 Y! z% [
% ^! o- P0 v6 k. V/ a V+ } bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);7 B' h/ M& k6 w) x8 ~& y0 G
}- {, I, L) v) e
if(true==bFlag): b; }& T' c! {. @
return true;: H: Y2 ?& n; t9 o0 y
}, r& _7 u% k6 v* }) L+ C7 |9 j# l
return false;
( T) r7 m, k5 _! J};0 {$ G% }. a8 y S+ C
int main()
+ e& | h/ W( r; y* N1 n9 k{
* Y" I. t. Q3 e! }9 p int nSeqLen;% X" g1 X. _7 D
int nRelNum;7 D7 V: F3 h2 f9 z
cout<<"Input the length of the sequence:"<<endl;
$ |, g, t( Q/ n& v( {7 | cin>>nSeqLen;
* } b# T J, V9 Q; S2 Y cout<<"Input the number of relations between the elements:"<<endl;8 ~: E' S# A- q
cin>>nRelNum;
, A3 h. t- E! X- T4 i/ D //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
3 i$ q' Q _$ H if(nRelNum<nSeqLen-1)7 X5 {9 a: n; _; p( f* L
{
' q7 T' d8 j& Q2 ]8 T# b4 Y cout<<"The relation can not be determined!"<<endl;/ q" b: J0 n1 w# @
return 0;7 U& b# B/ Q% ^5 j j/ _' F
}6 t+ e4 v6 l- [" {2 d) ^
string* strRelations=new string[nRelNum];0 e% [2 K5 K4 ?" u* g x
char* Seq=new char[nSeqLen+1];, A2 _3 I \6 S8 ~
bool** array=new bool*[nSeqLen];9 F$ i2 E; i# H- Q: r. }
% f9 x9 y" k% C7 d) j9 ^ for(int i=0;i<nRelNum;i++)$ T3 t* ^ ]) ~% ^) q* h
{
4 K+ M1 V. g" i6 a0 r cout<<"Input the "<<i+1<<"th relation:"<<endl; X" b* M& r! J. T
cin>>strRelations; n7 O' s, s0 ~7 \9 T" t# H
}& f. H6 W$ i' ^& E4 b7 f1 F" h
$ ?% O1 S" a& x* v6 z
for (int i=0;i<nSeqLen;i++): T" u1 d) q( ^6 e! f
{ S: t+ k( ?: U" N+ J' f" J0 t* d
array=new bool[nSeqLen];; p( A$ J+ N# q) C
for (int j=0;j<nSeqLen;j++)
6 o, ?& J" h' S* @6 _ z- T array[j]=false;" n8 Z* s# i+ a( t% ^3 x: m9 x- M
}4 }; N$ o5 U1 M, O5 G
//The main loop
, r+ j3 E, r$ P" v/ Q0 V( W. e% i for (int i=0;i<nRelNum;i++)
, z+ u. X8 i. u {
( v/ \. r! n+ b2 b# } [ char a=strRelations[0];
9 `3 J u! _. J; { char b=strRelations[2];* E8 m6 U! C! i. }4 \7 L- n
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);. f5 V3 ?; k: C* a
array[a-'a'][b-'a']=true;( u$ d- @6 t" @7 i9 P
* q W5 _# k) X. C1 G6 U
Marshall(array,nSeqLen);
. }% Y3 n+ a' a+ X- @ y; K, Q8 M% d: N& \( o9 B
//Check for Inconsistency after every relation2 P' q2 T* p* r1 A
for (int m=0;m<nSeqLen;m++)
5 j6 z/ U9 u$ ^! C {, l( V0 a Z/ ?
if(true==array[m][m])9 }: `9 i# }/ f. a+ u
{
' {) ~ [' n8 i% r- o cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
0 ^( n: P) f5 O% M' M7 O: f delete []strRelations;
. z: D: K9 M. C4 l for(int k=0;k<nSeqLen;k++)9 ]0 w% D N1 X( H$ ~ {5 y& T
delete []array[k];
# a/ A5 R! T. u* s delete []array;
v$ g) g) U4 m! o# i delete []Seq;9 N `( x1 g/ z. P6 D
return 0;
: f2 {) j0 D; h5 `- }( C+ i0 v$ O( y/ @; V) I1 ~
}
: J1 U4 K0 E- p; \ }
% _ m8 z# X+ q8 u* `& h5 M
5 G/ @ A; d, g //Check for the determined sequence after every relation ; d8 T( m: P s% V2 q5 a
for (int j=0;j<nSeqLen;j++)0 p" Z1 X: c' ]$ y
{& h1 s4 E, t0 H8 h7 f$ b
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
2 {" Y! O2 J& l- i8 u( v. z. n {
2 v, a& j! {* T cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
2 ]3 i1 S/ N( P delete []strRelations;
* n7 h0 r! t+ @" \7 O for(int m=0;m<nSeqLen;m++)9 m! P# `, ~' N# a- Y+ h! C
delete []array[m];7 L0 r. r- H- N. F- [6 e. O8 B/ z
delete []array;
* d0 A9 ^+ _2 \9 ~ o" f& L delete []Seq;1 C, u7 h. j# W6 m% T, h0 n
return 0;9 ^- n+ d) }+ O) @
}2 Q0 w6 {2 `$ C# F$ x
}
F5 N8 H2 o% w7 k- ?* {2 n8 b
- _- |8 t% S1 x i4 K1 v
7 t" H* l: _) O! d, o% Z+ @ }
5 `6 t8 K; M, d# T# u0 A( @ //If the program has come to here ,then the relationship hasn't been determined!!!% v, u( M, J" w1 x+ f
cout<<"The relation can not be determined!"<<endl;
& M. a* V8 f3 F5 c, B delete []strRelations;
6 |! D8 _* P, j for(int m=0;m<nSeqLen;m++)
( O1 `, `$ L. @3 ] delete []array[m];% }$ z6 M0 y& G$ C' w8 T! D
delete []array;
$ R7 W! u2 l! w7 Z2 M! y5 i' n delete []Seq; G' [ X$ u" V! y
; B3 e3 s% a, J: m' h! P8 W9 C1 K6 a
return 0;( R2 C5 T- ^- v- q1 L r% M% d
} C1 U9 d' h6 H- Z9 m- F: A
( [& j" ]0 ` N& i+ y0 C程序解题二:#include"stdio.h"
, F9 ] N9 f. rvoid main() [* k4 a5 ^6 _7 H, M' A# d
{
- K( U% f3 V- X6 J int n_m[100][2];
6 X: ?' d- r, G' ^9 B7 J5 h char re[1000][3],
' i$ l* @) H0 r: A$ u temp[26];
; x8 f3 O, j! L: { int i=0,j=0;
/ I5 o: X' ^* \4 r scanf("%d%d",&n_m[0],&n_m[1]);
% d0 @3 E8 R8 `7 V8 T+ g$ V- a for( ;j<n_m[1];j++)
3 L. n" h5 t" I/ n( m& p scanf("%s",&re[j]);
2 O9 A8 j) [ ?" e0 T while(n_m[0]!=0&&n_m[1]!=0)
N, D, j/ ~/ a. m) ` {3 I6 p4 b. A# c, i
i++;! x0 F8 Q8 H3 x2 Y
scanf("%d%d",&n_m[0],&n_m[1]);
7 D' V" C; I. q3 R8 d; H6 R" `2 @' X for( int f=0;f<n_m[1];f++,j++)/ H; U2 T! d p& R& ?
scanf("%s",&re[j]);( ~: ~ P& O5 a! v; b
}# ^/ m7 }+ v# y3 [
i=0;+ e; l- N% b* c
j=0;" N8 ~& n3 t+ R* R' t; h9 ]
for( ;n_m[0]!=0&&n_m[1]!=0;i++)* A, F; L1 T! f) ?& f ^% I
{
- n# x4 ~" @6 \3 Y. H. k int a=0,b=0,l=1; ~5 b& `& y. X3 F
for(a=j;a<j+n_m[1]-1;a++)( R8 k) C1 Q3 Z; E& C+ I
for(b=a+1;b<j+n_m[1];b++)' H" G$ S0 ~5 z j! {: s( w& h# [8 Q
{2 V, W0 E9 L. J n8 M
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])
: Y5 o5 ]2 a- }* |) Q! ] {
- C1 \: G n6 W/ B* j l=0;# w& x. o( ]$ M3 j1 `
printf("Inconsistency found after %d relations.\n",n_m[1]);
5 V: }. T: u* I3 _6 K8 { break;6 l! l$ ~- E2 b9 e9 e, U
}7 `' h9 `. V. i2 t; p* x7 x! d
}% J) ?2 h7 u6 d( `0 j
if(l==0) - F, {5 S4 r1 @5 [" z
continue;//Inconsistency found after x relations.
+ p# u- J% t" [) A" z' H else{9 q- ^. X8 D: d: y; j5 g1 Z+ h
if(n_m[1]<n_m[0]-1)
4 ^1 X# |9 G4 R. e6 R D1 w {
( N5 C/ H; P/ d D* {( |3 V printf("Sorted sequence cannot be determined.\n");
! s0 f j6 z$ o1 M# l ~* G l=0;0 h5 K7 U" Z* i- Y
}
& a. `$ a, R) J5 b e: B& t3 Y else
0 K& N4 s1 ?1 o* Q# X6 L: I( W { f7 r' n) G4 P2 E" u6 C9 A1 p
if(n_m[1]==n_m[0]-1)
# _4 z0 L+ U6 ~. q* e3 ~: R5 S5 O {
! b. L; K* i, H* T& b2 q1 \ int k=0,p=0; i3 W" `/ {* G4 Y/ J9 {
for( ;k<n_m[1]-1;k++)" O& J/ D3 _2 Z2 M
for(p=k+1;p<n_m[1];p++)9 c4 o6 t9 W/ ~' q5 i4 ?
if(re[0]==re[0]||re[2]==re[2])( _! j4 M7 k" X) J) K( T
{
& M, p# F2 a6 N0 k- d, E& ~' {: w printf("Sorted sequence cannot be determined.\n");; T T; N4 U8 Q9 a
break;
" n8 Z- a* r/ s5 f4 R6 d- j l=0; Q2 p; \6 z) Q+ V7 p
}
) F7 Y, b4 D" N% M }7 @( x+ h/ {6 F2 ?- [5 ~! `
}
5 h5 d( b3 ]; b if(l==0) % ]) Z8 s( n% {2 U
continue;//Sorted sequence cannot be determined.2 x q1 o: o2 J, U$ i; `9 p
8 R- H0 ? J7 A/ f: Z else5 `1 t# w: F& C0 |) ~
{8 F' T B6 \9 X
8 A/ K7 r4 }7 ^
for(int k=0;k<n_m[0];k++)
1 G7 y, X# {+ a+ h9 n/ t* [. \ temp[k]=k+65;
- W% [1 P6 P% l+ F0 Y0 F$ \% B for(k=0;k<n_m[1];k++,j++)7 u6 r# q5 M6 R& S0 L; w
{9 \8 I! ?0 x- G8 i: X$ p% Y$ Q
int t1=0,t2=0;
; y0 D% d0 ~! o1 r. G7 \4 S for(int s=0;s<n_m[0];s++)3 i$ d) Y# s+ ~( B7 E
{
" f) j, i" n. y. l8 I) ?9 j if(temp==re[0])
$ U, t8 Y$ I+ p5 r* O3 s t1=s;
/ {% Q# x0 q+ R: z$ J- c if(temp==re[2])
+ z7 }' {: C K) D0 Z t2=s;
2 @% o3 y' w( B4 l& f }3 r( C/ Q9 [' L5 Q
if((t1>t2)&&re[1]=='<')
. y& q) ]- v7 ]2 H {, j' m2 e7 H$ n+ t0 t4 K
char t3=temp[t1];- v; _ c& s' O. S
temp[t1]=temp[t2];$ N4 x% C/ ?0 S+ v8 r
temp[t2]=t3;3 [0 O6 G% Q7 }7 W# m
}
, `% T8 X5 s3 C7 R5 v9 S+ b }
2 _0 f% e# Q1 Q H8 U- M int count=0;
# C' e/ r& E' \) } for(int s=0;s<n_m[0]-1;s++)( A; e9 C, q! j0 ~* F3 S: U9 ^
for(int d=j-n_m[1];d<j;d++)0 ^4 e! X& B/ z' J
if(re[d][0]==temp&&re[d][2]==temp[s+1])( ?( {0 A! o7 W, v: J. a6 X7 F' p
{- x1 n6 B; N$ N! J/ u- n
count++;
- y8 ^9 L! \) E9 h& Q& j6 f break;( m- ~1 [: ~. K$ R
}
' h+ Z2 D6 {& Y+ C+ C if(count==n_m[0]-1)5 y/ g3 f0 w0 r6 G
{2 g+ c2 {( E6 R# R2 }
printf("Sorted sequence determined after %d relations:",n_m[1]);; b6 o% s7 y0 Y" B4 m
for(int f=0;f<n_m[0];f++)
, u2 G# R/ e% F& T/ x printf("%c",temp[f]);9 c, t. c/ X7 ^/ w+ q4 r# ^
printf("\n");
& E& c6 y) e/ s: i }
7 y# c9 `! L) W+ n8 }1 s6 Z$ L else
. o5 y1 ^; d, t' ]7 j printf("Sorted sequence cannot be determined.\n");
0 V7 P1 N1 g" ~3 ], | }5 G8 K% y) |# D* s
}
" i# V8 \" a* S0 a2 h' @4 y }
- B0 }7 [2 W4 n ^9 Q}! \$ S, i. v( P% v4 D, d0 ~
1 w6 @7 T1 X( `( }+ u6 u ], E
C' q( J9 [- o
# B) c$ x* }& R+ {( _7 `
4 h$ Z' T& ~1 q/ H
9 U1 p+ f, B1 m4 @
+ T6 l, g) \9 q: l. O2 X
* \/ q$ Y) u" n% m: S# x7 q w
6 |; I$ j9 L% z( x+ A# |来源:编程爱好者acm题库 |