本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 % x9 W G) {* u* I% x! q
+ Z n; K2 F% F7 z6 O
Sorting It All OutDescription
& H/ ?( |1 b& K7 d9 ]6 _& ^, |$ f k6 G/ X1 ]1 v& Z! 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. % ^) I8 a4 S' l7 J* O; f& J
Input1 l& C* p8 j* o) ^( b
; K, @( M0 e! @7 \# }" ^
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.
; l* ?: t( X4 a$ d8 ^5 H- cOutput6 n8 j! |) @: w
( \( l& e5 \; W8 o* z# o, ~9 ?) J
For each problem instance, output consists of one line. This line should be one of the following three: & k3 ^( M$ S9 o* p; o2 d0 t
) s& ~% O; i% t5 Y* `& f8 S- |
Sorted sequence determined after ** relations: yyy...y. # ^! |2 E3 Z1 A" V
Sorted sequence cannot be determined. - D- V4 G- I9 O$ k' U% f
Inconsistency found after ** relations. ' F( R. b, d5 r- t* t( N# U
& X6 F3 _$ g' Q9 l! D' ~& Mwhere ** 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. t9 Q9 p% @! O5 i( D2 Y! x
/ |, E4 l6 D/ a* o9 z输入样例
' H! o. W/ y3 x1 O5 n) i1 I" R0 c4 6
& X0 @9 Z) @# F% q+ q! c+ sA<B( f* Y" T: D2 y
A<C
+ L k1 T7 Z, E. i0 EB<C8 S3 y5 Z! v( |
C<D
2 K5 v& q+ D6 ^! @2 YB<D8 G$ a6 X* E8 \% g
A<B7 D! `( u) N" G1 U9 i9 T
3 2
. c7 |" k1 D4 f+ Y9 f& L* PA<B& v6 v( ~; T" r- K9 D
B<A2 ^7 c- k; o" C2 _6 j0 ^9 R; _
26 1
/ X( {. G0 _8 Z" o, D' hA<Z
3 n, @( G1 w g$ c0 0( L# m! @3 l* u2 w1 A; F: k! a
7 I9 K) W' J) O- a9 E2 R输出样例
7 J" y' K0 E) {$ r) S. zSorted sequence determined after 4 relations: ABCD.) h. B$ {$ w2 @7 S
Inconsistency found after 2 relations.
9 P. J7 i1 k, N/ @- jSorted sequence cannot be determined. , [& o3 h) J7 N% @+ V, Z
Source
- t7 S. }+ V) O3 z; E3 g; g. \/ E; M: w) V
East Central North America 2001
! J) b* ^8 n- _. c程序解题1: . p. D) j# z( j3 A' G2 R3 X
//By Jackie Cheung% [3 `( G9 c9 `3 D5 O. h. C
#include <iostream>" J0 K' i8 P) e+ E+ i# S* u
#include <string>
+ w3 T: y+ J. e2 k#include <cassert>
0 S% Q3 Z W x# gtypedef struct tagNODE 5 R; o, D3 H3 [( j
{ H5 q j6 e! D. v) X4 a1 C
char val;
, Z `- e7 a: s struct tagNODE* link;
# l* p3 i- [6 U1 _, X+ R4 r, w. d}NODE;! o0 k# j' Z8 \
using namespace std;
% W; D0 l: {. l$ E, l/ Hvoid Marshall(bool** arrary,int n)
N3 i3 J# Y* w0 o$ g5 H{
8 J; E y# ?; j/ q for(int i=0;i<n;i++)) s! y" `8 @0 H& Q+ j9 o& i
{
+ X; o; [# |. o for (int j=0;j<n;j++)
; t7 z5 [$ i: s7 g9 J! U+ O {4 e1 {" c, M6 N$ Y1 V& y( C
if(true==arrary[j])) z" m- O0 E3 j% ]1 O c' W8 q: \
for (int k=0;k<n;k++); o* X6 f4 K8 q, q
{
) J) m% b; q1 T2 | arrary[j][k]=arrary[j][k] || arrary[k];
& c8 U, u& Y+ S5 H3 I7 U1 |( i/ C }) R- h, p- c! L# M
}( ]7 e' J8 X: A. ]
}
0 l4 i. D1 o x3 S; z+ u};
, }+ w$ z, u vbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
% B. a$ r/ c& D$ D{) E* L5 o+ M+ V
Seq[n-nLeft]=static_cast<char>('a'+nIndex);
% Y1 ]2 ^" g6 x' ]: y bool bFlag=false;
+ `( ?$ f3 j8 F3 X if(1==nLeft)5 a* Z# |) @( [3 H
{
' X2 N- N# e( h X4 ?6 c/ j* o Seq[n]='\0';4 S4 V" R J* I, A, h. T
return true;
" a+ V- q; T- x5 D' b @7 h4 v }
3 F% @# y4 I9 G4 |8 I# X( i3 k for (int i=0;i<n;i++)
4 ?4 z4 @5 i( f9 }4 e+ e" @ {
8 V1 p; f8 j9 T7 n( k% ] if(true==array[nIndex])2 K! {9 S: ~( ~6 I2 z* m4 K9 M) T
{
: l3 a5 W; c* H/ z; U6 Q4 }: a
# b- Q+ Y2 x! q6 D- [ bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
( x6 M* p# L* o }
O$ d$ G) T" e% I+ f if(true==bFlag)4 F! A; L0 A& i3 [ G: |7 ^
return true;. a* m6 T0 Y/ H7 Q. @) U4 x" C" O" k' L
}7 s4 y9 _0 ]3 B& r/ I; i6 R. o! J
return false;+ @' I/ V8 u9 @: y J9 t0 h& M
};
! F7 r* \! K9 [9 }! cint main(). g( S4 Y! g7 l5 {' w1 _
{
1 m# A1 ]/ P+ c0 C! q, [ int nSeqLen;
" R9 g4 V6 {4 k8 ]% I- k int nRelNum;! W! |! N0 M/ Q& m, u
cout<<"Input the length of the sequence:"<<endl;
+ t4 A0 l& Z( U. K$ ^ cin>>nSeqLen;
. O* }3 k- S7 v) \ cout<<"Input the number of relations between the elements:"<<endl;/ b- ?& G4 K6 Y: S
cin>>nRelNum;2 |( a1 u1 J4 Q# z
//1:if nRelNum<nSeqLen-1,then the relation can not be determined!
8 H; V4 i7 J- g* y if(nRelNum<nSeqLen-1)0 ^7 k' s5 y1 g) A0 O
{
# k5 R9 L) ~- J1 x7 r cout<<"The relation can not be determined!"<<endl;: a" c; p/ ]2 V2 T
return 0;" G. H! z1 i0 x) o0 A$ Y: U
}2 \5 n: U1 C# e& E% d
string* strRelations=new string[nRelNum];5 k Y5 @7 |1 B3 f
char* Seq=new char[nSeqLen+1];' ?: [2 I; ?8 `9 Z
bool** array=new bool*[nSeqLen];9 r* N; ~, D& D$ Y5 |/ e4 ?: `
; A9 L' E4 i/ t# d% `, V. i! J9 L
for(int i=0;i<nRelNum;i++)9 P( A- z4 b0 p4 B% b# u
{' ~( q" D" V( O! L# q4 k
cout<<"Input the "<<i+1<<"th relation:"<<endl;
$ L L: R8 ^; F& ^" J- I cin>>strRelations;9 n! ~( S- i" p6 C3 C5 l: |- J8 r
}
& z7 T6 w2 d; p5 {% j. e * C0 ~7 P# s7 D1 g) J! p
for (int i=0;i<nSeqLen;i++)
7 f- F7 Z6 H3 b% g% [5 n% F# k$ X {* l) K0 w! Y9 v2 w# V9 C7 y
array=new bool[nSeqLen];
+ N4 }* C0 L3 c! t for (int j=0;j<nSeqLen;j++)
% w! U4 E8 [ a' Y! M; ` array[j]=false;5 b9 E5 ^; R2 n* S/ m$ f
}
" _7 L4 k; W* w9 v; z* I //The main loop
3 h/ {/ k! ^" a" m for (int i=0;i<nRelNum;i++)9 y7 N0 l1 }. f4 P1 C
{! N# H: x! O- w2 @( H! Q$ p
char a=strRelations[0];* i3 @# ]8 W: @6 Z- o
char b=strRelations[2];# e# b0 V7 N: u
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
5 \2 p3 c. [0 A) ?- \ array[a-'a'][b-'a']=true;9 S# D+ i s( u6 x( G i% ]
( N# s2 o6 C+ X
Marshall(array,nSeqLen);+ V8 U$ S7 B/ h+ i( y
% O7 V" }7 p5 f% q) k. w5 k //Check for Inconsistency after every relation
+ @7 |/ J; z, O% a7 \- G% O for (int m=0;m<nSeqLen;m++)4 K2 J) h K6 v4 Y8 z) W
{& U. K+ p! |; C; G
if(true==array[m][m])
& N# B* o* A, G) v1 ~ {
+ h8 ^/ j3 Y8 E cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
$ ~- ]0 r5 n4 l; d" B delete []strRelations;
" t1 M/ k! M/ A4 e; B" a( Q for(int k=0;k<nSeqLen;k++). ?! F! |8 ?1 M0 V
delete []array[k];
+ x/ s2 s4 h) R8 d( R* { delete []array;8 Z/ |* r8 q" O! J1 b
delete []Seq;
( V- l& A5 _/ u; h' B return 0;" j. Z$ `8 C0 `# a& k' t
$ b; p6 w3 A9 S }6 S+ c6 i2 O3 a& w9 Q+ t+ M
}- S! e: X& c, l8 q& Q* t" X, Y( \
1 V) R- m! @5 Y0 ^
//Check for the determined sequence after every relation . H3 j- z' J3 Y+ [$ Y, P
for (int j=0;j<nSeqLen;j++)" l3 K+ L; v! S, w' C2 |' Y# A
{
' m1 Z! @4 m9 S. {9 c9 @ if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))
* r9 E, P" V% o# w& W {
. n1 h; a" R0 X( ?3 y1 t cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;( Y0 q: | B& s% B/ ?/ ]
delete []strRelations;) K- }& h- C7 ?; X0 y
for(int m=0;m<nSeqLen;m++)
# H# S/ n. }3 c4 ]- |" N delete []array[m];; u: t9 G1 ^7 z6 v4 m
delete []array;, v& S* {; [* i9 g; ]) Y. V
delete []Seq;
; r) d8 N* E! w' ^4 w! ? return 0;+ o. `" f' }6 I/ h6 G4 c" M
}
; z" Z$ w1 |7 \0 `. t# ?% K }; V' ` g4 @# M' X# h4 d' i
* H x* ~8 X( @8 \" F
1 I4 Q! W( L9 h: ?
}8 m3 R1 q7 n) y
//If the program has come to here ,then the relationship hasn't been determined!!!* C% l" G7 ^, o, E0 ]) t3 `- w
cout<<"The relation can not be determined!"<<endl;# }# F' w7 a) y, o
delete []strRelations;& d& Z, @2 l6 E7 x& O
for(int m=0;m<nSeqLen;m++)
) `- X! G# J, c4 x delete []array[m];- |% m/ X, Y5 a) _! I
delete []array;
* F( D: P! U1 M* q$ C8 Q6 i delete []Seq;
; ~3 j4 P1 o4 h; ?: b/ r$ f/ [ - ]% {9 c- q$ T0 H( ]
return 0;
5 R H. }. S9 \1 c+ Y& O}
' S2 x$ D6 a, _. \! ?
' {/ C0 I7 f" d4 _" p程序解题二:#include"stdio.h"/ Y0 i# E7 e+ J p. z) I
void main()
+ q1 M3 Z+ x# s{
5 u4 o9 B. w" e+ C int n_m[100][2];
' n# }3 [% h! ~/ H- ^ char re[1000][3],
2 i* W# a% X. Z/ Y+ G0 | temp[26];
! O- r2 l, C) J- |( b int i=0,j=0;7 l6 I# E/ a9 u2 T/ l# A
scanf("%d%d",&n_m[0],&n_m[1]);
; i7 [8 I. W" c' S( _5 f for( ;j<n_m[1];j++); [( U4 S. J, d1 _1 l" I5 z0 `! o
scanf("%s",&re[j]);
4 _/ E; D1 M; k while(n_m[0]!=0&&n_m[1]!=0)4 U O: E" i/ K& C
{% W( q* x' Z% e0 a: T+ z
i++;& }# X( C( @- F# U) ~" I6 c# f( s
scanf("%d%d",&n_m[0],&n_m[1]);
, d6 e) ?8 e" v5 O for( int f=0;f<n_m[1];f++,j++)4 N- ^, H- M5 l: D
scanf("%s",&re[j]);. U' K* U- W' A0 F6 l
}8 w' @' E, Q4 [1 A/ ~2 i" L" u
i=0;
" O `! Y& K3 g- G: x! [ j=0;4 F0 Y* i& A( e, |5 W
for( ;n_m[0]!=0&&n_m[1]!=0;i++)! f# [9 ?) r, }% }, M
{9 ~! Z+ e# x# p! }* o2 x3 b
int a=0,b=0,l=1;
$ |2 V+ }5 n$ J; Q) W' e( } for(a=j;a<j+n_m[1]-1;a++)
0 w0 U7 Y5 h7 q: ?% ^) p for(b=a+1;b<j+n_m[1];b++). ?* k* b F4 y/ G0 R1 j( s' T, }
{% p4 q. \3 N: \4 j5 f/ 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])7 b' B3 N B, h1 M& a- V2 \/ _
{
8 ~& |. N, @$ T2 }* {7 {1 Q l=0;3 I! E2 t& v! |2 g2 Q$ m$ {1 c
printf("Inconsistency found after %d relations.\n",n_m[1]);3 ~2 N0 O- Q5 T$ n- j- I9 S1 [+ h
break;* Z; \; p7 w7 i% }) L
}2 D* q& K- y8 \' Z& l
}# P# i( w8 `0 {- q: m
if(l==0)
% z2 j; X( ^- W; s" I' Y5 z3 L0 U3 I continue;//Inconsistency found after x relations.
, d% t; a6 k+ M else{4 T: s5 C5 R: b! Q. o* q7 V# }7 N
if(n_m[1]<n_m[0]-1)4 U; _+ q" k- ]
{5 b% a* h1 }8 `5 p2 W* @; r
printf("Sorted sequence cannot be determined.\n");3 l: Q' {6 @2 [; W+ }/ v& Q. ~8 {
l=0;9 ~/ ?4 M0 [$ K% M8 Y0 d
}9 R% _& o; A4 I9 J; m5 B
else
8 f' ~0 [! t4 A5 I$ r4 k {9 L5 l1 E0 h; e
if(n_m[1]==n_m[0]-1)
4 Q* O; K* J+ |0 c5 \ T: b( T. m {
6 F# H) H6 \9 B, F; p6 f( L int k=0,p=0;- E0 {: K0 w5 s4 V: y, @
for( ;k<n_m[1]-1;k++)# P, A( j* J" H2 _5 |. J
for(p=k+1;p<n_m[1];p++)6 _) w7 d/ o8 h2 C7 x; j+ I
if(re[0]==re[0]||re[2]==re[2])6 a1 {! p) V& X$ t P8 \0 L4 [9 w
{
% ^6 P+ h$ }6 { printf("Sorted sequence cannot be determined.\n");4 s9 w1 [' Z8 l& f
break;3 S8 r. q k) ~' W# d% `5 ~
l=0;4 v( D* M; y. X7 _
}
: U8 k/ m4 j" b0 z) u# ~ P }! n+ E) n% L8 l+ U; R
}$ i W. Z% H1 n+ R2 c
if(l==0)
% R' D* @4 {9 b. Q continue;//Sorted sequence cannot be determined.! T0 J* T* o5 y4 D p1 g
. E5 H1 Z& C9 N- c: F8 A$ Y else2 s1 {+ Y& ~8 F6 k4 ?" @
{
& o+ [# r1 h( L& M2 \6 u + O0 r) v! ?7 q& ?- j G( Q, _9 G9 w
for(int k=0;k<n_m[0];k++)8 ?/ F. Y3 Q1 I) r! g. y
temp[k]=k+65;
9 _1 h1 a2 g- s9 K- f for(k=0;k<n_m[1];k++,j++)
" e( s0 f. m$ F; e9 y2 J! T2 F {
1 {- D) S) ?; E" d int t1=0,t2=0;
- g6 J7 }: p5 `3 v2 G; q1 B for(int s=0;s<n_m[0];s++)
% ?; K# K$ G1 h1 |/ p8 m) w {
" `8 F; I- y7 S/ V! D8 H# m( B if(temp==re[0])( P" o) c4 f/ |, h: ]8 F
t1=s;
% ~+ U0 P | Z/ [& t if(temp==re[2])
" v$ R2 r+ c* O+ x, p! F t2=s;
* U- e, ?2 _; n3 L- `9 G, h' U }: F) y6 T: I I" F
if((t1>t2)&&re[1]=='<')2 f y w9 s/ ^: a3 _3 f$ O4 F5 d) Z
{
4 o+ v' N! t1 w4 e char t3=temp[t1];
/ g3 r; ^) l9 G+ p" w temp[t1]=temp[t2]; O. C& \5 b8 u7 L
temp[t2]=t3;' K+ o, P' c2 I( B2 M3 `
}
; A& T5 _& p0 G0 o B G }1 r7 y% O' b5 z+ O
int count=0;
" O' k7 F# s4 D0 y6 P' r for(int s=0;s<n_m[0]-1;s++)( P; R; D/ {- n4 h& t9 J
for(int d=j-n_m[1];d<j;d++)
) O" I! Z3 F+ S0 j. `" \$ n' Y if(re[d][0]==temp&&re[d][2]==temp[s+1])+ h0 _8 H- j+ d" A6 m ~4 K
{6 R0 S; j! D5 ^) P9 S1 j
count++;* L' c0 g+ q# b1 o4 u" s8 H
break;
8 G" n' O- l# q% E$ y3 P7 |0 A( Y$ ^ }
+ R7 Q& e* {# \ if(count==n_m[0]-1)4 I4 I5 E2 ~! `
{
8 u0 M5 S$ q' n. d0 b7 {4 s printf("Sorted sequence determined after %d relations:",n_m[1]);# a; d* b5 v6 t3 f! i" ~% |
for(int f=0;f<n_m[0];f++)
+ h7 l9 t P, J0 b9 a printf("%c",temp[f]);
2 }3 |! g% ~) K& f2 V1 S printf("\n");
9 [" `* [) D) A# s' E5 }9 p }
8 O. V3 f$ y, ]1 O! v2 t else
1 d1 a1 |( t) P( N& U) ~! I printf("Sorted sequence cannot be determined.\n");
# ^- w4 ?+ J5 |# a! a* ?5 N }& p" w. `; @' C8 q4 K
}
3 i; D; G. W; V _) ^# W" w0 m2 t4 q }
1 Z+ s: ?( d3 a4 A3 v l) }}$ B9 [" m& M! N8 s/ j0 @
3 O3 y- ^) F$ @& m* ?$ c7 y5 k5 K0 \1 }
7 G/ j; R% N9 K6 _' X2 g# Z3 T
# k" a6 P- v6 d. A/ p
" L0 G) e3 N ~' N V
) W( L" A2 D# j% R6 q
1 Q. r9 }; T; B. R
4 V Q2 e, f Y. T& J+ N# S来源:编程爱好者acm题库 |