本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
( M7 |4 \& Y/ B- d2 y; N1 s, |
( i% H5 C4 _% T9 I8 R- R/ Z: W- PSorting It All OutDescription" s W4 r5 O1 ~/ [, O
. M* g. t( e$ C" a: [" U
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.
; Z: ?1 q4 V) V. k7 h4 oInput/ \, @. k% z4 A
& q* T7 r* l0 zInput 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.5 N" V6 B7 ]6 l
Output1 r1 e- ]& ]( g2 q3 w
0 Z$ j) P* g; h/ e9 o
For each problem instance, output consists of one line. This line should be one of the following three:
& e0 Q1 J$ L$ j3 B3 B% ~) d# q7 H6 r/ v
Sorted sequence determined after ** relations: yyy...y. 5 I# q G% v# S+ s0 R6 _. J9 I" z
Sorted sequence cannot be determined. & L, M4 h$ h z3 i: U9 D$ W
Inconsistency found after ** relations.
8 @9 ?" x- F2 g5 ?3 ~; E
6 c: ?( \5 K$ B. U* r" S/ Owhere ** 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. # {8 ]4 y/ N" e9 O0 Y
% H5 K, x: C5 j/ F8 D输入样例 2 k( s2 H4 B, } T" O2 R L
4 6
( x% ]' e. ^8 PA<B$ o% o& q; B1 u& b. P
A<C" c. t' W9 }) ]. W0 o' u9 H! |
B<C
* A( {3 V1 \; y* iC<D! U$ ]' F# a; O( \
B<D
9 ?# w" v% I: u g' DA<B
9 a. }5 t+ W, B7 n( c. f% @3 22 I% z# G2 Z4 J& [. |& T0 f/ c
A<B7 G- o' a; Y- S# X
B<A/ _! H. J. I, O7 F& K* K
26 1
6 @0 G' i5 h1 t2 DA<Z8 p! O9 x. \' W4 s& {" N m( P' c$ M. b$ ]
0 0
1 a4 i/ @9 p8 b0 k6 s
' h. l" @, ^# U! b3 _" S2 K输出样例
1 V5 R: Y# x+ |2 ZSorted sequence determined after 4 relations: ABCD.
- R1 o8 l2 s& Q. M) L2 w9 h" QInconsistency found after 2 relations.: d# R3 v1 ]3 X8 N# J. Y; N
Sorted sequence cannot be determined.
9 u+ _$ l% h& w9 e8 BSource
$ s) o$ i3 g1 w4 |2 r* K" g1 [' G' s7 F9 q
East Central North America 2001 % v+ w* Z; G6 J( \6 ?& ~2 Y
程序解题1:
& P# ^7 ]9 l; q1 j7 E- l//By Jackie Cheung W2 H% c' Y6 ?, ^3 {
#include <iostream>4 W: B0 o: ~4 e3 ?
#include <string>
( X9 c* F& ~. W- s B2 X' A#include <cassert>- R; t" J/ I" X$ Z* x. \' n
typedef struct tagNODE % ^1 k) ], X, R. C6 {7 P0 e
{0 Z. W- z! n7 O9 N- |& t
char val;, }3 X6 A$ m0 f8 o; A* R6 B, f, Z) Z% Z6 M
struct tagNODE* link;# L6 r8 B4 j' Z. b9 U9 z
}NODE;' U' {* X9 h1 X
using namespace std;
. G2 y R- h9 K- @! Z& G9 }void Marshall(bool** arrary,int n)
% m% T3 H% ` L0 R i, k5 h{
/ Q) V% [6 y5 C, ^ for(int i=0;i<n;i++)
/ w9 ~7 o; p& X7 H" @ {9 m% \6 R8 d+ B O0 o. ^
for (int j=0;j<n;j++)
2 [& D+ t/ N6 b3 R {
, [& R/ Z/ F) p( J" U if(true==arrary[j])
: O$ X' G4 |4 y% }0 e' u for (int k=0;k<n;k++)
$ H- v" |$ Q( u* q( T7 K" i {! ~* k/ \5 p l, X
arrary[j][k]=arrary[j][k] || arrary[k];
; v) S+ z Z% p' O }
& B2 I+ Z: n* [ }! w* w i6 a1 H6 H6 q
}% _* m9 ?& L( @2 h
};
" s1 A7 Q! [4 f1 pbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)& s$ V {4 P7 j+ g- A
{% x/ N. w l( M7 H
Seq[n-nLeft]=static_cast<char>('a'+nIndex);$ n' [/ b. ^( ^' }6 W4 y' B! m7 k
bool bFlag=false;
/ ?' u9 W& C$ ^ z# e8 x% k if(1==nLeft)
8 _: a: M; d) O {
" i. y8 W; |/ B( P6 W Seq[n]='\0';
" c* D6 r7 n$ h' |7 F return true;
) Q1 J$ k( b' s4 X* H5 ^ }
2 g, x$ F. Y3 [5 C3 j) d& G Q for (int i=0;i<n;i++). }9 y) m8 s6 {$ i$ q* N+ W+ }4 R
{3 _# p5 I$ M7 _1 ~/ |" E' ?
if(true==array[nIndex])1 y, t! C; Y9 Q; L
{9 h/ f& l. R0 u1 G% g+ }
9 v; U1 s9 F" d, A2 I$ q
bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);; E2 e P/ t* v$ v2 R. j
}
1 J" w# C, T. E2 i8 I* l9 Q if(true==bFlag); @, T: e9 Z, W/ J! P6 C' H
return true;
4 l; y; V0 G4 S9 t }: K7 @) h. X: f6 F& L* W
return false;9 X! r7 @/ a. ~' \2 C
};
$ q; v) l8 O Dint main()7 W* H. o3 A! g+ }
{
m# l1 p! d P+ G7 j int nSeqLen;
+ c/ C/ t, x- z' d ^& l; P5 l int nRelNum;+ T! Y$ O* w" [) f( |
cout<<"Input the length of the sequence:"<<endl;
/ Q& w6 r, n% f5 G$ u# X cin>>nSeqLen;5 s6 J/ V2 I3 [9 w$ f+ P" k6 j2 H
cout<<"Input the number of relations between the elements:"<<endl;
$ n- h& g# ^+ g; r; X5 m cin>>nRelNum;
7 f) S% Q+ F9 t. j6 x/ m //1:if nRelNum<nSeqLen-1,then the relation can not be determined!% P3 ?. d. L9 h% c; J8 w
if(nRelNum<nSeqLen-1): D7 Z' \/ ~' X# z' a" p
{
( c8 y2 ~- x' W3 a6 J4 Q; _ cout<<"The relation can not be determined!"<<endl;
5 F9 \; ~4 P- U' v& h return 0;. k# A4 p6 D0 s
}5 m s$ E% e( `6 Q) q' M
string* strRelations=new string[nRelNum];( I; ~; e& I1 y! a( p! D
char* Seq=new char[nSeqLen+1];
8 A/ d% B. j! O8 g5 M bool** array=new bool*[nSeqLen];
6 n+ W }! z; U" u2 Y( g
8 L1 c- f( A; J' q# x% j, h k for(int i=0;i<nRelNum;i++)
% l( e7 H/ T2 _4 I0 F5 _7 Y" s% d {
4 i! z. D1 ?+ e- x6 c8 k cout<<"Input the "<<i+1<<"th relation:"<<endl;
- A* r' E6 {. @ cin>>strRelations;
9 ~: Y) }5 Q' C: b" l }: j. N, P# S" A. A& G
0 U; H' e7 y" w, m for (int i=0;i<nSeqLen;i++)3 o, b, ^+ y! _9 m- H
{
( ~; W6 ^2 C m% ]3 Q array=new bool[nSeqLen];
. k! M4 `7 k; n" y for (int j=0;j<nSeqLen;j++)
0 i. W6 G0 ^# A1 i% a array[j]=false;
, H6 ~2 ^* q* p2 q }& f* j4 h; E% f, S; ]! Q( b9 n
//The main loop& f. H9 F0 P" } C$ @
for (int i=0;i<nRelNum;i++)
% s% Q7 Q1 Y% ` { v% ^2 y! h; k2 E0 Y n2 R
char a=strRelations[0];9 x% L- X% V) d* D
char b=strRelations[2];
( q" @9 u: Z6 q q assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen); ?9 u& }& G1 W) c! C
array[a-'a'][b-'a']=true;
4 x$ |' @" s! y( V! e6 ?0 t+ d& W4 M a W& l t9 X
Marshall(array,nSeqLen);
7 B/ n7 @; m* Q" _) _9 \3 P
( t M } `7 x% m5 L" z5 V //Check for Inconsistency after every relation2 ~" x; [1 O+ s' _9 `. Q% b
for (int m=0;m<nSeqLen;m++)- Z! Q8 `8 p4 V+ T7 i* W; y9 ]0 Q
{& C6 i( t- p4 C0 ]" R& D
if(true==array[m][m]); i+ \6 e4 s% A! f" T. j$ y
{
- U y) b2 u R' Z( C cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;, T; \- y: ]" J: ~9 H
delete []strRelations;4 X. O% Q, z; ?! T; K9 X1 E& J3 T
for(int k=0;k<nSeqLen;k++)% |6 J" \8 ^- t" X
delete []array[k];
5 `0 l" H7 u- z$ D9 ^ delete []array;* R. J" v! e% _
delete []Seq;; B W5 r9 I; [' N' I& Q R [
return 0;5 [- T" o6 b1 ?- A3 `* Q& q
8 T1 H) Z. j$ }4 @6 r }" _ p7 z$ \. B& S4 _0 Z
}* \5 H; x2 a# I4 G
& ~% c# _! ]8 Y5 @1 Z9 [ //Check for the determined sequence after every relation
6 ~8 A" s$ a8 _. `8 Y for (int j=0;j<nSeqLen;j++)
9 Q& q: ~) I4 n; e0 s" W6 [7 |, J9 z {6 d( F# r/ ^/ G. d/ j0 {
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen)) ]6 c- ^& J1 k X9 c0 |3 Q
{1 O: E b& m& a" K% W* J. I k
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
, p& _& _+ ^2 | D$ y delete []strRelations;* q2 g0 b6 Y- w; r$ k U
for(int m=0;m<nSeqLen;m++)' a# s8 L4 e+ L# c
delete []array[m];; D3 f- d& T- \2 s0 e" c: H* E) ~
delete []array;5 t* t+ c8 B0 ^; S% v( t, f" L8 ?
delete []Seq;
! l" j3 u$ m& h( e2 m return 0;$ Y5 @" t/ t+ E$ s( Q5 J! X
}. p8 W' e) |* K, W( O3 Y
}
2 x. q1 W( T: A" A3 F- T9 `( {) a# K& h# w* h* k: C
, w! ]; {/ E2 U }' \3 N3 ]1 u& E6 l
//If the program has come to here ,then the relationship hasn't been determined!!!% t( U( P5 B$ m, d1 t' e
cout<<"The relation can not be determined!"<<endl;- b) ?4 ~& z6 \3 f9 K- r
delete []strRelations;
' X; M/ A8 F- i. @+ d8 k. U for(int m=0;m<nSeqLen;m++)4 {9 }" ~7 D! I4 c4 j" y- J
delete []array[m];% `- n. S1 p9 z% {
delete []array;' F% ?, {- h* f' k2 B" h
delete []Seq;+ t8 z) W! R$ }* A: I
2 _, e" q2 c- l9 [/ A; D$ C return 0;
, Z# I* |& }7 X0 {: j% U}
! R* U- l! L- S( k+ D( F" O$ ]8 ^) D% K1 T6 |
程序解题二:#include"stdio.h"
# G) u5 f" f3 j; g1 Y3 h5 Svoid main()
7 q$ l) H# p3 Z2 ~" N" F0 K{, T$ Y8 M \1 V6 Z2 P D% W& F# X
int n_m[100][2];
; [9 r( v- h& k char re[1000][3],
0 u1 T0 Q9 X- X+ h0 S) T) s temp[26];6 N h* K6 `: F+ F$ G( r! f
int i=0,j=0;1 B0 j% G; A8 ^: I3 w
scanf("%d%d",&n_m[0],&n_m[1]);
8 x, L; L. ?/ _: M* v# D( x for( ;j<n_m[1];j++)/ O9 q! n+ _, u9 C$ Q5 @) W n
scanf("%s",&re[j]);
?* l- b; _% x$ `. l while(n_m[0]!=0&&n_m[1]!=0)
, Q% R0 _: L4 t% B" f0 v {+ N8 F; ]+ t/ i7 ^! _6 w
i++;
6 p0 z- T1 s& y% k' H, D9 ?/ e scanf("%d%d",&n_m[0],&n_m[1]);6 e4 m6 ]) D$ ~& i; m7 c
for( int f=0;f<n_m[1];f++,j++)7 g- ]7 W' I' _ J5 X, ?. j; o8 C
scanf("%s",&re[j]);: j' C" M9 r: c$ |: Y: p3 x
}
& J! E5 g' y8 u2 j i=0;9 j& V5 \) P5 T! N2 T" O {8 ]
j=0;
/ R6 O# L( u9 c; U' S. O for( ;n_m[0]!=0&&n_m[1]!=0;i++)8 @" ^! y5 `0 K* E
{' Q: W7 ]2 R5 [: o
int a=0,b=0,l=1;0 U/ o2 V7 u( L/ v2 P& A
for(a=j;a<j+n_m[1]-1;a++)5 f' C* X6 Q7 H8 ?1 v. `
for(b=a+1;b<j+n_m[1];b++)2 O$ L- f( P7 g
{
; N8 b% j6 j u3 E2 Z 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])* \& C' H/ o2 C1 [7 x" \0 }
{
* N% K s( R: @9 | l=0;
. o# @% T4 S" k printf("Inconsistency found after %d relations.\n",n_m[1]);$ A3 }- f1 E# \! N
break;
9 Y- i3 J& t9 R) k& J4 m; J }1 K, a6 @5 ?* p* m! a" l. x
}$ h& s9 e- Q& w7 z: w3 m
if(l==0)
6 {. ?6 x) b! v: c) o continue;//Inconsistency found after x relations.* D b7 Z& ?6 D3 f/ w o0 N4 H
else{1 g7 _- E/ l( F6 }% V
if(n_m[1]<n_m[0]-1)
! E1 z5 ]; ]0 X& q. z/ k {. z6 _; `) F: }( k
printf("Sorted sequence cannot be determined.\n");, B/ l$ i) C8 |" G6 p$ v6 Q
l=0;
# e+ |4 u, p0 O }
! B3 R: T+ Y6 U. u7 I2 W else
4 Y3 s4 u' v2 H [3 u' K {7 |' s" O- Y6 N( p# t, |( D
if(n_m[1]==n_m[0]-1), P: c9 A9 f, q" ]( z6 v
{ 8 n5 ~6 O! v9 Z3 h
int k=0,p=0;
! ^. Q; n9 K2 T: h! c for( ;k<n_m[1]-1;k++)9 e; T# t( W( M% h- t& J
for(p=k+1;p<n_m[1];p++)- l- m2 {/ ^" ~9 y
if(re[0]==re[0]||re[2]==re[2])8 S4 M( y2 z; o7 d
{% \+ K1 w6 @4 o% m
printf("Sorted sequence cannot be determined.\n");
! N- ~, q5 w8 V/ a break;2 A) i9 B6 d d1 X4 u$ O* u( G6 {
l=0;2 H$ b( I k( P! F
}) ^( S7 r1 D4 J) J5 P# o/ R9 U8 U9 M
}# _. f1 @1 Q, }
}4 W* \9 A0 a+ ?1 h$ }+ H
if(l==0)
8 X/ E2 h8 ]" O: s continue;//Sorted sequence cannot be determined.- i/ S! B" R; T# ]8 z2 K5 [8 F+ ?
- W4 v) i' |' g( x6 I6 l6 A$ O( A2 J else
0 _! f( `7 Y8 u* O3 w% t( K) H {
9 k1 w( r; c# B/ b% s) X( U ( f6 n% K: ]! Z. Q( h6 z) ]* }
for(int k=0;k<n_m[0];k++)
* W( K* K/ t* I1 R4 I temp[k]=k+65;
- r% |# @8 t% S' W; r: {- e) S for(k=0;k<n_m[1];k++,j++)+ M- R" W6 n/ b: f# u6 c
{
0 \- J$ X3 P) @$ n5 Y1 K0 C; Y; @4 t int t1=0,t2=0;
1 [+ D4 w& `. ^, i: h4 Z& \ for(int s=0;s<n_m[0];s++)9 m) {. l* P( o9 ~# y% y1 V) Q
{
$ t8 F; d6 I- z6 H! ~8 o; L if(temp==re[0])2 E2 n7 d# |# d# }* ?. Q
t1=s;
+ F9 y; M/ l" M6 r' R0 |; R if(temp==re[2])
( G4 r7 k x: o {% O t2=s;: J; C$ t* h! `: {3 L- H
}; q$ y9 r# r+ R& b) [) y' O( H' }
if((t1>t2)&&re[1]=='<')2 r" x3 {" i! I# L4 R+ Y2 k& J
{: l! O& r, U) M: F
char t3=temp[t1];
" L7 y6 O1 O2 S temp[t1]=temp[t2];
2 n) v& V! _6 v* S8 C0 o2 x+ b temp[t2]=t3;* W, c2 t q$ }$ K( Q* u7 p- K
}5 r+ S' c+ U- d D
}
8 W) {. M/ n: ?9 x% j( ^ int count=0;
; P) M8 |' c% ~" d: x( ^2 y for(int s=0;s<n_m[0]-1;s++)
0 N& P7 m0 l6 z6 @- a7 I/ p% O for(int d=j-n_m[1];d<j;d++)
" C/ |0 |3 v9 t0 } if(re[d][0]==temp&&re[d][2]==temp[s+1])
; v) {8 l( ^* C {
* t8 G. C1 i+ J7 h- K/ S count++;
0 `; X7 ?4 P3 b break;
K0 y# T' Q* q4 M0 \+ {! T }
8 w2 o5 x( ^5 Q4 r2 R- i/ o if(count==n_m[0]-1)4 O, x2 O/ R; o
{" o7 p0 f# w0 K
printf("Sorted sequence determined after %d relations:",n_m[1]);" s4 q/ K8 W* M! B2 r- i" \
for(int f=0;f<n_m[0];f++)
0 G$ v6 b B1 N3 @' k- ?- d/ j- ^& W F printf("%c",temp[f]);
1 U, m% k& X& C( e% {3 p printf("\n");- T) O! P# L+ d
}2 O+ f& t! o/ ]) T6 G
else7 y9 k, H' n, Q$ R
printf("Sorted sequence cannot be determined.\n"); 8 z2 z% \% m: H# |. k2 T F' N# y! { U
}
6 Q- E& D& {- h# I }1 m$ p4 }6 n/ y( w$ d; K$ K
}+ Q% L# u4 `; W E
}" c% [8 h* o" W) R, c& m; ?% q
4 F) I8 @( @& h+ P
& r$ e5 b0 N6 n
+ g2 b; t5 l& a5 K9 |; {$ G: o2 b* M! C
$ @5 u5 V4 [' R+ V
% I P5 j3 _% C9 @# |( r1 `* N- R( X% b7 r+ O8 l3 g
! }9 z* N& I- G% h0 h7 t
来源:编程爱好者acm题库 |