本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
; ~3 k7 W9 w$ O# I4 X9 v, T1 L5 h2 f7 E4 m5 @6 a' J
Sorting It All OutDescription
, u. @- `% Q0 k4 U; w# A4 E# {
3 h2 r( I# g2 D. P$ s6 nAn 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.
, z) [1 E* M6 w, kInput
0 E( c$ Q+ Y/ X0 g1 x% H( f! N. a& t1 i- q) P
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.3 h# p& L( [8 g5 n5 m
Output; X: H4 a9 i2 @% @
! l' Q) l5 }: t3 gFor each problem instance, output consists of one line. This line should be one of the following three: 1 X) N3 \1 N! |% ]
) w! V$ |1 T& {& LSorted sequence determined after ** relations: yyy...y. 7 }9 {/ [% l2 W" g9 A2 T( @
Sorted sequence cannot be determined. ( B* n n1 ]& {0 G/ w
Inconsistency found after ** relations.
3 ~- h" ~$ u( F. J. B
8 E' [- A6 [6 |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. - E1 U8 Y u; F4 e
% K8 w4 z- m/ f# o5 X$ k9 A7 {
输入样例 % r9 f. T0 R- \5 b* s; l
4 62 W& b" s n! b; O+ \
A<B; v0 K5 v F1 n1 X6 I
A<C: w/ t+ [4 W$ J( y5 K' I! y- f/ w" A
B<C
9 k/ P! x: ]+ pC<D% R+ g: x/ e4 F: o9 p; n
B<D( P2 i$ d) c" g3 X6 E/ c
A<B
/ p2 A h* }* m$ _0 r5 |, |% @3 2
) H4 S3 K& ]2 L; B& A4 c) @A<B
T1 S6 b) R& h, r/ S7 bB<A) b% [/ l0 F! f. K: K f- ]
26 1# N! f0 F% g% K. E7 i9 D
A<Z5 F1 F% V3 r& {: V' s- f2 O
0 0
; a( E* F& G1 p ; W F& x+ J& g0 h2 Q: I
输出样例
* A# |* t6 P4 Z( I7 L' JSorted sequence determined after 4 relations: ABCD.
8 w1 K: b+ D. e' ]' F- ~0 }" h4 v+ GInconsistency found after 2 relations.! r5 m/ {) p/ F) b
Sorted sequence cannot be determined. & ~& j5 D. d7 u2 h- j4 @5 r
Source
: O Y/ s9 j9 X( A! ?) b' W; I1 Y: e! W' W9 u
East Central North America 2001 / B% z9 N E. ]5 k2 B- d
程序解题1: ) k0 A- U+ P! r5 }6 |. a) G
//By Jackie Cheung* c% V, e- B% g. Z
#include <iostream>
' c8 }; m3 [! W3 ~) I2 a#include <string>
; m! m" F4 y2 G6 e0 @( O* D9 y#include <cassert>
1 j9 e* |( J6 l: _/ U. D. X6 ktypedef struct tagNODE ( `# L4 W) n8 f0 }1 W3 U
{
6 V2 t$ K: ^5 W9 ^- y char val;
) z- t9 I7 z1 i/ ^3 d struct tagNODE* link;
9 p& ~' c) G# p, E* V, D}NODE;
9 V, Z- y+ h. r6 p0 }4 [9 Iusing namespace std;5 q& T$ c% k2 z* f- s
void Marshall(bool** arrary,int n)
) ?) N" o( J4 J" _{
3 ^/ n4 R" c- N for(int i=0;i<n;i++)
) W1 o: S% }- B" D! ~" O {
4 q D% y5 Y& c for (int j=0;j<n;j++)- H c8 s( Z4 D& g) V7 q k/ [ b
{% H! B S, z" Y3 S& q5 m8 }- d1 q
if(true==arrary[j])( a! `6 H" t0 R8 n
for (int k=0;k<n;k++)
1 c7 J4 q- l/ G6 P [ {% G& y- t: T- H7 N% k
arrary[j][k]=arrary[j][k] || arrary[k];
. P |) D5 C" J ~# z9 X }
. ^6 F6 k* S0 _ F% w9 k }
* g4 d) V/ P7 B) A C# ` }
* {' t7 |* g+ d};3 X, ]6 }2 p3 k9 h" u
bool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)* J. ~0 a' N, G" U+ g
{- S1 |0 m. J# P6 U8 v
Seq[n-nLeft]=static_cast<char>('a'+nIndex);3 m4 M) v$ b; V& R; W
bool bFlag=false;5 q6 e( C% U! C8 E! R7 W+ I& A& O
if(1==nLeft)4 v/ Y D6 \$ H! l# O# p& |- t
{
2 x6 F ^( q( f7 k4 N0 u Seq[n]='\0';
& D- u; q( V" n1 _* V$ L2 u3 t6 n, Q return true;
. W9 z+ n) Q5 t; ^ }8 f w4 j! T" \+ i% U
for (int i=0;i<n;i++)" i! R8 M5 j; n- D; j
{/ J# `* b8 W* E4 H! K* d2 E- V
if(true==array[nIndex])
" \- P D c& z" w2 N0 V {
; N$ g7 w0 w' s2 V4 }5 N# s
: {+ f* K6 B d$ ?8 d) x bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
W* j/ q: R' C }# w6 X+ I6 |' L" n( t1 I8 k
if(true==bFlag)* F* g% c4 U+ A+ f
return true;7 G2 U$ x/ F3 P) o+ h
}4 n4 Q6 d8 G2 a8 S5 \" k, T4 e) B8 X
return false;
# [; a# N0 @ X. a: U};( \% n) d/ M3 a
int main()' H5 A5 q% b# y
{8 ]7 E8 J' _6 z: \! j
int nSeqLen;
1 q9 ~( `2 a/ x4 t. c int nRelNum;
s- n$ z+ ~( t, K8 a6 ? cout<<"Input the length of the sequence:"<<endl; X) f3 j9 a1 b
cin>>nSeqLen;
* T. x4 s# g6 H1 ~ cout<<"Input the number of relations between the elements:"<<endl;
$ D O5 N/ l6 p& T* _4 I cin>>nRelNum;! T& G7 h( ~* s# X' Q
//1:if nRelNum<nSeqLen-1,then the relation can not be determined!
2 S# Y' p/ v( c$ C if(nRelNum<nSeqLen-1)
/ x& D0 _0 t! G$ {$ o {
. N }& e0 M4 j) k! x cout<<"The relation can not be determined!"<<endl;
- E# u, N( G! s return 0;$ p6 s6 }$ A7 L. x2 \
}4 |9 A' e# X: z+ o' C3 U9 O& D
string* strRelations=new string[nRelNum];
* T, A9 E! s8 Q$ i" ` char* Seq=new char[nSeqLen+1];- C5 F2 ?3 B: |2 S& a& u
bool** array=new bool*[nSeqLen];/ W- K9 E1 W0 R/ y+ f8 y$ e
9 C. r' R @3 G F& c! a8 }% r
for(int i=0;i<nRelNum;i++)! C4 A: _, e3 L: ?8 f$ g( h
{
# ]( c* _+ w' Q& N+ E H cout<<"Input the "<<i+1<<"th relation:"<<endl;
7 O$ ^' D2 E) S+ p; D/ J2 Q, V' i cin>>strRelations;
& R9 L( x9 i; @4 \( x }, i# d) \# j! \9 S- g0 L7 b
4 }1 z9 G0 U+ ~- L+ E( r( B# g
for (int i=0;i<nSeqLen;i++)
! l) f% i: H3 U2 \4 J1 b* w {
3 \" P( ?9 Y# Q, o( b$ X6 m array=new bool[nSeqLen];. B! t6 k" o( s% l
for (int j=0;j<nSeqLen;j++)
2 A1 i) B$ A: \+ T! O array[j]=false;; \! Z! C+ n& e, J, s
}
* V3 }, d; Y6 `3 l6 G* D //The main loop( W6 R. h% n; y9 M: m) l- K/ ~' `! ]
for (int i=0;i<nRelNum;i++)5 F; Z, n/ _6 e" s/ V
{
. D8 R8 X" _) j char a=strRelations[0];
+ J' X/ [7 }! t% k char b=strRelations[2];
/ S# r2 m" Y3 G- h" X# N9 D8 p assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);+ G( H9 q( {& r0 R3 F+ G- D, i
array[a-'a'][b-'a']=true;8 c: Y8 A9 C+ J8 }8 g9 h# N
6 S- t d1 V/ ?6 o% A/ U' ]+ P
Marshall(array,nSeqLen);
R- H1 `" z$ X% p `
B1 g7 X1 f* l2 v7 S: ?. W //Check for Inconsistency after every relation6 ^/ Z: L( e/ j9 |9 G/ V
for (int m=0;m<nSeqLen;m++)$ \) l& R0 d# x- d9 r2 l" O9 w
{/ s! ?" A- j3 I r
if(true==array[m][m])
& [' t8 r2 b L) \. {2 D+ E& D1 O {
4 I' H! d4 H0 | g+ D) k8 G cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;- w, o) i0 Z, H
delete []strRelations;
9 p- h+ }/ k; h: ]3 ]; x for(int k=0;k<nSeqLen;k++) y# @0 v& B |& d5 I! G' j
delete []array[k];2 r3 d% ?4 {3 g' n
delete []array;5 _( w: w2 I t1 V
delete []Seq;
7 \& c7 ~* `/ t) g return 0;) b( Z5 |6 r% R0 r' A
6 B7 C7 l, s' h! K
}! u* h7 k! n$ Z4 p$ S' Q
}
z, Y4 ?& E: _& ?& i3 E, A+ e) j6 e6 M1 B
//Check for the determined sequence after every relation
: e1 M L9 j) e2 ?/ S8 K for (int j=0;j<nSeqLen;j++)
- x% n( q2 u/ j {) s6 T- |& U; W1 z! H( l" o1 I- u& e. s
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))1 i& b' i% D+ e! ?
{
! \& o7 I' }) w! z3 R cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;7 Y+ o# |. [* `% i$ Y
delete []strRelations;
. c5 ?3 F8 W; Z5 K6 } for(int m=0;m<nSeqLen;m++)
* c* ]3 ?' y' a9 f; R- H delete []array[m];
9 K0 |% u, L+ C8 ` delete []array;
: y W* O. P! R$ q4 F% q1 K delete []Seq;
* h% o% b( z- g6 d return 0;* A' S+ |- L, T' [; R9 k
}
- T3 L+ O* C* @5 j }
8 e% M5 W; \0 s7 z( K/ i/ w Z! ]# t& L8 d, C
" f1 H6 f4 w! K' G2 W, D0 N3 [* e4 { }$ \! M: K/ z3 m# w/ O1 @4 L
//If the program has come to here ,then the relationship hasn't been determined!!!
' I. h9 l2 t. ^5 J: y cout<<"The relation can not be determined!"<<endl;; l8 i% h- A. ?5 Q$ p* V6 {- M7 G
delete []strRelations;
' d' A5 |( s# [6 V% B for(int m=0;m<nSeqLen;m++)
* B4 M% I+ L. R5 z delete []array[m];3 B' X5 N& c" H" |
delete []array;( d2 l5 B2 I5 Q; Z O+ V1 T
delete []Seq;9 |4 f. w& \! C- [7 ^- G
. q3 z& S5 G$ A$ w return 0;) s0 Y6 F$ U7 z4 F, ]' J( M
}
. L0 H/ I* k4 i# k" K `) f
8 k& @$ a$ ]8 _/ m: N程序解题二:#include"stdio.h"1 n; G* r& I! E( _
void main()
( q/ d2 [+ I2 {9 |5 A0 C5 z{
0 r, M* ^5 a8 {+ b4 P! W int n_m[100][2];
% Z9 H: W$ g' X9 }% A5 N char re[1000][3],
) j2 ~, M/ y- ~/ i2 R- D temp[26];3 _/ l+ g8 c' Y+ ~0 l8 c, v
int i=0,j=0;
4 R! E( U. f) _* b scanf("%d%d",&n_m[0],&n_m[1]);6 s8 i: p$ U& u3 B5 @
for( ;j<n_m[1];j++)
6 Q( Q7 I4 g; d7 c9 A; D2 _ scanf("%s",&re[j]);) g9 C; q4 d+ j% X7 U
while(n_m[0]!=0&&n_m[1]!=0)
9 K1 g1 i" X# w3 H$ B {
( E" w# `% O0 @& z% e1 o9 Z% }1 z i++;; T3 q8 o2 l* D7 V" h, P' d
scanf("%d%d",&n_m[0],&n_m[1]);+ E- b, J* R* [+ l. \% L# T
for( int f=0;f<n_m[1];f++,j++)+ [" ]+ V5 g m" H$ m
scanf("%s",&re[j]);
$ S9 r: @* ?) H: g/ e' [3 ~ }6 o) j4 i$ j; \1 l
i=0;
5 _$ P% o# g3 R j=0;
7 Y* b+ H E* Q for( ;n_m[0]!=0&&n_m[1]!=0;i++)% o; [$ k( y% R1 m) l0 ?# b- k/ T
{9 N X1 [ Q9 @: Z% y" J2 ^0 G% u. \
int a=0,b=0,l=1;; D7 P# }$ `3 _! @% t8 @
for(a=j;a<j+n_m[1]-1;a++)
0 R' x2 o* {' ?1 T0 y2 y( m a for(b=a+1;b<j+n_m[1];b++)
6 G s/ ~5 t, E {
+ {3 q+ t. F4 p3 c1 L 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]) s* r3 y& M6 H0 S$ Y( i
{
6 Z4 i! t3 m9 p4 c. `' \ l=0;
; c& I: ]5 p3 B( t5 v3 }- N printf("Inconsistency found after %d relations.\n",n_m[1]);
o1 D \7 t# K6 Q break;6 Z3 Z1 |& {1 u3 u7 y0 n: f& ~
}; K1 T- D! A6 b3 H5 d, h
}; q5 C5 `0 e0 |4 `
if(l==0)
: M" N3 J1 E* Q) Y2 b' V! E continue;//Inconsistency found after x relations.
( t/ {1 d6 v, U6 S! _4 b else{
- [8 p6 G5 r& Y5 u9 q( {2 |' k if(n_m[1]<n_m[0]-1)' n+ n4 g- h+ l9 A9 _$ Z1 e
{" S' ?1 L+ `" O/ ^: p
printf("Sorted sequence cannot be determined.\n");% ]1 d0 s& L* }' B5 R2 @8 y
l=0;4 F8 o/ T S) d! K
}3 ] `& x4 O2 [) ?3 H5 h p
else
2 D6 ~6 ^, g* J {4 Q9 ]* n0 c4 C# Q, `. ^- V) \
if(n_m[1]==n_m[0]-1)
% Y' e. L5 ?" H! t9 T: q* B {
# J- A7 _( E) U' _: z( Y int k=0,p=0;
6 k/ f& ]# R8 y) h for( ;k<n_m[1]-1;k++)
9 J2 v. T4 s/ R) R) B0 R; g for(p=k+1;p<n_m[1];p++)
/ m. K, d4 g' b if(re[0]==re[0]||re[2]==re[2])
8 b0 F! M! ]3 {" J7 k {
) r' ?7 e3 V- a; R printf("Sorted sequence cannot be determined.\n");
' P$ p' E: C- J: n9 m break;6 x" s- M$ N- l4 K6 B- [; S. k; |
l=0;4 r4 D+ f+ b* N0 d
}, _0 ^5 W! u2 n. ?. }
}/ f) r* p6 U- X$ N7 L) N3 y
}! [2 O: v! ~, c2 \3 |/ N1 ]+ @
if(l==0) 2 ?8 g- C1 Q9 A# L+ Y
continue;//Sorted sequence cannot be determined.6 k; {4 z# s! E V4 a' h& g+ n
: W& j2 D8 C+ [: P0 Z `; i4 U
else$ ~, a+ I5 o' ]/ T# \4 {- R3 f4 a+ M
{
9 l+ K; X2 W) s! M- T3 u( S! \ 6 f, j% k/ W4 k8 N7 Q( I
for(int k=0;k<n_m[0];k++)* t6 i( z% I N% w
temp[k]=k+65;( C' o# O: j9 S. e: W9 E1 _
for(k=0;k<n_m[1];k++,j++)% f; o1 {) T0 i1 o* |8 V
{
# A7 f3 o/ s5 ~$ a; \' t: ] int t1=0,t2=0;) Z8 g0 A5 l9 O! v8 e
for(int s=0;s<n_m[0];s++)
: | c; t7 `5 {: @ {
5 a) T2 X0 ~/ f. Q if(temp==re[0])
9 e# G' j- M7 R2 X4 z+ a t1=s;1 D5 h$ F1 K. }
if(temp==re[2])
* q) ]4 Q+ ~: r" q5 } t2=s;
r; M7 @- r% { }
) C4 O) k- m7 S* P% w if((t1>t2)&&re[1]=='<') N8 m& A, ]* K9 ?. _' \, Z
{
6 z& a- A7 h" g. h& A0 N) L0 P char t3=temp[t1];
( M5 p, t4 B% }8 N4 e r }! ] temp[t1]=temp[t2];7 u. r) n+ J- N9 I. j% |
temp[t2]=t3;
3 i% \4 |* f4 y P }
/ [9 T: w3 @ q0 N. g2 o2 r }. b, A1 k1 g( Y9 n/ o
int count=0;
6 Z# t! F. i5 I/ R& s& u for(int s=0;s<n_m[0]-1;s++)
! Z9 k Z. m8 Q for(int d=j-n_m[1];d<j;d++)
; _* q: o5 m, X if(re[d][0]==temp&&re[d][2]==temp[s+1])# S: u) U' m: d& c, Q5 c
{. Q0 R X& F% x$ ? v
count++;
, m9 T, @8 N9 h0 A1 f break;# G/ I6 l: V4 W/ V
}
" n* N+ r0 R3 Z1 ` s if(count==n_m[0]-1). D1 q, q- f% [4 v4 ^" r, ^6 X
{
& q+ N `! z4 }: P printf("Sorted sequence determined after %d relations:",n_m[1]);
- h* J* l% _" @8 F- ` for(int f=0;f<n_m[0];f++)
( h, M4 c2 r: g6 g- v/ [% Q' g2 | printf("%c",temp[f]);
. Z7 M. V+ q8 \$ T# y. l' S printf("\n");
1 N4 A4 \8 f3 H }
% \+ {& {) \# A* D' e/ v8 Q* w else
1 ?1 W1 c, p0 N+ [ printf("Sorted sequence cannot be determined.\n"); 1 u# h% N; n+ t' t
}
" g; ?& B1 h; Y! E }6 |6 z4 G' F/ ^7 G4 u
}
0 z& Z' |0 s, t& ~- H$ Z! U# k}1 E" b- S6 Z- s& A7 R# r% g& b) P
7 L; k$ O1 m( G" f
; b/ k- n4 K& W2 \5 G: K- X; G# H# o8 t9 r/ N Y3 ~# b! [
+ E: x7 }2 A1 u3 e, L! T
3 y6 \" B$ ]! n3 R3 c7 l0 V& _$ ^
5 \7 U @" C4 G$ @( {& W/ i ]0 O% m0 B! d* }- _2 L
$ o2 l+ h7 g9 L6 B% k) h
来源:编程爱好者acm题库 |