本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
2 ^( G% S8 F+ ?1 @* P8 y0 d; R! e J" [ u- y. e& @2 K
Sorting It All OutDescription* b5 B1 E) T! a: J$ \; a
: T, c0 t2 ^. |* F8 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. / h1 G: k" u" M6 [4 q) z6 s
Input8 _9 J3 x5 Q5 {# I
# z9 U- v5 ^6 [; a" E$ Q* E7 d0 I* g
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.
. z3 ?4 a4 Z) N P7 z" q$ @" |Output. W! z2 Z5 P- f* G9 M
) ?1 d/ I8 X/ bFor each problem instance, output consists of one line. This line should be one of the following three:
* Z. S2 o) i' e6 j8 Q4 A! i
! V" B5 x% \) R9 v0 |Sorted sequence determined after ** relations: yyy...y. 1 H# U+ X1 p: u# i8 ]
Sorted sequence cannot be determined. 8 q; N* R; k6 J
Inconsistency found after ** relations. ( o( ~% D: D! C4 K1 C1 d
6 g. l; _1 e R& U; Hwhere ** 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. 6 Z2 Q! _$ R4 j+ N0 c6 z+ }) r" b
! C% p& n1 ~+ p K# r0 }2 e& ~4 Z输入样例
) Q% Q) ^6 c5 N2 \: Z4 66 f7 a4 k# L# O3 A
A<B
1 I( ]9 d& J+ e$ c$ f6 N+ k" uA<C9 ?- c* t5 @( S% T! m w" I" b
B<C
2 e% E! A1 U8 V) l6 M# AC<D& [ w5 i' l/ F \* X4 O
B<D2 t. N) R# X9 J& i
A<B4 h# _; _$ e( t& z9 |( o
3 2
6 s: @4 U; ]( n( o2 P& VA<B3 N3 Z2 [* G" m+ R$ j' ?; w- O
B<A
T& f; x3 w+ k6 S0 R26 15 M- p! c! L% }
A<Z
: |: e8 m) o4 R. d0 0/ t, u( C, \, Y7 V+ n
( ^) ^9 N9 h x3 q1 O+ A
输出样例 - ^! d9 F* n; A1 \
Sorted sequence determined after 4 relations: ABCD.# J- J8 Q- G) l' V
Inconsistency found after 2 relations.
7 G) F+ X4 d$ d. F" [5 m8 SSorted sequence cannot be determined.
& t6 }, z$ Q' o$ x' uSource
$ r8 c( p" p; [& u( M) B% w: C1 j$ `) @5 P! _5 x p
East Central North America 2001
0 M- B" J& t8 o: E5 [3 Q% A8 R程序解题1:
( l! h8 A1 s/ B# j. l- v& t' Z9 n" m//By Jackie Cheung
6 T8 {: s. E5 D) _#include <iostream>- D3 j( m% Q Y0 H, {7 E0 V+ J
#include <string>4 K% X9 ^5 O- d+ d1 B8 B* q
#include <cassert>' t; j5 E4 q# N8 T5 T5 d, d) ?
typedef struct tagNODE I4 ]: P# f1 I E) x0 ~
{! {7 n$ K$ ?% E+ p; ~
char val;
0 N6 j6 f o4 N& p9 L3 g2 ~ struct tagNODE* link;
! B+ w' F5 g" W8 d}NODE;
6 {/ U6 @% O$ ~4 r7 u Pusing namespace std;: t* r3 o1 v8 g$ x
void Marshall(bool** arrary,int n)
9 ^3 R# u6 w1 S+ x. F8 i{
8 J: y6 a j+ @/ T for(int i=0;i<n;i++)- `9 Q, A1 a% J5 l6 {! {0 [' d
{
$ W3 ^2 N, ^" B for (int j=0;j<n;j++)5 _8 Z! Z8 s% H/ G
{
) I; R3 M* r* }. u1 W, W1 u if(true==arrary[j])* S2 a/ f7 N; }$ i1 U0 d
for (int k=0;k<n;k++)
1 B; V8 u) \% z1 m/ R" ` {
9 W1 j: b2 G+ F" y1 Z4 o) v% e- k# y4 Y& V arrary[j][k]=arrary[j][k] || arrary[k];
% h8 R7 b/ e% P3 H: I }
$ D2 q; }* E& p# ~5 H# k& o }
8 _: l5 _7 r# ?5 S5 t B }4 @2 K1 d: r3 s, H8 z
};4 Q5 {5 [- Y! }7 z
bool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
* W: Q# ]) y5 u1 w2 {/ M1 u{3 r6 c0 ]6 m5 p/ ]1 R. ^6 L- V
Seq[n-nLeft]=static_cast<char>('a'+nIndex);1 J4 t4 X* d L1 d+ ?% L- u* M/ D
bool bFlag=false;2 K2 l3 |9 Y7 ~. N* W
if(1==nLeft)
; u0 e# z4 n' Z! N0 c8 B {
. X. |- s$ u/ m Seq[n]='\0';
& j7 K% g" w7 I& J! ]! \- K return true;
4 B, [3 B7 `: H4 a# W+ M }
# i7 Q+ Q0 b x, T* ?4 [. D for (int i=0;i<n;i++): B& Q5 T! u, ]0 L* {2 ~8 T( F' B
{
B; u2 B6 ~7 V; b, n if(true==array[nIndex])
1 D2 B; j r- v: T: J {# T$ `0 p; O3 `+ d/ ?7 s) M9 @( }
2 z e6 e/ I3 z0 K; a; n% S o bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
1 A! L- J4 z% p7 Q }
y; j L! a; ~# o5 ^& K if(true==bFlag)
4 I7 h7 Y+ r7 n4 _ return true;6 { H/ m# }6 K; L1 p* v# E
}+ e, A/ L. F, q* v& |& e; j
return false;
! I0 p. T* ^- f o0 a( V: h}; v) k/ G w# N; ?9 T
int main()3 ^4 Q6 R! `2 W. }. s! X- c
{' @ |; A5 N; @0 a( P2 }
int nSeqLen;8 G0 Q _) d6 h9 R
int nRelNum;
2 {" g9 `& n1 h q8 M: ? L2 E cout<<"Input the length of the sequence:"<<endl;% X. X& T; K. t3 h( l6 n$ ~% \1 @
cin>>nSeqLen;
$ s* T- }/ a7 ?% \4 @0 v cout<<"Input the number of relations between the elements:"<<endl;
) `7 c. S. E3 B! I- v. q cin>>nRelNum;
1 P* m% c- r9 ^: b //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
3 I3 M" A( L1 o) m2 |0 u if(nRelNum<nSeqLen-1)# k& x; P4 a+ u7 \. r* e7 M
{
" b- G8 @& d& x' ^# n! G6 t0 e3 I cout<<"The relation can not be determined!"<<endl;
# h, ~9 y: ~6 }5 `( ~ return 0;& c% ?) B/ O- x4 d
}9 L+ L* `+ \! b+ u8 P
string* strRelations=new string[nRelNum];' Y# I+ Z* K4 M F0 k- g( X
char* Seq=new char[nSeqLen+1];. e2 ]6 r, P+ g; }2 t! a
bool** array=new bool*[nSeqLen];
: b2 \) z, k) I+ w6 Y; B/ N; [; M2 g) ^ A3 b4 i
for(int i=0;i<nRelNum;i++)" L; M K" W8 Y$ ^) z
{
1 K l, ]5 L/ ~. E9 u7 Y cout<<"Input the "<<i+1<<"th relation:"<<endl;8 ~' u& a! t! e
cin>>strRelations;1 _+ J3 ^2 y; S+ u8 g
}# U3 n- T9 m! u: m
1 O# T) R) x# H! { H2 S for (int i=0;i<nSeqLen;i++)
5 }* M: \" n8 X. ] {
0 Q# S) E6 f0 q' A1 Q' O array=new bool[nSeqLen];
) L7 {" u$ H) J$ V5 n7 M; u$ ^ for (int j=0;j<nSeqLen;j++)
/ ?$ o2 l. o; j7 }% K5 Y% u array[j]=false;
, |- l. @1 m" H; R }
; T) X9 b% \8 s% r4 e' R //The main loop
8 R+ ^+ b% C! E0 W% T for (int i=0;i<nRelNum;i++)
. d" N9 h5 e0 T2 C {- K7 O& k4 }' U" A3 ^+ q+ T
char a=strRelations[0];
+ H! J+ y/ b" b char b=strRelations[2];/ {; c! T; o K
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);, q! @! o; b. r4 H
array[a-'a'][b-'a']=true;1 P0 @6 y1 [8 B0 r1 I
2 f0 x& `6 v& N W) p
Marshall(array,nSeqLen);; Z) p/ M3 g8 R8 d& I
$ Y3 _& U: ~, V% U
//Check for Inconsistency after every relation
* z8 W) \$ \7 S. z3 ]8 h1 v3 b' G for (int m=0;m<nSeqLen;m++)
X. ?, j5 w" z8 ^0 x8 t {7 `& I8 `7 x* Y+ l6 e" ?
if(true==array[m][m]), @: E! a6 Z8 [+ B) T' U
{
2 `1 g$ s, r. }3 s2 Y' w3 @" Z6 W5 X cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
+ h2 ~+ c2 |$ Z$ h+ U6 C delete []strRelations;
( H' f9 V6 ~/ ?; |# e' g for(int k=0;k<nSeqLen;k++)1 G& a4 [- r0 }- f+ J' L/ V1 D% x" N
delete []array[k];' H7 p+ m! {3 G/ E3 u: i y
delete []array;
! k- [+ v9 N: e5 r# G, {4 P delete []Seq;
' S& N/ w2 w1 Q; p return 0;: @! M, z" S( T9 R% G: p
) H3 r8 E- {% K& I# n
}
( O" d! i! s4 x. k2 n }; M- i/ G% F, h/ k. n X, x
6 K, F( u( {& Q5 g //Check for the determined sequence after every relation
B0 n, A4 A3 ~0 g, [+ r for (int j=0;j<nSeqLen;j++)4 l+ v7 f# p6 ]+ n
{
1 ~ C# d% j$ W$ {+ ?8 V" C) v if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
( `5 R0 K4 w2 N! {$ u { T6 o) I6 E7 Y* V5 @
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
5 i/ Z0 u- ~, B* b9 }: k) y delete []strRelations;6 ]- W1 r- s# ?! e Y& n/ O, l; }
for(int m=0;m<nSeqLen;m++)
j, X" P. N n9 |+ _ delete []array[m];
) K& k* I R2 ~5 A. u9 t delete []array;
, Y, {" g6 j7 U, T A9 e# N delete []Seq;
7 p5 u- E$ i5 `0 r* q2 Z$ g/ \ return 0;
: T( u4 y7 N; y, h! M }
1 _% L0 o: t+ c/ f+ M# A$ ?' _ }
0 v. x% ] N: v
, M$ g: u/ n. C% I$ m
9 h' z+ \6 r1 |7 W" S, Z }
, p9 J K) z7 {2 V- X7 X //If the program has come to here ,then the relationship hasn't been determined!!!
) S+ D& S) h7 W, d cout<<"The relation can not be determined!"<<endl;1 I/ w# n [" N3 x+ c; s; s
delete []strRelations;
8 t8 ^2 }1 G3 J; m" n( _ for(int m=0;m<nSeqLen;m++)
6 s" g7 \% H. }0 Z& F delete []array[m];1 G: }! e; l, M: _
delete []array;8 ?3 N+ F: x5 }! B
delete []Seq;
. z$ I* A2 W! w" ?+ }
7 P; p% `9 T: k return 0;
" g$ g) F% o1 s9 a4 o. w}6 _% F3 a, c5 x% W/ t. D; a
7 {+ E; Y. y0 N/ Z6 X/ t程序解题二:#include"stdio.h"& @' L6 }- ?. B2 R
void main()
) t0 @8 w3 G1 F' E# Z- K4 W: }{
+ R+ ?1 H( G( W int n_m[100][2];6 W$ { }5 Y- q+ K: m' p y
char re[1000][3],
+ _# ]' L0 O& f( {. L1 P( q, R, S temp[26];
9 `) x! B; u. k" \5 b5 D0 H int i=0,j=0;
! a9 Q/ h$ w) ?- s" e scanf("%d%d",&n_m[0],&n_m[1]);
. H+ w: e* T- p1 M+ Z) q for( ;j<n_m[1];j++)/ R. R. e; C+ T( D6 j
scanf("%s",&re[j]);3 u+ B5 F) T# N; p" v8 Q7 P, v6 O' X
while(n_m[0]!=0&&n_m[1]!=0)3 ?/ _# O1 ?% h
{7 p% o/ ?) v) D. \7 B i+ U
i++;6 }9 x. L9 E ]' h2 _8 E& `4 d& P
scanf("%d%d",&n_m[0],&n_m[1]);
% ]- D( G' W4 v: z for( int f=0;f<n_m[1];f++,j++)
7 d! r4 l; ]2 h scanf("%s",&re[j]);( Y+ q8 }4 {7 j: _- w2 ]
}2 l4 E- n, V( b6 n( X) ]) B
i=0;4 a: J o0 ?2 B' A w2 [
j=0;
- \7 y$ n" T/ l7 d7 } for( ;n_m[0]!=0&&n_m[1]!=0;i++)1 z0 l7 [+ c/ {- P( h4 N
{
% q# f: a) N y9 B' n* [2 U% T/ F# Y, l int a=0,b=0,l=1;
1 a0 S; r( t, g for(a=j;a<j+n_m[1]-1;a++)* u/ V6 a" t$ q+ }7 l$ H
for(b=a+1;b<j+n_m[1];b++)+ ~- k5 j) I5 `8 S+ Y( |: ` \
{
0 i/ T! h/ y) ~# r6 g0 c 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])- {) o* F/ r5 l! |; a1 x+ @- U
{
" i1 v6 J% R7 {5 N4 _: J l=0;
- T: w+ X& O4 @! R2 o+ [2 R printf("Inconsistency found after %d relations.\n",n_m[1]);
; M t% e( q, f1 h0 r( r. P break;
0 ?+ ^: C3 w J# N. S/ a }
% r1 g/ _( u$ A2 |" |, r$ Q8 ] }
: t3 l4 r4 _/ f& C: ?+ G: ? if(l==0)
5 t! ~/ b# u! X* }* J0 Y continue;//Inconsistency found after x relations.
% S" l: v: k; n4 p( e, r4 p6 B& k else{ p: Y& q) z, `9 Q
if(n_m[1]<n_m[0]-1)
% s* p' S% B/ {- j5 z {4 E! q0 _$ d; L! u9 `9 n& I& p
printf("Sorted sequence cannot be determined.\n");
2 r9 {4 B, |2 \* l2 V2 i l=0;& U6 ]1 g9 z: m6 d+ e
}& c+ N4 C8 z9 @3 ~( \
else
- P8 e+ H9 X4 x {$ P, W6 w, E1 p! D0 e7 j( q' |; t
if(n_m[1]==n_m[0]-1)
' [) m$ K9 B% e, E' G: \ { 6 @% x! @* s5 }- g5 a% Q) t# ]
int k=0,p=0;
3 T, X; y2 o7 `, n( C for( ;k<n_m[1]-1;k++)
) U! }6 {9 S# X7 i; p for(p=k+1;p<n_m[1];p++)
0 c9 B' R2 C3 c T: V9 s* M if(re[0]==re[0]||re[2]==re[2])
' P; M( i8 f( l Q {$ R' V# w2 V4 b9 }% T. s4 h
printf("Sorted sequence cannot be determined.\n");
+ c2 ^5 K; f0 P& @' U break;+ U7 o" Z0 n% }" z
l=0;$ \ |( k% W& F! C
}" @% G2 P J3 F% h1 U- h* x6 o
}
4 ?# _8 \+ ~0 w; ]7 Y8 t9 X. D, U( i }3 o" d3 {/ }7 G3 t; y6 V2 Y; a
if(l==0)
1 I0 D) s% O$ }( V$ X+ X continue;//Sorted sequence cannot be determined.
* Q) I* G( h. l( d# `* z. j7 N
- `7 _" E) A3 x# B. [! I else
. `- w$ m* S! @( v' d {: [# T, c& R' x! C5 o
9 t$ z# J# |) r- m$ `
for(int k=0;k<n_m[0];k++)
* \: c- C5 |7 _5 y8 y4 G. P& ] temp[k]=k+65;/ b# b$ a0 D/ h. U# Q5 `
for(k=0;k<n_m[1];k++,j++)
& e l- B6 f7 e1 r; d {
1 l5 Q! J C( @ b1 p4 R/ b+ w int t1=0,t2=0;# z6 W% r& B4 p
for(int s=0;s<n_m[0];s++)4 ?3 t& G% r, e! R) O
{
$ N( G0 N% R' p* o3 Y5 m if(temp==re[0])1 V4 r. C0 x/ w2 z4 F6 R
t1=s;
3 t& \ `) j0 h6 r; G H+ b if(temp==re[2])
. D5 ?8 f- n% L1 u. x t2=s;
$ U7 u4 A% _0 c! B" y9 t }; X$ C. j1 I5 u @" D. ^# b0 g% Z
if((t1>t2)&&re[1]=='<')- Z$ }# _' K C1 U
{) k# ]: `' U& A7 D$ ?$ X
char t3=temp[t1];4 t$ \! h8 a" F
temp[t1]=temp[t2];
9 m8 |! G% ^, S V, I0 Z temp[t2]=t3;+ s& Q" H+ ~7 c d4 i( ?0 f
}
8 z/ _1 ^: Z) E; a! N }+ p1 Q& F; X# Q1 Z
int count=0;
1 |( W9 `* V) x6 w( B for(int s=0;s<n_m[0]-1;s++)' G5 m, V" X5 k
for(int d=j-n_m[1];d<j;d++)2 K6 M/ ]. N& ]7 _. Q! l: B8 d; c
if(re[d][0]==temp&&re[d][2]==temp[s+1])
* W( A! P( f$ w3 H {% [# ]1 \9 C! \# k- z D6 X
count++;; |/ @$ [) H0 X
break;: E, N- L) N: R4 k [
}
" b2 ?3 v+ U. d if(count==n_m[0]-1)% e7 ?- x2 U2 k8 ]6 T
{0 V" e# X3 W+ d& X- l$ l
printf("Sorted sequence determined after %d relations:",n_m[1]);' w) C( Z+ B3 y1 @
for(int f=0;f<n_m[0];f++)
! d' ~! s/ M$ d0 n printf("%c",temp[f]);3 B, A7 h1 v+ Q7 i- [/ Z% E
printf("\n");4 f8 J. r/ z+ V! O- N
}
3 b P( A/ G: z9 M5 R7 x% P1 U else1 ^ Q5 g* y9 G
printf("Sorted sequence cannot be determined.\n"); % Z4 y& D% }+ A8 H( q& F; _; K
}
% _% X. S. Y* w5 _! N8 [ }
7 j6 d- [: F8 R! _ }) E( K0 E1 Z4 s$ B( v1 r
}
- f# ~; e* Y4 d; u* u
' j3 N; P0 ~8 E7 t; n, w. q$ c# ]; `7 e0 } g9 \
5 r8 V7 T7 @: c
% n3 p# E b5 u5 C( ]: ?8 x3 v6 ^! I5 o% @3 L
; e2 I2 Q# V6 l( n1 f4 v3 `
# t/ l& ^+ I" l
9 V( m9 ]; @# ]$ e来源:编程爱好者acm题库 |