本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
' ^6 L' _* O, P8 F$ w+ M7 r3 i3 z6 O7 t) {6 f8 `" i% T
Sorting It All OutDescription
8 D' O3 W5 ~1 k' D0 p- T9 d1 {$ [2 F) |8 Q
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.
$ s: N0 i! x) l4 A% wInput4 F5 U/ F- W+ k
9 ]6 M- T# W* L d- n, }' ~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.
6 U* u0 I+ c. {2 LOutput B$ E) {/ z& v3 Y, Y
; i0 x* A/ [9 Z3 vFor each problem instance, output consists of one line. This line should be one of the following three:
, T/ g% s: `. m: A9 _/ ?8 B d$ O+ {9 E$ w6 l" I2 b R* d, _
Sorted sequence determined after ** relations: yyy...y.
5 ^% W6 I0 x8 S. a& xSorted sequence cannot be determined. ) P y1 |" D1 x* ~# f. j. `: O) v
Inconsistency found after ** relations.
7 P G: ~# z* m/ V$ f. c. g9 f7 ~: a
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.
' m# e+ n+ U/ G% j. {4 G7 j, R2 `& \4 ]3 r4 i: X1 Y9 S: M* J
输入样例 + o ~7 O) N0 P* y ~
4 6- |$ l m7 l3 { X
A<B' R8 F+ P, Y7 }2 W4 J( K7 r
A<C
8 b+ n1 T. Y3 S# zB<C: v) I" x; z" H7 }$ A* J9 K
C<D& M! u i* X" I. s
B<D7 f6 ]" o. S @0 A7 c
A<B4 R! ~ G# A4 `5 s% U3 M* {
3 2* o0 P; J" E" B) i
A<B+ d: I' H3 c4 L0 h1 T7 q" q
B<A
2 @( t- C5 b6 P8 D0 l- s26 1
, J8 ]9 y1 b* @2 vA<Z
/ j X: n Z- j) q; {. d0 0
1 A8 K- v5 W: H. l3 K- S
0 G( H4 h$ ^* K" v% ]& c) \% P; R1 i输出样例
. X. r5 B9 J1 N( \( s: HSorted sequence determined after 4 relations: ABCD.& J( V" c8 P B# D: Q+ Y! J G _
Inconsistency found after 2 relations.: C3 i( \- U# J4 q7 k- M
Sorted sequence cannot be determined. # ~9 b" L% b5 G5 ?
Source
9 K1 w+ |5 ?9 [" h: I# E/ F
* Q: u" F& `5 Z; Y' JEast Central North America 2001 # {% a% D( N, R& K; L/ r
程序解题1:
1 H3 V/ G X' l: u//By Jackie Cheung
6 X+ O6 C- ?2 g" C#include <iostream>
8 q, V. ~1 X4 G. s) F* e" }#include <string>
! m2 m1 B% a8 w2 O+ v; T#include <cassert>3 [/ K& ]" v' k
typedef struct tagNODE 9 M9 O) u+ Y# Q9 }& p0 G% s
{
2 R) G7 _2 T; K2 p/ D) c char val;1 I f) h1 d/ K7 i
struct tagNODE* link;# V: v# i+ c9 S X
}NODE;$ M0 ?0 k& @4 p4 X# y
using namespace std;
# @' h$ A9 n6 Z. o6 o: cvoid Marshall(bool** arrary,int n)3 t, e# d" f2 M# B% Y
{5 i& b) H! u. F4 T) a8 k
for(int i=0;i<n;i++)
& h+ J' Z$ Q7 F {) {- ^0 M/ X& Y+ D8 z+ D' Z0 _, e; l
for (int j=0;j<n;j++)
- `2 T7 f8 i5 e+ T9 f3 q {0 n8 x) n; b/ B, k
if(true==arrary[j])
! ]; D2 ?! x" V. [ for (int k=0;k<n;k++)
' c; i) Y( f3 H% m) Y; l$ w p {2 s- n7 Y1 k; A1 R) c, K1 M& z
arrary[j][k]=arrary[j][k] || arrary[k];
* k! z; e/ G8 @+ W. i) H1 J }: H4 T/ Y6 A7 `/ ^% u/ q( Q
}
5 g/ M% U2 {9 \5 T" \: b' \$ ? }( [4 @$ v: z$ d4 U( y! l
};% m! H7 h t; Y
bool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
; \9 ]. q. [4 x. Q* C1 ^{- g, i: s; ^- D3 H% H5 W* Q! H! A
Seq[n-nLeft]=static_cast<char>('a'+nIndex);
* q+ m8 |' F) I# D5 @5 Z" q bool bFlag=false;
7 [4 q: E! F, h* a if(1==nLeft)- g4 m M) q( K0 s, Y
{
! Z+ C6 `2 N2 V( K1 U Seq[n]='\0';2 V; |2 C x+ X4 h x
return true;
- s- x: G' a0 S8 S7 A }
" h7 ~. g F+ F: w9 V' \' ^* `' | for (int i=0;i<n;i++)0 @/ Z0 r5 t7 {
{4 Q1 ^ F* S) ~( h& J
if(true==array[nIndex])
% r9 V- y; Z8 ~& ^2 k {
0 M) v7 y' m- t6 R* Y& H
0 R5 P! M! B( E' e& P j3 ] bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
' ^+ d8 [9 U) U9 ]8 B! E }
0 ~6 P3 ~, F3 g0 d. F if(true==bFlag)
# h5 l. |$ R1 S9 w2 h6 D8 K8 ~ return true;1 w6 b, F7 z( p. }3 G! ^
}
0 E$ c7 ?) s1 p( {8 o" ~' v return false;
% W( C& Z8 x1 T# b% `};
$ l y; V1 J6 o3 Mint main()
2 T8 Z, t& e4 Q) J9 @1 g{
. n6 f) @7 G, H int nSeqLen;
; [1 h" V4 w0 E8 I, i$ R int nRelNum;+ F# |( R' S' N1 r, D
cout<<"Input the length of the sequence:"<<endl;
0 W1 r) [ @5 @3 H- g( u2 V- t+ A cin>>nSeqLen;
- L$ I$ p! h, k( k0 L cout<<"Input the number of relations between the elements:"<<endl;
$ }: v# }2 f R0 l+ A5 E cin>>nRelNum;, X1 w! C( D, V6 x; X9 f
//1:if nRelNum<nSeqLen-1,then the relation can not be determined!
1 T# X U6 w" T u$ G% S* } if(nRelNum<nSeqLen-1), t+ j2 v0 n/ l& g
{
6 h; R7 z% k3 f3 k& \# Z cout<<"The relation can not be determined!"<<endl;! s4 @' v* t4 o/ w% s$ A/ a
return 0;
, N" z0 h6 A$ @ }
0 s6 a* A0 W1 N( Z2 Z9 I string* strRelations=new string[nRelNum];
( W9 Z k% E$ ], J- i0 w5 H char* Seq=new char[nSeqLen+1];6 i' u J% F5 F8 k) S3 B! N
bool** array=new bool*[nSeqLen];/ `% h- H1 o; D2 h; P4 A5 A) o
1 a& h/ \& ^( I$ s3 J P! X
for(int i=0;i<nRelNum;i++)
- S. f0 _1 W' J7 ?! b( A% c {
& b, e+ ~+ }3 x1 V N5 q cout<<"Input the "<<i+1<<"th relation:"<<endl;
$ d) X/ f' N: D; y+ T cin>>strRelations;$ H [$ B% D9 P! H# ~
}
1 V! u* V6 @3 l * g' Z6 y. w+ i% q7 H' A
for (int i=0;i<nSeqLen;i++)
4 W; r6 f$ K' Y; O* Q {- `4 K9 j$ U: u. i; B1 m; s" K
array=new bool[nSeqLen];
7 P- }0 u- r, B! |$ D( ? for (int j=0;j<nSeqLen;j++)
" ] F; l; a2 F' b+ \+ V array[j]=false;
# c2 \0 p3 D, w! J! J }
% R: S/ j* B- C) v //The main loop
; a9 z6 Z) P8 q4 O7 @ P for (int i=0;i<nRelNum;i++)
+ I, O3 I2 Z# E/ K# P L4 W {
6 F2 W1 d' l+ j8 z+ D char a=strRelations[0];
4 N" K. g( V! p! u+ U; p, D" H char b=strRelations[2];
9 O( t3 `- O. Y assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);5 j5 ~/ m; l) B2 p
array[a-'a'][b-'a']=true;
: o: i8 r# u8 V0 e" }- @
4 ^6 e- v9 `( G Marshall(array,nSeqLen);
5 r' M9 [/ L! f# D
$ V# J5 d5 M& E5 }9 D7 w9 ?/ b; g: { //Check for Inconsistency after every relation
n; U" M! p3 m% O, b+ s for (int m=0;m<nSeqLen;m++)% f$ ~; o- x) [0 [% E- @
{
/ y3 h' L( z' }$ w: R% p if(true==array[m][m])
" Q. T5 P- ]1 Z0 ]( z* g2 ] T {. }( O. v! X/ A y$ F
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
0 U! o! ^+ W( [: d" `4 L4 k* N delete []strRelations;' S5 K8 y5 H8 B( P _
for(int k=0;k<nSeqLen;k++) } Q2 j/ ^( h
delete []array[k];+ ~% K' M' ~6 B# o" Y5 t2 r
delete []array;7 ~! R A9 O. O Q7 V8 o
delete []Seq;
6 J) j7 b2 t, z) o2 R return 0;( k. \6 I- n7 {# R& O
7 p- B* i& K, F; L
}4 f" R. c2 X2 Q1 O% _* [: y
}) k+ c( u h- m: W1 [) R9 ^
* [/ ?% ]. _1 ?/ e8 H4 } //Check for the determined sequence after every relation : |) {1 M$ n+ E+ n- m
for (int j=0;j<nSeqLen;j++)2 D0 P" G3 L' L3 s; |
{" e8 m, n0 E9 k
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
6 F# h* m- S: J3 u# O {
0 L) x5 O; \, ^' ]8 ^' S cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
- v! F. m' m5 m* `# r delete []strRelations;
% x1 R/ ^8 u. o. B! U- a for(int m=0;m<nSeqLen;m++)
1 a& a* D% `5 \- u7 f1 E delete []array[m];; p+ q7 z3 Y+ p: l& {4 Q
delete []array;
" |$ ~. h+ t7 F2 w1 k0 {. B) J delete []Seq;
- j. J' E, j# J( `/ I return 0;4 M* s9 i# H/ P
}+ o3 y% K2 q1 ~- {) { J/ h
}
* s! j4 i: X: g1 t( v7 L; h$ h. k8 a1 d
/ l0 n; Z% f7 ~6 n+ C7 E }/ Y5 o3 i; J$ g
//If the program has come to here ,then the relationship hasn't been determined!!!
, |+ _0 T, }8 i& A cout<<"The relation can not be determined!"<<endl;
# }" B1 J& k/ z7 k delete []strRelations;! Q" b% _4 {- c! Q1 a
for(int m=0;m<nSeqLen;m++)
+ T8 f4 y9 m, R7 c* o+ e. w delete []array[m];
' w8 X+ ]* j* r/ O! x delete []array;: [% E+ E# r# e3 @% t& S# K
delete []Seq;
- l1 p/ g! m$ E' Y: W& P, y ; ^7 ?& q# F) ]/ I! A% x
return 0;
5 j3 M! Q6 k& Z# v}
" o: C" C' c& L z) `8 _
7 I* V# t3 Z) @, }! T U6 \4 U, a程序解题二:#include"stdio.h"! z B" Y( H# O
void main()" T" |# [5 k# X8 G0 f
{
; ] \) w$ W( H/ j int n_m[100][2];
0 }. V' N& x" g9 P" c c char re[1000][3],
! h9 p5 W; u4 F! J+ t T) l temp[26];
# w' v, T; z$ U b5 ^# S int i=0,j=0;+ K% k: `1 q/ E* W' ^7 _/ n
scanf("%d%d",&n_m[0],&n_m[1]);
) l3 G% T: u) w: \: k for( ;j<n_m[1];j++)
+ L2 I% s; M5 [+ \, z; s scanf("%s",&re[j]);
0 H/ E) |+ r7 ?, C% ] while(n_m[0]!=0&&n_m[1]!=0)) y; t# w! ?! p' Q. N' q
{) M/ f. Z+ D( y Y% [
i++;% ?& F) u5 P5 x( U
scanf("%d%d",&n_m[0],&n_m[1]);/ m, ]" s8 Z0 v/ e2 S8 Z# E
for( int f=0;f<n_m[1];f++,j++)5 E% K* h t$ z8 d
scanf("%s",&re[j]);( t" ]' a; n3 }& G
}
( r" Q9 k/ \) E9 d, _ i=0;
! [8 i+ [% k9 |' E# A j=0;
9 Z$ _$ M/ K. T( }7 U for( ;n_m[0]!=0&&n_m[1]!=0;i++)
3 p9 y# Z A& {- s- V, o5 ~ {
3 {7 U" u# J3 B, b int a=0,b=0,l=1;5 L1 f2 H; y* N: T7 l5 O( D- ]
for(a=j;a<j+n_m[1]-1;a++). c( @# M' T k. \
for(b=a+1;b<j+n_m[1];b++)5 @4 y& x; X9 ~' k
{; M g. {+ m% S, F8 n) B
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])0 r1 B- |% Z. t1 l; u
{
0 @1 \1 E% Y" S5 ? l=0;
9 M9 Y3 p* _+ ]+ @/ d printf("Inconsistency found after %d relations.\n",n_m[1]);5 D! {: Q5 P6 E' C
break;
$ |0 t7 C! X) t8 v! D I' r }& Z _: u: ^+ z. W
}
5 G( J% S" G2 Z7 I/ h3 C if(l==0)
8 q* x+ n* U0 F$ T0 `+ I. k continue;//Inconsistency found after x relations.
" q. a2 @+ s) n4 d: G B8 j else{
; e' x0 _3 P( |+ ?( o: u, C/ c if(n_m[1]<n_m[0]-1)
6 H$ b! q- W$ v {
# h4 X, _. X4 R+ N printf("Sorted sequence cannot be determined.\n");% t1 `* H+ L# g3 |0 Z* E
l=0;& P, f* a5 s5 @! L, v. D
}" m) X# S8 O6 r) r2 p `
else. c1 l. V" Y$ T; {2 Q& u
{
$ C, U1 R9 p/ ], X) t5 h8 C/ r3 w# I8 Y6 G if(n_m[1]==n_m[0]-1)
! q4 H7 h& H9 w { / t$ {! g3 t. S0 ^% q
int k=0,p=0;1 L4 o& V4 Q$ H" r9 \# ]/ m
for( ;k<n_m[1]-1;k++)
) l7 y- Y V1 U. Z for(p=k+1;p<n_m[1];p++)
- c- f+ c8 {4 p& K7 T if(re[0]==re[0]||re[2]==re[2])
' x% D2 |5 Y& ~& S {
0 W! R" w$ i4 Q' l7 M9 O printf("Sorted sequence cannot be determined.\n");& i% {' W+ H( N7 V6 g9 g' S9 Q" v, a
break;
7 y9 N3 S( a3 h/ o* Y l=0;. F# u/ N+ K5 u5 S" }
}, {5 T- C; L, g( J
}
5 Q0 Q' p3 H- d f2 o( @% Q/ @ }4 E3 W$ k5 `1 P% h3 r. r
if(l==0) 4 O3 X& Q3 Q5 d- U( \6 |
continue;//Sorted sequence cannot be determined.4 F; U; a- M* [' U0 d1 x: r" n2 E
5 W0 @. D+ t ?3 L else* Z2 l _* O" A T8 a$ O' W
{
. {7 x+ }# a7 C: A5 {8 s7 e7 r% `1 r
9 y1 @4 C- ], H9 q: ~: M1 l for(int k=0;k<n_m[0];k++)
4 h/ `, h- ]0 I temp[k]=k+65;
* N5 R0 G4 a+ u3 u, \9 S3 f: I) j for(k=0;k<n_m[1];k++,j++)) [6 w7 f0 F( [/ J
{2 Q5 A: ]% F6 o$ ]2 {0 Z
int t1=0,t2=0;8 F, H! t) k+ d
for(int s=0;s<n_m[0];s++)9 k, B# J/ q( z, H+ l0 P, Y2 I3 o# J
{
! d# U& f- x( x5 O4 K& ? if(temp==re[0])3 F" Q5 J0 o: K0 Z( M7 T5 }2 [7 T6 i
t1=s;
8 q+ I# k1 h+ M2 Q. p" s J if(temp==re[2])& ~- B _, p. ^3 y/ I* O
t2=s;' A7 Q8 e- t, ~3 d' z; z: r* M5 K
}
- ^3 P3 o m' m if((t1>t2)&&re[1]=='<')
' T* P$ N$ I' s1 J {
- e& q' I7 i4 g/ ~" ^+ }& D- ` char t3=temp[t1];
: H: G% J8 T, f+ ^* P* ] temp[t1]=temp[t2];
1 d" `" E S7 R' {, J temp[t2]=t3;
+ _" r4 ?1 V6 l. b' L }0 j+ n' s% c! {$ W* H: [+ |
}
. e/ F4 `2 ]! [ int count=0;
8 i; S( T! p5 A- I. [ for(int s=0;s<n_m[0]-1;s++)! x4 e2 \2 U7 E2 u8 r# _1 r$ b
for(int d=j-n_m[1];d<j;d++)* Y {4 j4 r8 Z8 M3 P: R
if(re[d][0]==temp&&re[d][2]==temp[s+1])
2 t1 y5 G2 ]9 i# ~, \! ? {. q- `" J0 z+ J3 [
count++;5 z; \. u4 z" o2 A+ ~" t7 h5 A
break;
; m8 l1 j! p1 b }
$ Z5 k' K. ^6 Z2 @* [6 s if(count==n_m[0]-1), w% ~9 h+ M) l2 y/ k2 r! e
{+ ^ a0 m6 C! a
printf("Sorted sequence determined after %d relations:",n_m[1]); S; f, B/ K! T+ `. `
for(int f=0;f<n_m[0];f++)
2 F/ I* w- \/ A0 \/ ?# k, I7 r printf("%c",temp[f]);
. @# a. A' |- v' G printf("\n");
& L$ j5 h9 g; p. }! m! h$ \ }
: w1 o' c+ A8 |) F- y* w' t else Q2 Y0 |: S- ^
printf("Sorted sequence cannot be determined.\n");
4 V4 R2 B; q" M' ]$ a& ?+ ~5 L4 V# r: B }. s# f- k. ?% i+ z- e, w* ]; ?
}
( f$ ?1 M6 o$ ? }
, }$ Q& k* L) Q# |5 r3 D! P}. U# p( m% d W* X% I6 E
7 P0 D" Q4 B+ ~
& }0 ]+ V5 f5 z5 g! E5 u4 N" o$ `* K8 L
" h" z8 R6 c( n, W4 Y& y
g9 I' R K, V! W" m
2 m/ M3 G# j7 O. \. f
, [) o9 F) R& w& G! N' @8 N$ r! j: t. o8 `+ v
来源:编程爱好者acm题库 |