本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑 ; w. u, h5 K% _0 B, j
# @, ^5 Q$ i N$ H7 bSorting It All OutDescription
( {# ]3 L& e, u9 `& Q) p! Z9 i, Z5 M& [# v5 F; `9 P
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.
$ p8 _! W, w) t( M l; q- F5 HInput
4 `7 Q( \: c3 ~; y; L5 F
& r+ A j2 o1 Y5 c$ Q- eInput 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.$ w! X O6 q K# e$ p, D( {
Output
+ ~9 r/ A W1 Z' y- ?" J" I
2 O/ Z0 L0 }, V" n! E5 @For each problem instance, output consists of one line. This line should be one of the following three:
. f y7 C/ o' {1 z. t, u; S, i0 o' B
Sorted sequence determined after ** relations: yyy...y. 6 B8 b6 I; P8 R+ T) w$ Y( i
Sorted sequence cannot be determined. - w* `" i( k( [" Z8 [, s
Inconsistency found after ** relations. 9 s; e7 l0 r" f5 m1 N" k
5 m' f! M) Z* _( U' _% X1 x0 Qwhere ** 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. 0 ^- I9 {% R# ~% ` `5 u
8 b( k' c. c5 H# h% N/ ?
输入样例 " ]0 @) D/ B ~' w2 b
4 6
1 Z5 l: \* h5 ]7 f. l' v' bA<B
; M) Q5 m* I; }7 R, pA<C, e' S0 i, e% B4 `% i
B<C9 H* O/ ~/ J* K- j0 d9 ]2 f/ g
C<D
U* t/ t. [7 TB<D5 e' J* A( k; H9 D' z
A<B0 H1 o* f1 w: t' G
3 2
2 J. f/ s& L" ~2 B" I1 ?A<B7 W. p' ^: @+ W* m+ D( Y
B<A
2 X* a# F6 J$ E* c26 1) Q7 V& C; y+ K2 s5 ?
A<Z) f6 _, J! Z' U5 [, H& W1 |5 C
0 06 d+ W: o# I, }7 s% J# E
+ I) D2 Q* i# |6 J, n. r; n
输出样例
' t6 z* P8 A9 R& Z) J. F# i$ W1 xSorted sequence determined after 4 relations: ABCD.. Q( r/ n4 j3 V. p
Inconsistency found after 2 relations.
2 w9 D! ^1 _: C) P4 QSorted sequence cannot be determined. ; Y' P, M/ |0 q" W; b& I) b" I: U
Source
; {" C' d; D8 x2 z# f% \( r
# f3 C/ M) M6 D# v- t1 REast Central North America 2001 / r/ y0 a: W9 L
程序解题1:
* q( n4 q6 v1 s# a% F$ u//By Jackie Cheung& b" l/ ?" e4 r1 ^6 I5 A
#include <iostream>
+ s: U6 D. T4 E2 B+ U8 T#include <string>
" x' q6 q: G8 u+ J' t' c, V#include <cassert>/ _/ C3 Y" K0 m& y1 y( u- Z
typedef struct tagNODE ) Y; r* h' E/ ~
{
8 Z- a( Y Y3 F& ? char val;* e' C! _5 \1 b8 I! u. _
struct tagNODE* link;
/ O9 c1 @+ G* N- S}NODE;* e5 q* A" g5 c+ e7 t: c8 i$ X
using namespace std;
; n& q8 v. A f2 u2 T( Zvoid Marshall(bool** arrary,int n)8 \/ B5 y# k5 e/ g% C
{
4 D4 F9 j |' p. T* i( D for(int i=0;i<n;i++)
* l$ l& T. y5 N" l {! l T7 z( O) d, s- e
for (int j=0;j<n;j++)
8 l- Z E) K$ ? {
; t$ h8 K4 p! B# ~9 S4 J if(true==arrary[j])# O( e0 j+ }& a/ {( h' N/ s; v
for (int k=0;k<n;k++)
8 Q: Z; ^4 i' x8 c. B/ X n {
7 V8 F/ o% o9 u( P L7 S4 {4 }; q9 D0 p arrary[j][k]=arrary[j][k] || arrary[k];: d7 ?0 g* s" H1 R
}
2 c% `! g$ D+ g( {' L D }
5 U! w) B4 ^: x/ z( v+ W }9 b( D+ J* H- t- K/ l
};
, f! A9 [4 i1 r, Y I7 rbool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)
: y: j C6 f, z$ P( E9 K. [6 N{
7 q. N- S. C9 R* |7 p Seq[n-nLeft]=static_cast<char>('a'+nIndex); |: C& X( [0 \" |0 U% S' \: X
bool bFlag=false;& q1 y# L2 T7 y Z+ p) R
if(1==nLeft)5 {7 X" s5 ~7 d- f
{8 e* o1 }6 b. c. \+ O
Seq[n]='\0';% ]" p- J+ }' C/ p
return true;. N+ O; L4 r3 y+ k
}% f! F3 r2 _( W! h" x
for (int i=0;i<n;i++)0 g/ a& W# b1 ?5 b# l5 I! W( x
{ L( T+ d+ L( f, I
if(true==array[nIndex])
$ Y, u8 W$ U& D8 j {0 p, Q" H( N1 \) p- c
9 g% I2 I2 ?2 t bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);% b+ t: I* q- b
}
, u0 q \% ?% j# d( f+ a if(true==bFlag)9 }) n: z0 i! i0 a
return true;+ |4 F& G1 Z4 n' D% i5 F1 p
}
! [. ]' I0 D7 y0 r2 c2 k return false;
3 t; ~7 a! y( X};
$ O; D$ a+ J1 m. N* nint main()6 y& p0 R9 p$ h
{
- W8 H7 v" u, a4 p M int nSeqLen;
, w7 \) c& a+ f int nRelNum;
0 k: C s: V, ]. y; J cout<<"Input the length of the sequence:"<<endl;
* z7 m; ?( |. D4 I* \ cin>>nSeqLen;
/ b- n' y( M A7 L cout<<"Input the number of relations between the elements:"<<endl;% q% h4 O- b' \- s/ G1 i
cin>>nRelNum;
0 L# a( C! C: V" ^; @1 F //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
, h$ H" H1 K" `" E+ r, B- p1 n if(nRelNum<nSeqLen-1)
' L; b9 A) I) E/ n; V- u8 O {
* A; E( A! i$ p" D9 c cout<<"The relation can not be determined!"<<endl; Y) @5 ^" W- x2 _. w8 P, A
return 0;
6 [4 o1 w# S3 L0 Z# ~ }2 i! X: L. [2 t0 k
string* strRelations=new string[nRelNum];
5 A+ L4 h/ J8 s0 O char* Seq=new char[nSeqLen+1];
2 J: c# L9 t+ q) n/ f8 E bool** array=new bool*[nSeqLen];
# ~0 C% S9 b8 h# f# s# x
. Z2 U |) ?# k1 k! C% ] for(int i=0;i<nRelNum;i++)3 G3 M0 }! y$ A5 ^
{2 s& [5 }8 `' A- z/ I; t2 R
cout<<"Input the "<<i+1<<"th relation:"<<endl;6 [( h# I+ k' ^/ I8 b
cin>>strRelations;
' q% M, Q, y2 b. P+ w }
4 A& Y' h" Z# L6 E7 A7 S : d0 g4 g0 X" t+ Q+ a
for (int i=0;i<nSeqLen;i++)
7 w6 L0 ]! q& U! w {& M' _0 ^& O# Z
array=new bool[nSeqLen];
w0 n7 T2 q& @& \( _9 G for (int j=0;j<nSeqLen;j++)% n* n; ]4 Z. U4 A- W
array[j]=false;
# i0 j, V+ [4 @ G' a( n! p }
j2 ^- j" y; G$ Z/ S: O! I2 n, A; | //The main loop
5 w2 g( s( V! n( x6 O6 R for (int i=0;i<nRelNum;i++)
: l! ^8 ~; O$ R7 c {
9 w. O, m2 C% n3 Y* q- f8 n char a=strRelations[0];& S. _" y$ q3 H" p- {$ {; I# P6 Y
char b=strRelations[2];2 j! h f7 g R9 { ]( ~/ z. S
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);/ @6 {* m5 X( `/ X, v0 G
array[a-'a'][b-'a']=true;
[* S: l+ c }9 H% P8 [! [- V" L9 O/ v* L; W8 T/ Q! ^
Marshall(array,nSeqLen);$ [: _$ c) I! A) F
* o# Q& W" \, a4 O/ W6 V* j2 f6 [- \ //Check for Inconsistency after every relation
' q( k8 F# r8 T2 n1 k for (int m=0;m<nSeqLen;m++)
1 D& c" Y9 g7 d {& x' M0 `4 W' G: H* \& r
if(true==array[m][m])3 G9 u. P: M3 e* g, Y( S
{ ?! d( o( G% T4 `" \$ q. `7 d
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
+ h6 q7 F5 w3 r8 S2 O9 Q delete []strRelations;
8 z2 p9 M) t( q0 C0 p for(int k=0;k<nSeqLen;k++)3 L* v/ E0 x: b
delete []array[k];
: P9 N4 J( \4 D" I6 ^( g' i delete []array;6 c8 A- v, G' \6 M8 P4 W: ]
delete []Seq;( F8 \0 @0 n0 e. O9 B/ I. i0 ?
return 0;- O+ ^4 c3 c/ M
4 w2 R1 z7 ~6 s1 ?8 l- ] }
1 E2 U f- w' L8 S/ Q5 t; q }
8 x1 A, e u( \8 {- E: q
$ o( `% ^; m1 G& x, K8 @, b //Check for the determined sequence after every relation
! c/ D, H+ [8 K/ U1 e7 X for (int j=0;j<nSeqLen;j++)) d% V, Q7 [8 d/ r
{& K2 ]( j! j& R) q
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))7 Z+ ?8 U7 v0 l X9 U( r; b
{' m2 \2 d9 a; t! F" k) O, o( Y
cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;
2 p2 T* n" }8 z# {% f delete []strRelations;
- F: e# e' Z$ [4 a7 m% Q ~3 M for(int m=0;m<nSeqLen;m++)4 [* S1 j7 t4 X9 ~
delete []array[m];
" W8 D; V6 j) H7 n+ b6 X3 I1 ]& A delete []array;* `' } C# @6 m# A8 v$ _6 Z
delete []Seq;8 L- d: L, M, A: O% S* C$ ^
return 0;
/ e" v* m Q+ K# M: V" ] }: _! d# P) ]$ [
}' f- x% m6 t2 Y. V) c, T
^) r+ f& U0 T6 w
O: F: }! R" V; m$ i( I# ~ }/ W7 n, _; P% x$ U6 {' M3 a- L, O
//If the program has come to here ,then the relationship hasn't been determined!!!; b& \! ~3 n2 s0 u G, q' H6 U6 f
cout<<"The relation can not be determined!"<<endl;
) b" A* u% ^0 J! i& p1 s delete []strRelations;
* `# `- Z0 Q! f% I) d, ^& w0 M for(int m=0;m<nSeqLen;m++)
! U: V5 f5 s. g+ T; \* i: P delete []array[m];
( r G3 Z! B/ ^7 Q) ?0 y' p delete []array;5 \5 s/ M" l# a P% r
delete []Seq;6 F- G2 p( |! T3 \/ L
" o4 ?/ B) u6 y6 U
return 0;/ J' U, z! y* f( c O8 b
}
! k B7 W0 y0 q, z, x9 ?# T" d @/ p! G1 E, E! z$ T( E) k0 ?
程序解题二:#include"stdio.h"
7 K. N$ z! o6 Q0 I9 T8 {/ c9 Nvoid main()3 b8 \# Q/ X4 e) g9 d
{1 ?0 E3 c" r0 X+ w; D4 h
int n_m[100][2];
! q \3 D$ C7 s4 T- C char re[1000][3],+ I3 Q: V7 V" e C% i6 r2 _. w
temp[26];; S3 b/ g/ I3 k8 v6 R8 k3 V6 H4 V
int i=0,j=0;) N. _2 f+ s! P0 E, K% Q- @
scanf("%d%d",&n_m[0],&n_m[1]);
3 T0 S9 j9 B/ Y' s0 c0 T for( ;j<n_m[1];j++)
4 H" y0 S2 F4 o& [- |+ M% c scanf("%s",&re[j]);5 \* q m5 Q- J$ e7 e; ?1 }4 K# k
while(n_m[0]!=0&&n_m[1]!=0)- N; g0 u; [; W8 [
{% V: E5 f% G5 e( x3 k
i++;4 j1 e. H1 n T/ f
scanf("%d%d",&n_m[0],&n_m[1]);) V) i) b& ?7 |
for( int f=0;f<n_m[1];f++,j++)8 B5 @' c: ~6 Q n
scanf("%s",&re[j]);0 s" r& |) L* I! n, y7 {
}
p# {2 w- _7 K) Q i=0;
2 M$ Z7 F: l2 @% ^0 Y' x+ [ j=0;
3 q/ G! E! C& i$ L for( ;n_m[0]!=0&&n_m[1]!=0;i++)3 o! J* }* D3 q, i! b* h
{! y/ H; S1 _' e7 T. p+ i2 i; j; x y
int a=0,b=0,l=1;
( Q. j, Z: a4 k" X6 ?4 X for(a=j;a<j+n_m[1]-1;a++)+ q5 N. y! d7 r, q
for(b=a+1;b<j+n_m[1];b++)
. p7 T6 \( c- u H4 L {9 V+ @ A+ \8 P7 K+ C6 m Y+ k! A) k' i
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 @1 A- d9 O5 l3 z7 Y {% J3 U/ g& M/ F: I7 ?# B5 Y
l=0;
! V$ q% Q) _0 ?7 ]: _( ]$ K printf("Inconsistency found after %d relations.\n",n_m[1]);8 X1 i- G& \; l8 v
break;
- U1 U5 M4 O( _( f, Q# q) a }/ V1 h5 b; S# G8 g4 o( w
}
# |4 X& \8 Q. C# K# r" N" H if(l==0) ; [+ U+ f; y! ]7 J( C4 M0 D
continue;//Inconsistency found after x relations.2 I# F9 N8 |3 ]- |# r; ]
else{
4 D/ W8 `9 ?" e/ {. H3 |4 V% l if(n_m[1]<n_m[0]-1)
% x9 A" |* X* v& X: I" O( I {$ i) D) S3 I2 v) T- h5 N
printf("Sorted sequence cannot be determined.\n");
' n4 K$ |- ^# p& L- S2 b" ~$ q T l=0;5 L: y0 y( A4 b7 K+ @
}/ P+ z0 ?, Z0 Z1 y& [
else
3 Y$ E" S: Y* q! n1 i {% P; U$ e9 g! f
if(n_m[1]==n_m[0]-1)
( i- N0 r3 E5 M) |$ W _ { * ~" L# o/ h/ t' ?0 }
int k=0,p=0;
' M+ @1 j% `1 k p for( ;k<n_m[1]-1;k++)
' Q0 N7 m5 O% h2 ?! \) f! r8 {) o for(p=k+1;p<n_m[1];p++)
' q" i$ b, N6 `4 v6 u- i if(re[0]==re[0]||re[2]==re[2])5 [) }. A% o0 L) Q
{2 d& k3 I0 d/ k0 u: z
printf("Sorted sequence cannot be determined.\n");
" I% t% g7 c. t v( s break;
{% E5 S! B5 g$ f2 } l=0;
+ x4 ^" }+ F+ u% T& Y* m" \. i- m; L }
2 d; ~- w2 a9 r. ]8 m' p }
0 @4 X5 q4 o/ {: d% e }5 R& \" S0 c' X; X9 Q
if(l==0)
4 Z+ R E. N* |+ ?6 f1 R4 x0 D continue;//Sorted sequence cannot be determined.
& \! e; R0 }! _& \! D8 k, f9 }5 u( q1 |" [: Z
else
6 z+ U3 E! D k, ` {' W$ n: {2 H# A/ j) U0 E. i
+ A8 i: t) l* \3 Y/ L* `( Z
for(int k=0;k<n_m[0];k++)( H, j. z8 n' G/ O. Y& i6 f* y
temp[k]=k+65;, z4 `) @& t o) ?
for(k=0;k<n_m[1];k++,j++)
6 P1 E! I. L$ Z4 K+ M% M! N {
" A9 h' r$ E" f int t1=0,t2=0;% Q, K8 r) J6 s, I6 P9 C
for(int s=0;s<n_m[0];s++)$ G% y; U7 ~" f
{% q7 \& M3 c( j) L
if(temp==re[0])
8 }7 i9 a4 |7 L9 W t1=s;
+ r7 \4 X" u* v4 o if(temp==re[2])
+ n, H2 o3 }" N7 E5 l& c- D t2=s;
4 b3 @- ~/ u$ d: x7 Z }7 _4 g E5 q; S( g b& G
if((t1>t2)&&re[1]=='<')- d/ A6 m. s/ p' w
{
/ n/ V5 o7 k* n+ i4 z char t3=temp[t1];
1 k- W# P( N/ B8 ^# C& Z2 z% [ temp[t1]=temp[t2];
: a8 u o; ?( U/ M I: Y temp[t2]=t3;
1 s) n y+ l" ~; K- O X- @ }
l3 w: Y7 _8 U }3 S# q" G' b- V, S1 H+ _
int count=0;
/ w- O6 z# Z# ^: F5 H for(int s=0;s<n_m[0]-1;s++)- U1 m: Z# b) Z% m% i9 Q+ [# b4 R
for(int d=j-n_m[1];d<j;d++)& q. t" i) f) M6 N3 Y: J/ L, A
if(re[d][0]==temp&&re[d][2]==temp[s+1])
+ z- V* g% W0 \9 o {6 I9 ~: |4 w0 G1 G: s2 ?$ B1 L
count++;9 @2 b3 l \! A: q- @
break;! i& [1 T6 w( `) p0 j! g
}4 }) |: R# k" V& x6 b
if(count==n_m[0]-1)( Q' O0 a! s; U9 P: ]
{% h9 e" N/ v. j# F
printf("Sorted sequence determined after %d relations:",n_m[1]);
T- X0 Q3 ?% c0 y2 | v1 j4 ? for(int f=0;f<n_m[0];f++)& z. u# C* r. n+ f& J5 c" G2 @
printf("%c",temp[f]);
7 R1 R9 G1 [! j+ ~6 R: x printf("\n");
3 z0 {0 s6 \6 o0 P5 M }
1 v) |! f' y4 [' l else
7 M% i6 F1 b V9 `# l {( i printf("Sorted sequence cannot be determined.\n"); ?3 r0 Y# Y6 N! n! E
}, v/ M- N! s8 L* w' N' d. r4 k2 U; e' l
}( v. J- R1 M7 \ u5 p
}
- m. J& E8 d2 X; K+ y) K- T}
, [ X; P( m! R" r/ v& }' w/ D+ v, M6 w" J6 v7 _: ^
9 l3 K. N" k4 q: r' m, `5 F" e
# [% J: _2 @+ O& e% i" E5 H
2 |$ x! u' K$ A" q+ r/ }+ r2 \
2 a6 l* b, @4 d0 p: `2 v( K
8 v% G2 y* w6 C3 Z+ {) s; A4 I# z
6 N) r) X. ?" |1 Y! H6 G
来源:编程爱好者acm题库 |