本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
! l* a1 _0 e X$ v5 _- a3 U4 \4 s. R% @2 F
Sorting It All OutDescription
! O' Q2 V% s9 j# M7 @6 Y* m& K4 ]: k9 K; ^8 B# W* W
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.
5 d( U$ y3 D7 W+ h3 d- O7 _Input
0 R; _3 S6 _1 k& B" w1 [7 ~2 T9 J& ?6 s/ a# h
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.
! m3 q- ?' ~" ]3 m: p2 i, ^Output7 ~+ G6 O; C5 e6 f" j
9 g$ e4 C4 O6 }# `5 a9 l9 h
For each problem instance, output consists of one line. This line should be one of the following three: 3 J% v0 O# V) c* S) Y3 b9 G' k
- O) L$ q' C: z' P7 F- n3 zSorted sequence determined after ** relations: yyy...y.
" F& [- `( ^; h+ R! T9 @Sorted sequence cannot be determined. ( Y; o: f# G4 k3 y
Inconsistency found after ** relations. 3 `1 \5 h% k3 Y$ q& u/ N) x9 D) n0 c
/ n; c2 B4 @, \+ y2 kwhere ** 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.
- Z$ z5 G R1 ^# C
8 x' T5 G& j8 `( n( H" q, l输入样例
1 }- P; h2 a( _* f. W. k- @4 6
! ^9 Z) S6 F8 f2 lA<B
' k% k) g( P- ]; }, tA<C0 Y% Z, \6 N# \4 u, A
B<C+ D/ [# X7 g- M( M# Q
C<D
# i' n; T# \- `- M6 c) N+ V4 @+ lB<D! v" Q. v% ]9 E# p7 ^0 U
A<B+ C4 u( L P) X, f @
3 2
, I. U/ j1 |6 B. c5 h3 FA<B. P. p3 t, h2 `7 x
B<A" V, ?# [$ [7 m
26 1
: y3 Q. w: z6 S$ rA<Z
I u- O" `$ l0 @, m6 U7 X0 0+ ~+ K5 ]! r1 O: o4 s$ X
% M4 D' C; Z0 P5 [/ v6 Y9 l
输出样例 4 _" Y' |* Q' }' v
Sorted sequence determined after 4 relations: ABCD.0 d4 }$ k) _9 r1 J
Inconsistency found after 2 relations.. z; A( q2 M+ Y* e: g
Sorted sequence cannot be determined.
2 z2 l/ k4 ` B& D% wSource' c* x2 x2 f H9 b
7 O5 p0 g5 d B& d4 T" m
East Central North America 2001
; {: f' x- ?/ H' }& E% {4 } Q. ]程序解题1: 4 Y1 j1 _8 b) h! b: ]( e6 P' b
//By Jackie Cheung) L* ?- }$ u* ~% k+ v
#include <iostream>
. B' V+ o$ w, O6 Q! ^#include <string>! l5 m r" R6 u6 b; i
#include <cassert>
- {* Q( B+ f0 f# m0 D1 H$ ?( D4 ~. Mtypedef struct tagNODE " W& u, |" ~ s! A6 V! q8 x# p
{* _/ ? `( L# x8 n4 `
char val;8 M: C% i/ t- Z% e
struct tagNODE* link;
% R' @" }$ s: w" V, J}NODE;
7 i5 M9 Y6 U% pusing namespace std;
# I z8 N0 c3 \4 `9 V& Y) Zvoid Marshall(bool** arrary,int n)- }* W2 w* O- R% O; N( I8 q i
{
6 b$ {' \# m5 X4 A4 e4 I# X for(int i=0;i<n;i++)5 D2 T8 v# F: {" h5 D: S, U3 c
{% P' Q) p/ |- x' j+ P% A4 g
for (int j=0;j<n;j++)
+ _- [6 l/ [# Z5 T3 l7 d# D, T {
9 l( I) t/ f0 o# K if(true==arrary[j])
, v- B" G+ y! n* \9 X$ A g for (int k=0;k<n;k++)* N3 P6 u+ a( ^! o; a& ~" A
{
9 e; e- U7 ^8 c6 e arrary[j][k]=arrary[j][k] || arrary[k];2 Y* G- |- A: U
}. G! |3 q" U6 J8 D
} W8 u" H) @" S3 i$ L
}
9 \' ~4 V$ ^( Q};
( R9 u- J: u$ _; E- Ebool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n); m( F' \- [; j, v; t. d* a
{
W% \# S, }" G3 z( d Seq[n-nLeft]=static_cast<char>('a'+nIndex);
" k: o. V9 f" ] a' V bool bFlag=false;% ]/ ^5 X" x5 Z _9 T
if(1==nLeft)* X, i# N/ |" w- n8 w
{( R+ e }0 D4 D# D( Q
Seq[n]='\0';
- ^ Y" H) b/ @. u: w6 q return true;3 k, ~+ O# ?* q; H# E3 U/ f! m; |
}& k% o8 v. |2 e
for (int i=0;i<n;i++)
) U. X* C2 m; z" b {
, Q5 W+ u/ ]# P+ l0 U/ J if(true==array[nIndex])# c: F m) U: A
{3 w: S- [! G' a; C
5 S" B9 `! O, H) d5 y, Q
bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);; S3 I# T% t- j9 E
}& Q4 r$ b# ` S' y
if(true==bFlag)
5 {' ]- t2 m7 k3 Q0 q$ F& N, Z$ ] return true;
4 [) c. w6 U& ]% _5 k, O }6 m! B, P) j# A* }
return false;4 w3 F) j+ i3 I% E2 E D. b
};
2 Z" Z" H) t6 k' u4 h, yint main()
7 U( b2 s; }. c3 x/ d9 M9 Y3 @{- r" D! _5 E, {7 R
int nSeqLen;
: A1 a0 S1 T& r1 Z3 z. Y int nRelNum;
8 F: l0 X+ G ]* K/ c4 }* |7 P cout<<"Input the length of the sequence:"<<endl;
$ o# p7 f) p$ z& B" U8 O$ S cin>>nSeqLen;
" ?7 ]- C9 H" Y* I& p" s j cout<<"Input the number of relations between the elements:"<<endl;2 W1 e9 \$ c4 O7 o9 U
cin>>nRelNum;
8 E3 y2 b4 ^& H% [+ H9 f) t6 c) w //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
) z* m$ r- R$ x if(nRelNum<nSeqLen-1)( I/ G$ f5 z a+ E9 K' h
{% z0 F6 y! y( y5 J7 ]6 E
cout<<"The relation can not be determined!"<<endl;
, P) t2 u& q& [3 c return 0;
' ^1 X7 k0 m7 z& ^1 t: T, \ }$ t# }" ~: k" n+ G% ] R3 }
string* strRelations=new string[nRelNum];7 T- |- h# V i$ f$ t# w0 G
char* Seq=new char[nSeqLen+1];/ x5 ~. S6 R1 n% O2 r7 ^
bool** array=new bool*[nSeqLen];
4 G# h/ |& I" E! e7 o
" i0 \2 @" n8 ]6 z& K M9 m9 q# S for(int i=0;i<nRelNum;i++)
9 Y8 X9 [4 t. V! L7 x2 ~ {
, K, x. p1 Z/ r8 N7 [5 o cout<<"Input the "<<i+1<<"th relation:"<<endl;
: O y6 k+ X0 f' _& W( C5 W# H cin>>strRelations;
( [7 [/ I- ?- d8 q! u }# O @* _ V* O, L+ L/ L$ k# E
- R8 U( H/ R7 M- y- X% l
for (int i=0;i<nSeqLen;i++)
1 C3 r4 p, \5 n2 i& `: Q: a0 x {; s5 L! ~9 \3 m' k* O$ ^$ `/ C, m7 A
array=new bool[nSeqLen];
1 P- G6 E# Z3 G7 z for (int j=0;j<nSeqLen;j++)7 S+ k0 v$ }1 e3 d1 k
array[j]=false;
. `4 S) w5 H. T }$ } i8 }6 _6 Y6 M: E2 f
//The main loop# w: `/ S8 C! `3 }1 l% n
for (int i=0;i<nRelNum;i++)
, P9 Q7 \2 M) q7 Y1 y( H- H {, L6 ^0 ?# u% {7 I6 X6 g# T% H
char a=strRelations[0];* A$ B: Q9 }4 Y! f' c- G" @/ g
char b=strRelations[2];
& k! p. q" W/ g) I' s assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
+ q- K- `9 @+ t6 H" B) O! \3 | ` array[a-'a'][b-'a']=true;2 `- R# g7 f7 B# ^
" [3 x+ \4 [ u+ g Marshall(array,nSeqLen);
: X. q9 B/ }, K
+ B" }! p& n. Q9 I //Check for Inconsistency after every relation2 a$ o8 y' |6 b/ j
for (int m=0;m<nSeqLen;m++) L' l2 ` b3 l& M* g
{7 J2 J* [0 q( @" t& u
if(true==array[m][m])4 C% r( U* d B
{: H$ Y0 y8 C. y+ r) @
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
/ _5 L0 J* ? J4 g delete []strRelations;( X) ~2 S, T" v; g; d# E: C& n
for(int k=0;k<nSeqLen;k++)
1 T1 o* D# w/ V8 M4 N delete []array[k];
! S8 r; ?8 P3 x delete []array;" M8 _5 l) }) ~: F
delete []Seq;
2 [# t% q3 U" r! J% {& T# T& N! Z return 0; c: }/ P8 {3 I& k6 H+ R6 d
; `( g" U' L o5 W0 i }
+ P+ h# x1 a8 y* l3 { }! V$ i) l0 t' b+ l, A i/ y. W% A/ I3 j
; e# N- N2 i; L" l //Check for the determined sequence after every relation
+ b6 K% ?( m$ o, u8 l& f for (int j=0;j<nSeqLen;j++)
0 Z) }: @+ I7 ?# m6 [ {. o: r/ Y) U z9 ?
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen)); f, `) O; z* H4 N6 X! j) n* ]. P
{6 g5 }* h% n6 S4 ?' H1 d
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;, J+ Y" k" m7 ^ \" G7 \
delete []strRelations;
9 f- ~" H& _5 @, P' { for(int m=0;m<nSeqLen;m++)2 w! ?/ [$ X) T+ l) O4 w# f; ]
delete []array[m];6 v j6 G1 ?% z5 T) J
delete []array;8 y8 H* X" v+ o
delete []Seq;4 ^% E v: l" j8 T" s; T( d4 d: ^
return 0;
5 V( d% m9 T: K: N/ A: A3 ?" O }- [0 _" n6 i) r
}/ v/ u7 b- G. k
# t; R$ L, g# h/ c# b
. @8 G! B4 `4 C; `8 G }* a' M7 Z4 Q. z* q3 l: J4 Z
//If the program has come to here ,then the relationship hasn't been determined!!!! k& w' G) O9 z, a
cout<<"The relation can not be determined!"<<endl;+ `+ ?: ~0 L7 I( V p- }8 V
delete []strRelations;( x5 X% M, E5 B& P: v( r
for(int m=0;m<nSeqLen;m++)
8 l& b- E9 R0 B7 O1 y7 T+ h, b delete []array[m];
4 E; g! ~# T1 j( n% A* w* D5 J delete []array;
+ `! E% T0 d2 G delete []Seq;0 ^+ [" @ |6 y/ D# w5 l: U+ Z
; \9 C# f: V \0 M1 o4 K return 0;
& C& e- N1 b. r, T; r$ h5 h7 Q0 D}
" v) M! h# \" Q5 E; V4 E2 X- t# d' J2 I$ r
程序解题二:#include"stdio.h"
: O4 N* S" k$ O. a8 pvoid main()
8 r) E; g# x( I F{! f) |" A7 [' d
int n_m[100][2];
9 |8 }: U3 V; v$ V' g5 K- _8 I$ k* n char re[1000][3],
! z. r7 W) L* F- M6 w) x f( ^ temp[26];
3 u) ?0 M9 B6 U+ {8 R int i=0,j=0;
$ G& \& Z: s, c- W scanf("%d%d",&n_m[0],&n_m[1]);
; o( ^) P, H9 t" K+ c3 [# t for( ;j<n_m[1];j++)
: }+ d& {. r# y: i/ y) b; ` scanf("%s",&re[j]);% [2 c" A" I4 d( G# L" `& b
while(n_m[0]!=0&&n_m[1]!=0)& s- d+ J7 k. m
{
" c- f! K0 h+ x( Q' F. t( p i++;
$ l/ D! w) b) B# Z scanf("%d%d",&n_m[0],&n_m[1]);; }3 i d$ Y2 y
for( int f=0;f<n_m[1];f++,j++)- Z' Z' g# x5 t. B. X1 K6 p5 P
scanf("%s",&re[j]);$ A! _& D9 Y0 @
}
/ }/ [' Y8 t5 E9 {# R i=0;
- f% }* u% q$ ~$ c" X- q" h q& a j=0;
' d8 g' \7 M# O. C for( ;n_m[0]!=0&&n_m[1]!=0;i++)
& ~: i7 V9 s3 o4 n5 H; u3 R% l5 t {; ~7 L# C/ i# V$ Q
int a=0,b=0,l=1;
% |: ?3 w3 S# L1 c for(a=j;a<j+n_m[1]-1;a++)
& o; D5 J+ K+ ~; O5 E# N8 [3 N for(b=a+1;b<j+n_m[1];b++)
J ]- k; { H4 [, E8 | {4 e2 s% \0 |) i* c: K' q
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]) ~6 j/ P+ L1 G. @! L
{& n) T; e' N7 `
l=0;
_* f7 Y. _$ X. f0 k printf("Inconsistency found after %d relations.\n",n_m[1]); r- k" z1 m' {# @( L! e
break;# a: W6 O" g9 a) ?3 |' N# T
}
' X) j4 Y+ }' \3 F1 @- Q3 Q }$ c& b1 x. }" n4 N% |+ D
if(l==0)
: f$ B) B" E! i w' `! N continue;//Inconsistency found after x relations.
# Z7 Y% \6 I5 d7 a* j. i8 I else{7 V$ L9 t7 ]9 A; u! u: L9 B
if(n_m[1]<n_m[0]-1)
! P1 S; M8 y/ X# Y- R( ` {6 O' j9 b) w* J" h* E
printf("Sorted sequence cannot be determined.\n");9 W) B+ Y% D0 V6 M
l=0;
# w% U2 u) z" `* I; e: Y4 ~4 T }
' p# m0 R3 J2 T- q, W0 I else
6 Q. h6 P5 W( F# k9 \% K {
d+ r8 E7 V: s) l+ c7 E6 J, E# B Y* i if(n_m[1]==n_m[0]-1)
" j9 \6 p) u8 r' ~/ U { 9 J7 I0 d# P' I! r, l
int k=0,p=0;, j. H% \9 x3 o$ l3 e; Z7 L
for( ;k<n_m[1]-1;k++)
/ V3 W2 R( V9 _! O6 U- m( E8 Z' V5 e for(p=k+1;p<n_m[1];p++)) h: x: Q" V J6 i y& x
if(re[0]==re[0]||re[2]==re[2])3 e6 i, G& [. D( ~. K& J7 |
{0 U P: ]' B+ i! Q4 N
printf("Sorted sequence cannot be determined.\n");
6 y/ g9 P1 r7 d; F0 I break;
# H# d$ {3 I$ |9 M l=0;
- S) h# C6 s/ |) f8 ? }
$ I4 \1 P+ ?& [( V1 u2 M/ w- \8 Q! V }, }& s1 a6 A0 E6 q
}
/ g" w2 o* G# @# f4 y if(l==0)
. [, F% Z5 y: b) } continue;//Sorted sequence cannot be determined.1 j' ~# M9 R: H) f' L, j
5 C! C: g- L& R9 ^ else7 w* m0 k. b a
{0 w6 l5 N9 T* ~6 R
$ t" t. ?: ~! z4 n, g for(int k=0;k<n_m[0];k++)
3 o. z: `/ M% i" @ temp[k]=k+65;. U9 z1 J% K9 I P, x. g
for(k=0;k<n_m[1];k++,j++)
0 x- R0 d) c1 h7 o4 C+ W {0 J6 x5 I' F% J3 e9 Y1 u
int t1=0,t2=0;) ?8 }9 W( s* m8 b, q6 w2 A
for(int s=0;s<n_m[0];s++)5 T# k& g. `3 u/ f
{" N; K8 o1 |' N& B5 x7 R+ ~
if(temp==re[0])% s9 E% d* I2 V$ K4 X' {+ w7 D* c% h
t1=s;
2 U# h) e( ~- j) h8 M) M* ? if(temp==re[2])0 p5 `0 d# c4 C+ a9 f0 m! _
t2=s;7 v9 _5 G s% J; C5 S! F+ [: N8 k
}$ U3 K6 p. T# o9 b: m4 [
if((t1>t2)&&re[1]=='<')8 B* r1 q& l3 B7 x/ [' }9 B4 V
{. D' L3 [& C9 \2 R; W7 g
char t3=temp[t1];
# c: Z- C+ t+ d) e. U temp[t1]=temp[t2];
2 L" S1 Z' P, _7 f+ L1 M3 y! _ temp[t2]=t3;
/ ~0 S& k4 D" V! S$ b }- u+ H7 _4 ]! I u4 [6 K
}3 W- H# G, a( U3 u0 d3 b6 p0 ]
int count=0;
; v2 z0 M) a. ]% a8 G for(int s=0;s<n_m[0]-1;s++)
' y! P( w+ A% Z6 N8 Z+ h; j for(int d=j-n_m[1];d<j;d++)' O9 Z+ @4 l# b* o* Z& ]
if(re[d][0]==temp&&re[d][2]==temp[s+1])0 x' c* ~* Q6 b& M0 e* p$ Y
{
! i6 W/ a# J) B1 y count++;- z: }1 i# M6 G+ \
break;
. h; C4 V& Z0 b" @& H# Q% M }+ \0 ~9 L) B3 N" `; [2 _( u1 i8 H# }! F
if(count==n_m[0]-1)' n0 X: N9 d0 H+ u- q; d; v
{' C0 }& F$ d' [
printf("Sorted sequence determined after %d relations:",n_m[1]);" x. Y* c4 g+ i) y& V4 w& _
for(int f=0;f<n_m[0];f++)+ T; k9 C& P" C7 J5 F# R
printf("%c",temp[f]);# d9 ^* t3 a) ]( n. ? a3 F9 o
printf("\n");( p% I& Z: y7 W8 X7 E3 G
}
' i/ r6 [& y7 ]! x8 m, }+ x6 G- _ else( G3 b; M/ ^0 }8 p; \2 A% S9 `$ N
printf("Sorted sequence cannot be determined.\n"); , ^! e& h) p. G
}
) d8 k) }# N X1 e6 T }+ u7 C, j0 h3 y0 e$ B' Y" q& \
}# p) Y8 F$ Q. x# T2 y! K
} G8 x* K$ A( Y& U* v
4 |6 d& W+ [5 C: P( M: L
7 g" h7 ?# R6 v* k5 a- m7 ~. e: H# }* G
/ P; p+ r. w* K2 _; @( ~/ L. J8 p+ u7 y
6 }6 N) d' S Q
( y. d9 k4 N- ^0 ]% m H9 ?; f- V
v7 t$ R/ Z+ ]
4 _' d% ]$ G! H来源:编程爱好者acm题库 |