本帖最后由 厚积薄发 于 2010-5-6 18:40 编辑
8 |8 d6 O" l, n5 ?( J b% n
2 k' V( R7 ]1 v2 _1 cSorting It All OutDescription1 e8 V5 i: M5 R- l5 F6 }/ K
. p) B- K- y7 ^/ V3 N R
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.
% G( L1 B$ P, V: D% l0 aInput7 t8 `, Q- B5 [
u% |% v4 x [" l5 d, Q: |
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.' \: x4 G! F6 ^" d ?7 l/ c- e
Output
" y9 n3 a9 @. J- z0 b. d
; K* D. G7 ^$ w: wFor each problem instance, output consists of one line. This line should be one of the following three: 9 b4 \2 C# G0 ^* d9 ?6 M3 r
' ]& M0 w1 s! ^% M* ?" RSorted sequence determined after ** relations: yyy...y. 8 x4 J9 x& f. z. x$ t# ^4 k4 m0 @& q
Sorted sequence cannot be determined. 7 r0 [. } E! b/ A4 h
Inconsistency found after ** relations.
6 I# _2 u! X8 f; G m1 _- {' c" F6 I1 v+ ]8 }: Z
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. + ?0 J2 @# W, v) x) W# o. F6 i' ]
$ |6 N" N: a. P6 t( k7 c
输入样例 - q% ~# y3 V3 X1 `9 `. }
4 6
3 E; Z' J7 W, d9 X5 ~4 z" B0 ^A<B: b4 N' f! ^) _; n% x6 ?: f
A<C$ U, K% }/ S/ G7 i: O; P( O: @
B<C
' r2 C7 `" t/ e$ xC<D& r' f. S# P0 B& R6 O: Z+ m
B<D
9 p( |4 `' \7 A, o' ~* O2 PA<B
8 a; c+ ]6 b, x) T" J1 l3 2
: R/ m+ X: n7 u) a7 L9 J) R J$ VA<B
p9 X- y8 p1 NB<A
/ D/ {+ F$ B7 i26 1
- q8 r2 Z" z# ~" Z U5 BA<Z& n, n4 i* L5 A# G" M; }. c
0 0
- U- H5 }8 r2 h0 ?
! n) ]! } O1 R R( J* I输出样例 9 {( H z9 \& G* O' X
Sorted sequence determined after 4 relations: ABCD.: k- s" W' ?. {' h" `8 ~6 G
Inconsistency found after 2 relations.: t% u, C" S3 ?
Sorted sequence cannot be determined. : A# O/ `8 z2 |3 I/ e
Source
& a; Q& b& Z3 k. F( o- q9 d+ `' P) A; E4 m& @+ a: L; O
East Central North America 2001
# g g- T8 X, n, \4 C9 t! A程序解题1: & h6 P: j1 @) j
//By Jackie Cheung
7 p& k1 `& B" C9 O: q7 M#include <iostream>
. S7 G+ W. {+ a) g4 j7 t#include <string>/ ~9 Z, A/ N2 N. ?, h" y$ Z' q
#include <cassert>
: J" T8 i9 a: f; [0 Htypedef struct tagNODE
. I1 D% O {, P: F* c" }{
6 M+ E- \9 z5 y8 j5 V$ W+ ]& f2 d char val;
?/ w/ Y1 ?8 V) ]7 e9 ?9 s struct tagNODE* link;) {8 K, _: g/ [2 _. }) Q$ I5 F- V
}NODE;/ F+ ^( V4 j4 j8 Q) |& e" B) i( m
using namespace std;
" Y; X, j6 \$ Q7 s/ ^void Marshall(bool** arrary,int n)3 {& Q; B* F$ f# Y( t: h
{
4 D3 ^8 s$ d5 U4 x7 W; W# ] for(int i=0;i<n;i++)7 f& F0 m, t! t. C, M4 {
{
: y: G5 r- I! s' Z for (int j=0;j<n;j++)
. ~4 Y. I _% L$ t& q; p+ } {
6 M `+ [- p+ \$ ? if(true==arrary[j])
2 X5 g; C3 a2 ` for (int k=0;k<n;k++)
; H, J! d$ a+ l0 M {
' V" F6 l4 J- y) N8 D. h% x k8 Q arrary[j][k]=arrary[j][k] || arrary[k];0 t) }+ I5 o h W2 ]7 O; N
}
* ^: u3 k3 b! s+ x/ b' r }
. Y4 A5 R) h+ L% f$ k$ _ }# E" D v$ f8 ^
};
8 ]5 G* p' L1 R% k8 v9 Obool SearchForRoute(bool** array,char Seq[],int nLeft,int nIndex,const int n)+ v! O7 {$ G1 g* I K2 _
{
3 }0 y2 }3 j1 @: d- N# R7 ~ Seq[n-nLeft]=static_cast<char>('a'+nIndex);+ b5 G% s' m& `
bool bFlag=false;
. R8 U) b0 \" C* S5 w if(1==nLeft)
# d$ H1 A j2 _' `$ i {- X5 d: L. C0 n ^4 ~8 G: h9 W% ?
Seq[n]='\0';
& m Z: c! F/ J2 ]0 H6 p return true;
* b6 S9 F) |$ R8 @, d( V' s4 X( q/ [ }1 P( p9 ?) W" [: ? A3 b8 U
for (int i=0;i<n;i++)
2 \; E' y6 F. r% Y/ h& b {
" z- s0 \9 I% f6 P if(true==array[nIndex])! ^& u6 c8 i7 q: x
{* T$ J. \& Q) K
3 I: w& `8 R$ }' P4 P3 G bFlag=SearchForRoute(array,Seq,nLeft-1,i,n);
+ J: ~/ A, T# s. q f) z1 q: L }
0 j+ j1 J3 \2 D: P1 ? if(true==bFlag)
6 m8 u. T4 C) C# [9 n" V9 r return true;% ]9 g, {0 P z" U, j' I/ e3 H
}
9 Z* Y" d E+ O* V, g9 }) D return false;2 ^: l* I3 i2 c! R- G. T4 c
};$ q/ Y( e! i- y; @; X9 g
int main()
- o% n: t/ V3 D5 O# Q$ N{2 D* c- z' w7 Q, s, }
int nSeqLen;
$ H: s* t" C2 V int nRelNum;
# ~& e" `2 w. T: z cout<<"Input the length of the sequence:"<<endl;
% i' G, c& z7 n4 G1 V" S8 s8 t( B cin>>nSeqLen;
) ~" |* w2 ^0 y& c cout<<"Input the number of relations between the elements:"<<endl;
% U% x, ^* v5 d3 r/ O1 a& X cin>>nRelNum;
3 f1 C, A. n8 p5 j3 J& R6 D) v //1:if nRelNum<nSeqLen-1,then the relation can not be determined!
8 R- _. l/ G$ |6 W if(nRelNum<nSeqLen-1)0 }( S$ f. S p% {6 y5 e
{' s* B+ ^) J% v- R+ Q
cout<<"The relation can not be determined!"<<endl;& ~5 o; n: r" ~% [7 L5 ~) S1 N$ U
return 0;
0 j& [4 O0 s8 g( g+ \# b* j3 \, W }3 f1 _. f# Y( n% S
string* strRelations=new string[nRelNum];
* A2 B0 }5 j0 |! I8 V1 _ char* Seq=new char[nSeqLen+1];1 @0 |9 K" M8 m! [3 [! i
bool** array=new bool*[nSeqLen];( r1 @0 O; w8 G% {- @
1 l' |# u8 e6 F# y# r0 T) p for(int i=0;i<nRelNum;i++)
1 m* u& s% ^+ P( X- L8 ~8 N {
0 a' P, F. p5 f k cout<<"Input the "<<i+1<<"th relation:"<<endl;
( ?- P5 J8 S# ^0 h7 s# D cin>>strRelations;
4 I2 v. L0 e% ` }
% @. L% g; z8 Y5 u) J* s 9 `/ D6 O. n$ w1 m
for (int i=0;i<nSeqLen;i++)1 ~0 C) Q+ D: u) ?
{/ g9 N# h$ c& {
array=new bool[nSeqLen];, i z1 ~7 f! N/ V0 b) h1 D
for (int j=0;j<nSeqLen;j++). f, y) W h x4 H
array[j]=false;
9 M- W! o; \: }5 `$ H }) O) ]8 O7 I4 c0 \
//The main loop% p" e1 B: `$ I8 H/ K# |# [
for (int i=0;i<nRelNum;i++)
9 S) e) h( `' m% l8 D3 {# } {
. s$ _2 A1 z5 W' w$ |$ e# c( V char a=strRelations[0];
& D- y8 B, h6 [$ t$ D char b=strRelations[2];. @$ f; R. ?5 Z! J; `! ^$ J$ ]% z
assert(a>='a' && a<'a'+nSeqLen && b>='a' && b<'a'+nSeqLen);
; B! w6 y+ a8 a: |# ]+ @ array[a-'a'][b-'a']=true;
0 U/ D" \3 k& m0 J) E0 a
6 k' L2 G1 D0 O4 Q Marshall(array,nSeqLen);, q2 Q; \2 W) g2 q0 K6 {4 F
2 d9 }9 H3 C% n //Check for Inconsistency after every relation0 ]' m4 T3 r3 C7 Y% h
for (int m=0;m<nSeqLen;m++)* _: }$ }0 u# D4 s$ A$ m
{% H/ f/ U4 X' t: \, |/ M
if(true==array[m][m])
% b# h! Z+ ]1 ~3 Y, f: |( F0 k9 H! ] {3 E3 d9 z$ a4 n$ A$ l3 X
cout<<"Inconsistency found after"<<i+1<<"th relation!"<<endl;
! A d/ m5 n; C% F% R, N7 l delete []strRelations;3 J5 l) p' t% r, }& S: s
for(int k=0;k<nSeqLen;k++)
4 U$ P* l( y: ? delete []array[k]; n5 w& S- ^9 M9 M# L
delete []array;
2 \% K2 h) Q1 ]6 f7 l4 v delete []Seq;# ~; i/ ^2 x- G E8 y) N: G
return 0;1 N- A- r# X: M
1 k! {0 q. x: Y# I$ B6 @/ x4 k. x }$ O$ ]8 O8 C ^
}) p1 H" \7 g" t
( D: x4 G# U, t$ W ?2 R
//Check for the determined sequence after every relation # \* I. ? D$ r2 z& E4 c! x7 \$ M
for (int j=0;j<nSeqLen;j++)4 J6 Q7 ~) N9 V
{7 [1 l# l3 r* f& ^! H
if(true==SearchForRoute(array,Seq,nSeqLen,j,nSeqLen))# F6 v7 [% D) |% f0 h
{
6 g' y5 ^ _# Y9 B" E6 E8 y cout<<"Sorted sequence determined after the"<<i+1<< "th relations:"<<Seq<<endl;) D* N* J/ Z3 W7 e( U: G
delete []strRelations;
, {! J* G( f- M, W$ U. I for(int m=0;m<nSeqLen;m++). g2 i/ p3 W6 g* F/ B; g! T* e; r
delete []array[m];
x. d; J! _8 I delete []array;
& B) |( K, X b1 c: Z' c delete []Seq;; _3 `$ H7 A% t. ^
return 0;
) K, _4 f% f+ Z; p* U7 u }+ g0 J9 e5 T& z; z
}
4 u u0 m! u0 y$ Q2 C$ C& ~. t5 }
* {9 C. R! R E; r$ W
}
+ f3 z* h+ h5 [5 z8 H //If the program has come to here ,then the relationship hasn't been determined!!! t/ w" ~! ?; G( W( F4 N' c
cout<<"The relation can not be determined!"<<endl;
- l+ n% C5 ?5 | delete []strRelations;4 [. n3 L4 ]. W5 j% d5 B$ i# N
for(int m=0;m<nSeqLen;m++)! M' j3 G9 x% g1 `* f
delete []array[m];
! t+ c; V) `) l5 z0 y, n/ |. \ b delete []array;4 d9 P4 Y# \! u
delete []Seq;
, N+ n! y, ^& r7 w0 g8 N) s
3 B' q4 s% A3 i) ?) z7 \; K return 0;
" R. ^, ?6 Y! `% x B& J7 ^}# z; j. P2 S- ^5 B, [9 A4 @
' ^; ` v" d H) K/ @8 s& T程序解题二:#include"stdio.h"
+ s5 S; l( T s# L$ gvoid main()
# ^, ]' _. _9 @6 w! `3 G/ n{
4 f8 r8 K& w2 r* u, x. c int n_m[100][2];0 F, `6 w9 l! p$ b0 }( p8 p
char re[1000][3],, \, Q. j7 p, f. \4 f
temp[26];
4 w4 T: z" X; p6 _ int i=0,j=0;
3 K. w; E8 f, d: j } scanf("%d%d",&n_m[0],&n_m[1]);
i6 R9 j9 x/ L5 e+ ] j2 o' l for( ;j<n_m[1];j++)& s$ q; d+ b! M
scanf("%s",&re[j]);, E5 i' l ?$ T
while(n_m[0]!=0&&n_m[1]!=0)5 f% C7 t5 Y: Q, |* y
{8 f' c" a: o6 ^# q9 Q
i++;
, z# v. i( r. V9 ]4 H scanf("%d%d",&n_m[0],&n_m[1]);& U- j% w+ Z: A0 m" M
for( int f=0;f<n_m[1];f++,j++), f& _3 G' J9 l8 K5 ]% [/ l% {
scanf("%s",&re[j]);& v" E+ J% c- s/ b# N
}4 \( d* @! t& v1 h X4 S- }
i=0;9 Z: A/ l% p3 P5 l, R: K- \3 w: N# A
j=0;
* s( H; _/ E4 `- m$ }9 C for( ;n_m[0]!=0&&n_m[1]!=0;i++)# v- r9 I, e9 C; C5 j, H: S2 o
{
4 u/ O& w3 U+ t; U2 B int a=0,b=0,l=1;
9 a& s) u7 T" h) h5 S+ j7 D for(a=j;a<j+n_m[1]-1;a++)
& F$ S/ f. Y& d3 z1 y+ P+ m for(b=a+1;b<j+n_m[1];b++)! [- l3 s5 h- p9 P* G+ h! G: h
{; S, N8 |* A9 [; }
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])
' j7 O& x& P+ G6 A( y0 V {$ R: r1 x! ~. D( G
l=0;
1 _. ^/ J' @6 F, p2 `( r3 F printf("Inconsistency found after %d relations.\n",n_m[1]);0 a% ^% c Y( D0 p
break;8 D/ B0 p9 `# {0 n
}
/ I# F l: Q4 E' [8 m }
5 [# {4 }$ g# L4 `) \ K if(l==0)
( U6 `$ x0 J. i V: v" M continue;//Inconsistency found after x relations.4 O5 \1 D& O$ J$ |! e' Z2 R' |
else{
' Z7 x- d$ s- p2 a+ v$ b+ N2 Y: ` if(n_m[1]<n_m[0]-1)
1 f1 P1 y& ~% I( @1 W {
" G- h5 T1 l+ ` B" ~ printf("Sorted sequence cannot be determined.\n");
) I2 T, o; H! X6 ` l=0;
: M) c/ P; t- ^8 m, g }
8 C3 c6 t7 b F% S else
) s& c4 @, E# \2 x& } {
( T! N, q" W' \# M9 B: A if(n_m[1]==n_m[0]-1)
7 Q$ U+ O2 M: A) @9 t7 T { + }; e* Z& i" J3 t
int k=0,p=0;
m: _8 F2 x6 D) |/ J for( ;k<n_m[1]-1;k++)
* v6 T7 n. N, P; l9 x for(p=k+1;p<n_m[1];p++); z' Y) a( l7 Z& Z3 I( e% J! |
if(re[0]==re[0]||re[2]==re[2])
+ O- @, `: p2 R) Y( ]$ N5 P* r8 q {! E+ y! g8 v4 k: M+ `
printf("Sorted sequence cannot be determined.\n");
( \4 z" z+ q2 f break;1 I; Y5 G1 {) D& ?4 S5 ?4 u
l=0;! t! s* l# ]6 P0 l, _6 k' K5 J
}
! G! ` I9 }5 Q f- f }: a6 T; G1 ~2 l, ~" f
}
1 m* {) z- K7 f5 ~ if(l==0) 0 k! _3 T7 i! y5 a8 d
continue;//Sorted sequence cannot be determined.
' t" ^; ?3 h$ r& a: {; E$ \3 [6 r$ }& Q
else
% u) B4 ?1 O% k {. o5 [6 ?" O# n p
" g" N6 ~* r. D5 y
for(int k=0;k<n_m[0];k++)) O5 E% p) Q, e9 g6 p6 y
temp[k]=k+65;$ l+ S9 z' N+ t+ G
for(k=0;k<n_m[1];k++,j++)* d D) _7 n& ^4 y1 T4 r
{7 i$ j, U4 A8 U" }$ ?
int t1=0,t2=0;9 ]/ w! y4 K f& x! [, N: X
for(int s=0;s<n_m[0];s++)* N) y5 x2 S B' Q& c
{8 H m1 K9 l+ A6 n* M
if(temp==re[0])4 r6 C3 ~( T; t. [6 m, f: X; f. a7 f$ E
t1=s;6 Q6 i' O0 t6 P# Z/ Y. n, ^) f
if(temp==re[2])9 ?) [ @6 y9 U$ a0 Q1 t/ B
t2=s;+ [3 f0 c% j6 j, m& ?
}! V9 R$ c2 { @
if((t1>t2)&&re[1]=='<')
- W$ j9 n: N1 A, K5 F; {# B {
! t% D! S; S" B+ Z char t3=temp[t1];, j) I4 B( {+ r- s3 _" \
temp[t1]=temp[t2];
+ m* k# ^. @7 V9 Q; B temp[t2]=t3;
) L. q5 V) E/ Y/ ~/ x }9 Z. x/ {0 ?9 l9 U0 a5 K
}9 Z$ ~8 {' Z) z( x
int count=0;& o- X a9 b- U1 j1 K
for(int s=0;s<n_m[0]-1;s++)% i0 q- {- h8 k
for(int d=j-n_m[1];d<j;d++)
$ o9 Q4 b6 ^5 M0 U& J% P if(re[d][0]==temp&&re[d][2]==temp[s+1])7 l+ \- p6 u6 ?$ k' H/ V. d5 u" Z
{" K/ j7 t, E# S5 p2 m9 v5 M/ A
count++;' Q% N$ K7 h& w/ F* f6 V
break;
1 C) i/ Q2 c) @7 y }
% D# T# l1 i0 b if(count==n_m[0]-1)
3 E: ^& ^( T- I {) H5 u, b' C& M) y6 J9 f) `4 a
printf("Sorted sequence determined after %d relations:",n_m[1]);/ V/ H; y# p" m
for(int f=0;f<n_m[0];f++)
u" w0 d6 K' M printf("%c",temp[f]);
4 n p8 N5 g+ T; j2 ?9 _6 @ printf("\n");
7 @4 b4 z+ [) E, B& T }
. N; k* D0 V2 u$ ?) q else
0 W9 s+ V* H. Y3 G5 p printf("Sorted sequence cannot be determined.\n");
, k4 R, j2 Y [' S* y' e }/ H N5 c8 U. n
}
: U: D! U0 @* ^: q }
" K" s0 C' S( Y7 t- ~0 F% W}
) q R* n3 q8 {' Q+ e7 o8 w2 z
$ \. l% u# N6 ?* p- ^3 ^
% y6 ~: Z3 p6 C0 b5 r
" k$ D8 w6 a8 `9 t7 w' K- j; K( {: A' }9 T# k/ V0 f
9 ^6 q9 p0 m2 k' g) M) n8 G
. K, d' j3 f+ f0 z! [
" Z. f- T7 B# ^$ d$ ~
0 ^8 v, M# F3 H4 X" ~7 l8 q来源:编程爱好者acm题库 |