本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 ) v4 Z4 f* g8 b" U4 c! ]% k( f$ S( r2 s
5 M7 @4 S* \, mSorting It All OutDescription
0 p6 c9 B, `4 h. ~* \! F! g6 }
/ E2 t, v/ A, X/ y. X0 eAn 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. ( u: Z$ u4 [& H, N
Input9 a6 \4 W7 ~1 w8 J8 m6 @) ^
5 ~7 H8 w& y7 O. I" w8 _8 d( q9 ~) kInput 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.
, h5 a0 \* v t3 A5 s# tOutput2 T6 z) q r. ]# M+ ]( [
, C* v A" m' N w4 Q& A/ s: EFor each problem instance, output consists of one line. This line should be one of the following three: ' ]7 T; v% X! _+ }/ X v9 T
+ _% } U) E7 C, f
Sorted sequence determined after ** relations: yyy...y. 1 Z! @ x; \+ N0 D
Sorted sequence cannot be determined.
' R( e+ x& W* F# n$ m7 Q: ?( yInconsistency found after ** relations. ! S2 s H" I, _0 ^' h
, g0 A* e9 d5 p1 ` e1 _ h
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.
/ ?: s6 O/ ~7 c! O( p2 t
) E- v7 }' Z! g9 M$ y9 s$ G( t7 R0 U输入样例
+ m* ]) Q8 o6 D8 j% o; U' g4 6
9 v; ^7 ^" ]6 H5 s+ d7 M4 H& iA<B4 p0 q5 k% [% ?- z9 E
A<C4 _$ f l% m9 p& W+ O
B<C7 ~4 d( Y& } C* ^$ C0 o. q4 o
C<D- [0 \+ g6 C% S* [; |
B<D" s, w$ H& ^* P( u8 P4 H1 j W8 O
A<B
; c' z# f6 K- }7 t/ J1 S3 2/ G L" G4 F! V0 @: y$ T
A<B; f9 s2 [4 W$ i9 f, k
B<A
4 a- C8 L- d8 G% G4 q, E+ V8 U26 12 T( @/ D) W7 z8 l* N5 r& ^* _
A<Z
9 I7 @. z- K; {0 01 S* v _3 [/ ~1 S; h( n" m$ e
/ s$ e4 M; Y* }输出样例
" g7 i! q! s& [) {8 lSorted sequence determined after 4 relations: ABCD.6 x* V; K5 B# J; o% q' K
Inconsistency found after 2 relations.
$ F- X' ?2 `- T9 m# y0 lSorted sequence cannot be determined. , Z6 {6 t% M" f$ _1 | O: |
Source! Q( F O/ w0 p7 l
# M' ]+ K9 F+ m! NEast Central North America 2001
* b7 A& ], g/ `" C$ r7 E6 M2 ^程序解题1: 0 N' Y+ D) b% j9 _8 c; [# ^$ d
//By Jackie Cheung
, ?% t* |9 i% c* N% O4 r#include <iostream>
5 s" d) l; `; d/ |+ c1 }+ s5 f#include <string>
8 b! J) p( B9 m$ ^) I4 W& p& d3 j#include <cassert>
2 X! X: v8 y* l( \4 d; Ftypedef struct tagNODE
& q. m) E. e! ?3 L" H, W8 @3 ?{' f( M5 b; @8 o4 e5 N
char val;8 B, }1 U9 s& A, s4 ]: l$ h
struct tagNODE* link;$ r2 K) M4 k4 o4 u8 e& H
}NODE;
' `/ ?7 E8 M& n' H+ v) v6 Yusing namespace std;/ y) D4 ]# o; X7 }1 r1 `" n
void Marshall(bool** arrary,int n)
& F5 w1 ~+ B; [, O{7 p% L7 x2 w K& h6 k# m! c9 F
for(int i=0;i<n;i++)$ x/ A5 K/ K. _, d2 R% T
{
+ a9 Q6 j+ o' N1 x9 D( ? d9 S, [ for (int j=0;j<n;j++)
# \! J. z; f6 x) F, W% @* Y$ ] {
( ` f" x, Z0 Q$ x6 | if(true==arrary[j])
- g2 i( r ?7 f. K for (int k=0;k<n;k++)
; Z$ t% O% v3 }3 S' v6 C/ L3 R( | {
& T2 c' o! P6 f, W arrary[j][k]=arrary[j][k] || arrary[k];% A6 ?! z/ H7 |: m
} U/ {0 @: h G- m5 B
}8 v( _# K2 f: J* F( t! O5 O8 d$ g7 i
}
4 y. u& X' b! Q9 S% q+ y};
, L, i; L' y3 U3 m8 hbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
0 t* n5 |. s' p) Q. L6 v{
) \/ B3 X7 d9 @8 p3 z- d0 c Seq[n-nLeft]=static_cast<char>('a'+nIndex);
* ^: X7 k! s* L3 F, T bool bFlag=false;; ]7 E( N- n! h9 H
if(1==nLeft)' E8 h" j/ V/ J, o. {0 B
{ E. o5 q; M( S0 [% N% v
Seq[n]='\0';
8 M: ]2 r7 O: d return true;
% \4 p3 _+ O, M }" s# H' g1 W; O2 ?! Y& E7 P/ J
for (int i=0;i<n;i++)
" Z* s7 r! R& H7 o9 | {
/ t9 b. [8 w' T8 B1 c8 L' { if(true==array[nIndex])
/ o* M+ Y7 W) g( `2 e- V1 s {
& k! F) D; s% ]- t) ]
2 e" m1 W) H% ] bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);% Y( f% c* p. L i2 R2 s3 {! `
}
( B" z% ?) T' o( \. d if(true==bFlag)
' Y! u# B3 Z& M1 f return true;# W: W3 i+ D0 C2 e6 F9 V3 {
}
& y$ h6 S. |3 w+ r) R return false;
: t, N3 \& ]' e) H/ a! x) Q* W};. R" U. Y& ], F5 G3 e4 Q
int main()
4 Y- R; ]$ C+ R{
, W8 {& s$ Y9 n0 L int nSeqLen;8 P( u$ e+ s0 T) I6 V
int nRelNum;
: Z l8 q% C- [! A3 f( n+ V! _ cout<<"Input the length of the sequence:"<<endl;& U/ m1 L ?' [( w6 ?( C! p
cin>>nSeqLen;4 u e+ F, |: v. t# ? _: }
cout<<"Input the number of relations between the elements:"<<endl;
4 L1 ?8 [, l8 i8 j+ [4 y @ cin>>nRelNum;& @3 {8 D9 P1 Q( A+ @, R! j, M0 d
//1:if nRelNum<nSeqLen-1,then the relation can not be determined!
2 T: H5 b1 P/ W* i% ^ if(nRelNum<nSeqLen-1): s* q0 R0 i/ } j$ E6 |
{
5 G, I5 l, c# h+ K cout<<"The relation can not be determined!"<<endl;* o2 g; i6 j" I; w* O% z. \: ?
return 0;$ O% U4 S! X6 R0 y# t- k1 s, ^5 V, w
}/ d9 a3 b' {+ h1 p" F4 S3 Q
string* strRelations=new string[nRelNum];
* C; W* r$ {3 T* _ char* Seq=new char[nSeqLen+1];2 j7 E& ?* K( k( ^
bool** array=new bool*[nSeqLen];
# v9 }& Y0 s# T+ n, }6 }- G1 q% w6 n4 {1 Y1 i
for(int i=0;i<nRelNum;i++)
, `$ I; T& j7 L2 D" d' a {% N/ W8 K* Z" m/ R0 ^' q2 t
cout<<"Input the "<<i+1<<"th relation:"<<endl; Z% P0 T7 x1 f/ @( R* k
cin>>strRelations;) E& i: x1 z& U% R3 D
}
) e- i- \8 Z% v# }" x: b5 b& R3 D' v7 P " S7 m; ~. o, ~: b# J
for (int i=0;i<nSeqLen;i++)/ S, A- a- u4 j( H
{( c& R5 @- S$ i* q' G6 B% d
array=new bool[nSeqLen];( v ]* ~ h0 d' V& \
for (int j=0;j<nSeqLen;j++)& b4 [/ `1 g T$ n) Y+ Z1 J
array[j]=false;; R4 e* D: y( A; R# Y2 @! Q6 l3 ]+ N- y
}
& ]3 z3 v8 x9 P, ^2 c5 Y+ I //The main loop
& w. g6 C K" k7 Z* u for (int i=0;i<nRelNum;i++)9 @* O% Q+ D3 M
{
6 E3 j* r5 d/ s! ?1 O char a=strRelations[0];
/ o& k% o0 |/ j' P8 S$ u char b=strRelations[2];5 n3 A4 M( ~ ?) P( j: i& Q
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);: ?3 |1 N$ n0 Z5 y8 J
array[a-'a'][b-'a']=true;% u' j; p( P% k5 O1 M- s
' V4 C: J1 I* M8 O Marshall(array,nSeqLen);
z9 b, Y& {; A+ v, ]# S% H. v3 X9 x. r" y2 e9 A/ V, g
//Check for Inconsistency after every relation! M7 }; T9 {3 ^0 Q
for (int m=0;m<nSeqLen;m++)
- U: h" ]* b' u {7 @' i: D a2 t# T, X& f; K3 z
if(true==array[m][m])( @/ P5 h/ z0 X9 o, t
{9 d- L( \) q- b* v
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
1 H0 I2 U# w9 _4 `* m! P/ k6 J. k$ G" ` delete []strRelations;$ }2 ?" x; s8 {0 G1 u
for(int k=0;k<nSeqLen;k++)
& N7 ]+ U$ ?7 s: a- L delete []array[k]; F7 T# {; X9 M/ Q9 X5 k D
delete []array;
4 ^( U" b6 v3 g3 x; K* S delete []Seq;2 o6 J) k( E4 z [
return 0;8 R% y5 H' c8 N5 L1 B2 q( S7 F! `* j
4 l4 r4 p e0 m$ R }
' b ~+ {, W) S" P; V' Y) p }8 H' y8 T7 G5 T# L) i, u9 Z# P
, _7 k, I/ { L) Z
//Check for the determined sequence after every relation
6 R5 U% K4 \7 d" S* P for (int j=0;j<nSeqLen;j++)
. ?3 y* N6 P! a, J& i2 a' b# t* } {7 P1 Y. a% w# e, L7 `6 s* w
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
0 u7 v% o2 A0 Q' Z0 _* l/ ? {1 m5 _/ m) c+ Q+ R
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;0 U- p9 r. k; Y! J
delete []strRelations;
/ C1 Z$ t/ u9 w- m) E1 i; C for(int m=0;m<nSeqLen;m++)
2 Z) e2 P+ s4 \ delete []array[m];
, P# r; _$ {6 s B delete []array;1 c- N5 h# Y9 Q z. e7 ]/ Z8 I
delete []Seq;" f( C/ {1 t, X ?& {
return 0;" n, @6 \2 o* f) r
}+ |# m' ^( H% y% r2 }5 L4 p) ]
}
- E5 z Z+ F1 O9 T8 p) g0 u
: `5 @ u% k% N4 w. j& ~6 }& H7 ]( F8 k% x0 u! j5 J
}4 G3 j% S: @( f2 [" K
//If the program has come to here ,then the relationship hasn't been determined!!!6 s) J" _: P! Q7 M7 y, T& j
cout<<"The relation can not be determined!"<<endl;
8 r0 H( c7 P( |# `% ~8 H delete []strRelations;9 o S1 |4 S# q8 e, ?) c) z! p
for(int m=0;m<nSeqLen;m++)! Q0 H7 X) W& m4 O P2 G
delete []array[m];
6 Z: C, ]; E: }8 Q4 P# w( g$ W7 p delete []array;
( @% \5 J. X) G0 x- p1 a$ G6 U% l& \: \ delete []Seq;$ ~% Y3 Z4 Q3 k' z% C; }
& z1 U: ~9 B F# l( }) u return 0;3 x3 n" J! P$ E: h! s/ L
}8 G6 I6 a3 D* C. d
, F+ g1 R" H' v- q& [2 F8 ?2 n
程序解题二:#include"stdio.h", X& P! o: x! a* h. ?/ I+ @, i+ J
void main()
" O- r9 K- F7 p{
9 l/ o4 c7 M3 o' ]( L# d" j int n_m[100][2];
2 ^" \4 U1 O4 [" H7 @. I8 L1 I char re[1000][3],8 b, u7 u7 \1 B8 i0 X$ i! c
temp[26];& U, O/ X7 ]4 L1 ~( W9 y; p
int i=0,j=0;
) l3 }- ~4 k' W scanf("%d%d",&n_m[0],&n_m[1]);
/ Z: E, A1 o& V3 ] for( ;j<n_m[1];j++)* E9 B" |2 p3 F) [) `7 ~& M
scanf("%s",&re[j]);+ B6 C; y' ^" V4 q% Z; a1 ^9 X* O
while(n_m[0]!=0&&n_m[1]!=0); e$ n+ k9 C3 |5 ?& t
{. z- z0 q1 T: M7 O
i++;! f9 S/ v7 T5 q, E, K7 c
scanf("%d%d",&n_m[0],&n_m[1]);
% \* m! j/ M6 `# X for( int f=0;f<n_m[1];f++,j++)
) b0 ?7 Q' f. \- T scanf("%s",&re[j]);- W( X. k' v5 M8 h% F' t# F8 E
}
( Y: S* V6 v Z- A9 k3 ?- Y! m i=0;
9 t" t; C% Y, d j=0;% \6 I8 F e+ k' O8 Y2 R
for( ;n_m[0]!=0&&n_m[1]!=0;i++); V, V ~2 \2 t9 _8 F
{
1 U7 C; k8 F& u4 ^& [ int a=0,b=0,l=1;
' P: P$ n8 J" u+ q. }. V; \! m for(a=j;a<j+n_m[1]-1;a++)7 e& L+ v) R1 F# K4 h* @6 ?( X
for(b=a+1;b<j+n_m[1];b++)8 M) ]2 P1 n( l" {
{% h) P# D ^% w& @0 w z; F- W
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]) A: S% B8 t# i% a& O
{
, X, V- b* o/ |/ h& I0 _* g2 j4 T l=0;, i/ G# _4 Y/ n
printf("Inconsistency found after %d relations.\n",n_m[1]);
2 C; k: Z! x s2 A3 l break;3 m0 a' j v' H" Q: c; }
}6 r7 M; {& \2 S$ R3 \2 ~: ]
}
$ ?1 p1 D7 a. N9 M9 H7 {4 T if(l==0)
8 b2 ^& v2 g) t0 `7 N( } continue;//Inconsistency found after x relations.
- k2 Z8 Z7 B' t# Q else{
+ ]0 |2 c( v0 k5 o if(n_m[1]<n_m[0]-1)
* h) z) @0 Y+ N2 y4 k2 T0 \ {0 L+ W8 z% i1 [2 D' H+ a. L
printf("Sorted sequence cannot be determined.\n");
7 e1 m2 R, g) Y! V; S0 l l=0;
& g, l; _3 \) M }
. c# _+ H% i7 v# W& _0 C- [$ s0 P else- U' j2 ?( d4 |
{! t" }3 }6 O; O4 ]7 Y# a6 ?. v
if(n_m[1]==n_m[0]-1)# H( T# Q0 E# g7 \8 w$ a7 {
{ 9 B0 _. Y/ w3 t/ P8 u
int k=0,p=0;
' w& {2 h# Q) y0 D: T x; B for( ;k<n_m[1]-1;k++)
8 L" ?2 J' Q! b4 o9 h, t for(p=k+1;p<n_m[1];p++)+ }; n! V9 O( x$ p
if(re[0]==re[0]||re[2]==re[2])
; ~" x6 J* ^( i, r {( W8 H# W; l- j9 ]+ R
printf("Sorted sequence cannot be determined.\n");
4 n5 M; u9 o. I/ F$ q& [8 f+ @: K break;3 _9 |/ ~" g4 i' I# ~5 [# P' v' U z
l=0;
! P2 ^8 W) N' q- O/ p }8 R, ], E' {1 x# [; t6 Y) S
}( T% a' G* `) V/ b
}
! p1 _& b @" u9 p9 u- {$ ? if(l==0) : O7 d6 _1 h5 |8 g" s5 B7 t
continue;//Sorted sequence cannot be determined.
' d+ p {+ M: X: c( I1 _; M+ C2 V# r" z
else% r" w5 q4 A1 O4 a) F
{
# S* _: u$ {, \( A5 e
5 Q! }% L* O; f) D' C% c) i for(int k=0;k<n_m[0];k++)
$ f9 L4 }* N* W* N8 f7 b# ^ temp[k]=k+65;+ _# {/ \% S% P; r" A( c
for(k=0;k<n_m[1];k++,j++)
: B X d1 f: b, q7 {* E) U$ P' o {
( Z! k6 S9 s' f int t1=0,t2=0;/ N, W5 M! L7 U/ q
for(int s=0;s<n_m[0];s++)/ y2 `# {3 X* Y
{
( n) d# P/ k7 m' N. \( G+ b if(temp==re[0])5 Q3 z! U g6 ?; ^$ b4 a( v8 ^
t1=s;/ Q9 ~. n4 k& `6 J1 J- m7 i
if(temp==re[2])
% m, m- ~5 x( a7 H" |7 q t2=s;
$ @# F8 F! q$ E t }3 m# ^& v) [5 ?! C4 l
if((t1>t2)&&re[1]=='<')
( F1 D9 l) f/ o! u H {, j) c# }$ [+ G6 U1 {
char t3=temp[t1];9 D+ y& b( q: z5 Q
temp[t1]=temp[t2];
$ u% d0 H3 P: X temp[t2]=t3;3 C3 a G# V1 w' B
}
% n/ K5 }- E0 j% Z# K }8 ~2 W* U- |5 n W: N7 R( b$ p
int count=0;3 n. L# V$ T1 R
for(int s=0;s<n_m[0]-1;s++)
& ^2 v) l$ `/ P3 l for(int d=j-n_m[1];d<j;d++)6 J" x* t8 [0 D
if(re[d][0]==temp&&re[d][2]==temp[s+1])
2 J/ i; V- V7 S F# w& _ {
, ^ V/ c) e; Q5 @ count++;
. W" `3 S# V) M; `7 I break;
, j- b: F3 L6 V4 P- a. R4 h }
1 ], A6 z, J, E* C4 Q if(count==n_m[0]-1)
2 d& ^1 P: `0 T: d {3 k6 w E2 A$ {# `1 _
printf("Sorted sequence determined after %d relations:",n_m[1]);
+ g0 e2 ~3 D. [- U3 a* a7 d0 z( t for(int f=0;f<n_m[0];f++)
: V7 Z5 n3 N5 i. e8 C printf("%c",temp[f]);8 W7 n4 z/ P2 d
printf("\n");
+ t: z- ?, [4 s$ S% G }) D$ _" m& O% H6 a$ |. G3 z* R, p" X
else
5 H$ {" ]% t+ @' \( h) b printf("Sorted sequence cannot be determined.\n");
( g6 \: \1 ]+ W* x0 \ }
2 w/ G* w2 J9 g6 S( E' }3 z* A }+ M8 l8 w+ B; Z
}: Z- {5 V0 F( P F0 c8 b% n% d
}
/ g. y! H* H3 f4 M1 E
0 [! Y# x7 e$ Y' u' V2 J+ B
& r% x+ W. Y" v3 f5 c
5 ]2 }; W- ]4 S6 J4 E! D6 z
0 V& C" L. i7 r- ^. Y {6 X4 }6 n1 T
; ~2 y. z7 q ]% r4 G% \
$ d0 w8 B/ z9 ^# ]% C& w
5 I" o! c" q% L1 d Q1 ]2 W
来源:编程爱好者acm题库 |