本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
2 |5 t R& ^2 ~" T6 s! b' ]! k p$ X" }
Sorting It All OutDescription
) I- m5 @5 w# {
( |* b8 f1 G, Q) yAn 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.
$ Q* y$ i0 k* C9 A. j" Z1 t( j! gInput T. k" H' l4 S7 m4 e
4 R ~& b, k' T, a& {( d0 Z
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.
7 u! _* G. C# p% ?/ [& IOutput0 l1 h( y1 T, P" H+ _
8 ?8 {) X9 l# R; K* JFor each problem instance, output consists of one line. This line should be one of the following three: 3 ?. i" \) {3 E6 P# @
) O: h( u: H8 r$ y# e* C
Sorted sequence determined after ** relations: yyy...y.
9 N% h S4 b3 z# W; G& C* }Sorted sequence cannot be determined.
) a, G7 `' U# Z% u6 |4 gInconsistency found after ** relations.
) Z9 W. }2 S9 N5 n$ R: V+ H) m T( x- 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.
/ x( X7 ~# F( A
9 }2 C4 N2 b6 }- H: B输入样例
; z. S) e. C6 n# Z" H6 q; s4 6# ^' g$ H3 \9 S4 p( c' E
A<B
- g. X8 j, ^. y9 ?' dA<C
, L; Q( o' |8 h! w8 K" w& }- ]B<C
% x3 |5 Y& j. A0 ~" o8 F$ D/ A0 M% AC<D
' o/ z: h" C7 ^$ L" M5 X0 EB<D
# M8 s. @3 J7 a) q/ [- VA<B
1 i5 q3 r" q& P& v' A/ O7 t3 29 ~; B* A7 d) g6 W$ H3 ~9 l& G
A<B% A; s1 e( f& _; W @# i
B<A
! x, N1 E( c2 y9 S+ u26 13 X6 z0 F, C$ Q3 P9 X9 k6 R; G2 t
A<Z" G9 Y0 ~ e8 W8 t$ L o
0 0- S0 y& R w8 x( m
& b2 d( ?2 T7 d' H6 Y/ s
输出样例
& a, @- z( `/ |, f: z1 V) ASorted sequence determined after 4 relations: ABCD.2 x' n0 G1 _$ ]- U; ~4 U0 q
Inconsistency found after 2 relations.
7 L( E2 Y$ W% A! wSorted sequence cannot be determined. 8 O1 a& ?' q' a6 m2 z
Source
$ x" ]/ ~" x Q4 U/ o0 r% C6 }. F/ L/ b- A
East Central North America 2001
* p9 L% c" |$ R, X$ R& u程序解题1: h% G. h) M" [/ m- l2 c+ l9 B
//By Jackie Cheung
1 c2 U% D& `# G+ E! z5 b#include <iostream>2 A+ c8 E; ` Q2 ^) C, ]8 c0 l4 ]& _
#include <string>
7 v" M0 g/ C0 g$ U#include <cassert>
y7 u B' H5 C6 p8 A3 ^3 Itypedef struct tagNODE
- N0 p: [. z/ N; C$ }# V3 F7 B{" \) T% s4 Z# O1 k9 Y }1 i, |/ R
char val;- T6 c& i7 x0 V8 c
struct tagNODE* link;
' ]% l$ Z: ^" f9 U8 T) Q}NODE;
% _4 y1 t5 g" [7 t: v% J* Vusing namespace std;8 v7 R; |: d) L( G' I0 Y8 ]
void Marshall(bool** arrary,int n)+ A/ y! d8 H7 _% q; S
{
7 c* g& }* w& C for(int i=0;i<n;i++)
3 ~% ^5 A3 h1 m+ c; @" R {8 O& q( ~: r7 L _% d
for (int j=0;j<n;j++)* A! {4 s8 {) L) v" U9 `5 N6 t
{
6 V" l! P! c3 R if(true==arrary[j]): `) D0 z2 ~" `- Z
for (int k=0;k<n;k++)
8 I& E, `# X' u4 U- b3 @' I4 t: {$ { {
3 i( p1 }% D) h8 N; v/ w" P6 K8 T arrary[j][k]=arrary[j][k] || arrary[k];0 V3 K; v9 V0 T" U, T( {
}! N* }+ I0 L# x% V
}
! A) {6 F6 F1 w2 G2 R/ x; v }
' c6 j' ^! }6 R};
8 `' D, Z! \) I. qbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)% x( }0 n9 J4 T7 l; s( G
{
1 Y+ z4 f3 B; B9 ~% P3 t' _& o Seq[n-nLeft]=static_cast<char>('a'+nIndex);
# L8 t4 ^9 H. E8 L ` bool bFlag=false;0 D3 X, M) J3 Y" a+ k* Q
if(1==nLeft)
% U$ G, n) Y1 t$ k8 u: K1 T6 w {, Z" m" ]( x1 M1 @* ]6 _. u
Seq[n]='\0';7 ^" S! f$ e0 H% q
return true;0 t6 A" |( w8 X. `/ b# z
}; `+ s% [. c- F; r \; B
for (int i=0;i<n;i++)
7 Z, F6 s8 I. s5 ]( | Q0 k {
6 ~9 R9 ]9 x+ H/ k if(true==array[nIndex])+ I$ e# H) t" V! F, s, j7 B: I
{' }- ^8 u3 q y6 X
) p d' D- G6 t. T! h
bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
0 F- a2 b* K0 }$ U& N# o6 [/ S( ^ }3 F# i2 I" U& J' @: F
if(true==bFlag)" Y$ E1 W3 ^0 O" h% W/ [
return true;
7 z, |; i; K, U; J& c }+ H: X4 P4 d! D9 z" b6 @
return false;
; Z3 \. K9 j/ ]4 g! k4 W8 s};3 U: h6 G/ \$ j3 L
int main()
F! m+ v7 x3 g$ `% W& z{' l" f. F9 P) ?5 \+ ~* E- P
int nSeqLen;$ o7 L h! ~% ]
int nRelNum;5 r$ m7 L. ]+ f& h
cout<<"Input the length of the sequence:"<<endl;7 Y% @- n+ W6 r: {$ g w7 l
cin>>nSeqLen;- l" u: G# e0 x5 g7 R- {
cout<<"Input the number of relations between the elements:"<<endl;
6 w. ~4 L( }6 w; D0 e6 V& W cin>>nRelNum;
$ z6 C$ u( f' A* r8 D; ~( U3 r //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
2 t" N" U+ z: O5 D! h4 u9 m% _ if(nRelNum<nSeqLen-1)1 M8 R' z! L U" W
{, ^/ e7 V7 u. n* C
cout<<"The relation can not be determined!"<<endl;, b3 P& u/ i- G
return 0;
5 ^2 L4 a, A8 d& F$ ~4 M }+ x- K" c9 [: m5 }: V1 a
string* strRelations=new string[nRelNum];
& T4 m- C/ x- ~: v2 g: |+ S) s# D& E char* Seq=new char[nSeqLen+1];1 z: C w b( M% I9 f% \. e# k
bool** array=new bool*[nSeqLen];; @7 u) W# n( m: [* L6 Q; A$ X
9 H4 S4 e2 W- w# D- y0 H4 f p% z
for(int i=0;i<nRelNum;i++), ~, d4 C- v/ {5 j+ a
{
+ R! o* P' a+ x+ O4 ?6 @/ a cout<<"Input the "<<i+1<<"th relation:"<<endl;; x4 H, E+ o4 D' j
cin>>strRelations;/ E% G% C1 M* v+ e
}$ K; n$ `7 `; B# Y6 v8 J1 }
; `" ?+ g* V& R3 g) T
for (int i=0;i<nSeqLen;i++)
5 r; `/ X3 e) ?! e5 R- j5 q; d4 i {
( e, _9 v. V, V! m$ `$ H array=new bool[nSeqLen];
1 b4 V$ H" D0 g9 b+ n for (int j=0;j<nSeqLen;j++)
8 N$ u; {. z& @ array[j]=false;
) B1 j! V/ E# M3 N! Z& F }7 P3 j9 @* ^( }5 p8 J
//The main loop
' I% F4 Z9 U* J0 n6 D for (int i=0;i<nRelNum;i++)3 b) }" t2 U2 y1 d/ c- x
{
! z3 x9 z: K$ O" L" ^ char a=strRelations[0];
' c; p+ ~) l" u; k char b=strRelations[2];) G$ l0 b- _" F( `7 x
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
1 S* e$ C8 M1 R& p array[a-'a'][b-'a']=true;
( X/ }0 Y- F' \3 s+ Z/ M; r+ _/ p1 Z4 g0 k
Marshall(array,nSeqLen);+ H$ M$ _5 }4 D, k4 q8 G5 `1 h
$ F) j9 L; o* g, X4 t/ T+ D9 { //Check for Inconsistency after every relation2 _- O2 H6 v) e$ S
for (int m=0;m<nSeqLen;m++)$ \" ~' X9 ]) i7 ~* u7 h# U
{7 l5 B% @2 _% s
if(true==array[m][m])5 W" y" f- @8 y4 Y5 w+ k
{6 u6 L {0 ~$ ~! s% k
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
0 {2 M O- _2 p( h) {0 A delete []strRelations;0 H6 t" E, o* n. X8 }' ^
for(int k=0;k<nSeqLen;k++)
5 L3 N( e3 t/ ^7 i delete []array[k];
" T3 i0 f4 ~' e% x delete []array;
+ R! j# V& I: N1 X. P! o C/ D0 ?# s+ x delete []Seq;7 Z. c- e( b* y) k+ d
return 0;
& d' d( N! W# ?' t4 @5 }! e
% x; P8 w+ Z8 F, v, @ }
7 d$ Q1 t# l6 A, m6 N6 V" ] w! z }
1 ~/ A; c- V/ _& H7 G! M9 m, N" J4 N. m9 L) F
//Check for the determined sequence after every relation
2 i% h8 | [% s: h( ~, A for (int j=0;j<nSeqLen;j++)
; e. L+ c1 t2 \5 n* i' `9 q( Z( S {
5 [& \ e& G$ C- k' i( D if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))' d9 F0 I: v6 F" U( x# M
{' y' {1 R! d* s6 N+ R/ `: x0 Z
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;" d+ w& f# p& o" [8 C' @& X
delete []strRelations;$ I% E! r2 @3 X& [9 d* x6 r
for(int m=0;m<nSeqLen;m++)" P* v! c) F8 u- K9 E( L
delete []array[m];
) ^( f" @% Q# [. n: y( x2 w; p9 f delete []array;
- X& \$ d3 b& b% e1 b( b delete []Seq;) @6 m" u$ K3 W, ?8 h. Q+ P
return 0;
# T, ?; _% D9 Q( ?$ D }
0 b1 L! q& O; V+ N+ f& w1 ? }
1 |, W, ] }4 S- Q
- O9 ?6 F; q) s6 n5 K5 {: E" M9 x7 e% I# t6 b7 X
}
0 F3 R- A, r0 ^! r; Y: z s% f //If the program has come to here ,then the relationship hasn't been determined!!!
' L0 X% @0 m/ P1 c9 O: v0 G4 D* W% M' F cout<<"The relation can not be determined!"<<endl;
% B. N8 E4 q' @ }) f4 e3 o* H delete []strRelations;
# V& ]* ~6 k- l4 _. o' U: T: | for(int m=0;m<nSeqLen;m++)5 t3 @0 N. W* m
delete []array[m];" K+ ]2 f# o6 d& a
delete []array;0 v- z/ Y( |/ B- A+ D1 T
delete []Seq;
1 i$ b- o# x7 T' u
, p+ _, Z7 z- V5 ?- b return 0;/ `/ [( h; W( H
}
& d6 Z) A9 u0 P y+ U# f( X9 c; M3 q
程序解题二:#include"stdio.h"4 R3 {) w% i# }' |' |
void main()1 c$ A8 Z, U# L# T8 Z/ K
{
# {) B, P" r( C9 T6 M" x int n_m[100][2];: }* ^+ F* j! V. ]& m! t' r
char re[1000][3],7 {+ c# |1 _. j4 [! N8 @8 |
temp[26];
6 I6 c$ E% T3 t1 I int i=0,j=0;- q4 H4 L; X$ |, G6 W" P
scanf("%d%d",&n_m[0],&n_m[1]);
: p2 a2 y* ]" T T! C9 G: \3 | for( ;j<n_m[1];j++)
) j* J# n2 V" H* k) U6 _3 e scanf("%s",&re[j]);
3 H4 @1 Q/ a) @2 o( S/ N3 ~) y while(n_m[0]!=0&&n_m[1]!=0)6 |% M: ?6 m; Y4 k
{
0 B& [. b' r4 C {$ N2 {5 f6 J% O( x i++;+ |- @- H, v7 k; L$ O7 ^& `- {- x
scanf("%d%d",&n_m[0],&n_m[1]);5 r' `# t" P/ n
for( int f=0;f<n_m[1];f++,j++)
$ L' |, C; u8 K" t2 ?+ Y scanf("%s",&re[j]);& g) v6 b: P4 T9 D( R u9 b
}7 F4 n, q, E6 Z7 z% C, a$ }
i=0;
/ l% V# X7 [) D' B" k j=0;
3 C, {7 k5 i. e7 V for( ;n_m[0]!=0&&n_m[1]!=0;i++)
" c3 W7 e& K; I) ^7 g$ E {& `6 [7 V y8 m2 {; K" Y: ~
int a=0,b=0,l=1;$ I) x# g) l+ N7 Q
for(a=j;a<j+n_m[1]-1;a++)
- T/ ]7 Z$ C/ R; `2 I! p4 @ for(b=a+1;b<j+n_m[1];b++)0 {9 n6 H7 S0 p# E" a4 P0 R
{
( O! _5 n7 q: } 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]): W' _$ e+ X5 T+ j* }* r f
{
+ m2 D! `( h2 Y, K l=0;" g5 }6 u) c% ~, r# i4 H6 m ~
printf("Inconsistency found after %d relations.\n",n_m[1]);: A" t; ~2 u' y3 H- n
break;+ B" i1 Y* W, E3 ?
}
5 n# {3 S- M* m. m* n' N }
. q& H$ N& P' h1 r$ Y' N if(l==0) : |$ @ j6 k% X& h' C, H
continue;//Inconsistency found after x relations.) c( ?! g9 W( ~3 j0 A2 k9 M
else{9 V ]( ~- h0 _# _9 d3 t
if(n_m[1]<n_m[0]-1) T E; U$ v& [' c! b* k
{
9 [6 x7 S' H. }6 f8 i5 ~5 | printf("Sorted sequence cannot be determined.\n");1 b; b9 V% V5 S! t
l=0;
$ A( T8 M1 n3 y/ H& d }
; l1 v% t r6 o) e else% u+ e* Y6 j E# g1 X
{/ y- n1 e2 B! V, L% k& L
if(n_m[1]==n_m[0]-1)
8 e2 f2 K: @! {6 m1 |* q7 i {
5 t9 M! }1 g( ]. q+ F$ k& `1 ^& T int k=0,p=0;6 m! g9 p0 g) [4 I2 ?% w
for( ;k<n_m[1]-1;k++)
% n8 E$ Z7 ^3 \- m: y4 E+ W1 u/ z for(p=k+1;p<n_m[1];p++)
9 |4 s8 }% @. J if(re[0]==re[0]||re[2]==re[2])
; ], h0 L$ ?/ q {
* G( E5 C; _0 Z% I8 f: a. u* _2 E printf("Sorted sequence cannot be determined.\n");
' K: p( b/ z6 t: t( y- e, i+ F break;" |, y- E2 f& I2 m5 {, K
l=0;
0 t2 _" x( D' [6 [' u4 e }3 W- v9 v- o/ R1 M; b7 O
}# h6 i- |5 E6 \5 d& u* L4 N
}
* f% h0 e3 o7 a; m* G1 x& z4 F if(l==0)
. m, S2 u ^7 c) ]. O* K) M4 Z continue;//Sorted sequence cannot be determined.9 X6 L2 h' Y' i6 W! `
v" v6 t* |& L2 `
else
2 h( i# @0 C4 Z0 v {! l+ P/ T0 B6 i4 `
9 T/ a6 s' r) l for(int k=0;k<n_m[0];k++)- ^ k. @: h' s
temp[k]=k+65;
C! Z) {+ w; } P# D+ J b* a- N for(k=0;k<n_m[1];k++,j++)4 ]3 V6 b+ K3 _
{
, D E4 R i0 _, G5 Q# T int t1=0,t2=0;
) a) B; F6 P, |, ?) L for(int s=0;s<n_m[0];s++)& u0 q; m. j3 R5 e' ], e' ? k. N
{# u \0 V8 D1 k- O/ X" U0 _/ n: v
if(temp==re[0])
* Y% `4 r4 o i: y t1=s;& Z3 T/ e& c& @ p8 ~& N
if(temp==re[2])* q) Y3 ?9 o* }/ b
t2=s;& J r! Q0 G, h$ b
}
6 `$ `$ R9 s9 A/ b if((t1>t2)&&re[1]=='<')3 q% m- ]; Q9 U( B
{
& A+ d0 F1 Y% j0 b, g char t3=temp[t1];
, J+ z1 S. s. ~6 j% u4 B' X temp[t1]=temp[t2];% Q) \1 Z3 T" F
temp[t2]=t3;
: u' ?3 {5 R# [! @/ a4 h1 p) g; @ }; V7 d: p3 U( \
}
# V7 q: E) \( P$ I; L- k/ [ int count=0;
' e% e. J5 ?. n+ ^4 _' Z5 N9 X7 t% j for(int s=0;s<n_m[0]-1;s++)
2 u' s2 L2 D3 z for(int d=j-n_m[1];d<j;d++)+ c3 d1 a) R- o
if(re[d][0]==temp&&re[d][2]==temp[s+1])
4 _$ e. X9 i0 k& b {2 I% U" x, [: {, `4 n4 T( N2 E+ `1 Q
count++;
& R; z Q; \5 Z, ^2 W break;
0 V' Z/ [9 i% i }" }+ A- P2 m/ b, o; X
if(count==n_m[0]-1)) J9 f1 u: j# {, e: A6 M/ w
{$ i' k$ v! c* v6 M, y
printf("Sorted sequence determined after %d relations:",n_m[1]);5 k% V( u, J1 A# T
for(int f=0;f<n_m[0];f++)1 q7 h5 D4 V Q2 ]$ ^
printf("%c",temp[f]);
r& Y) D! A% v' u6 Q printf("\n");
% `& A _, P$ M- E) l& n8 \ }: U: B/ ? g3 m; [
else
8 V, ^" S& X. a# g& Z printf("Sorted sequence cannot be determined.\n"); - I' H4 ?( G+ ^7 k5 K
}
) d; Z& v+ T \ }
2 ]( F# u3 X$ i) z; Q }0 {! t' d: n: t5 O. \. B
}; T3 K/ e9 h( s& @
% k. v6 k; j( e& s) t* o5 P
! z/ \9 c6 m6 A7 Q# L$ v L2 T# a, h% O# P# B5 y
, W# `/ e) ?2 @
) h) v- U g: [( L: ^8 L& d+ T# V' ^
J: r* v' O; B6 }8 ?
Y P/ M2 g3 W! c
4 I2 `2 I Z7 m* y# a3 s来源:编程爱好者acm题库 |