您的位置:首页 > 编程语言

又一道图论题PKU1094“Sorting It All Out”我很容易就把代码写长了

2006-07-24 11:53 501 查看
基本想法是用图论做,用floyid算法。对letter字符串数组进行排序。我发现我很容易把代码写长,看来我要改正一下这个缺点了!

我的程序代码 :

/*
Sorting It All Out
Time Limit:1000MS Memory Limit:10000K
Total Submit:984 Accepted:297

Description
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.

Input
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.

Output
For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx 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.

Sample Input

4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

Source
East Central North America 2001
*/
#include "iostream"
#include "memory"

using namespace std;

int main()
{
int n,m,i,j,v,k,flag=0,t,e,d=0,sum=0;
int dis[26][26];
char ch[4],letter[27];
int flag1=0,flag2=0,f,flag3=0;
while(cin>>n>>m&&n&&m)
{
if(n<2||n>26)
continue;
memset(dis,0,sizeof(dis));
for(t=0;t<m;t++)
{
sum=0;
cin>>ch;
dis[ch[0]-'A'][ch[2]-'A']=1;
for(v=0;v<n;v++)
{
if(letter[v]==ch[0]&&!flag3)
{
flag3=1;
break;
}
}
if(!flag3)
{
letter[d]=ch[0];
d++;
flag3=0;
}
flag3=0;
for(v=0;v<n;v++)
{
if(letter[v]==ch[2]&&!flag3)
{
flag3=1;
break;
}
}
if(!flag3)
{
letter[d]=ch[2];
d++;
flag3=0;
}
flag3=0;
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(dis[i][k]&&dis[k][j])//i<k,k<j->i<j;
dis[i][j]=1;
if(!flag2)
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(dis[i][j]==dis[j][i]&&dis[i][j])
{
flag1=1;
j=t+1;
cout<<"Inconsistency found after "<<j<<" relations.";
flag=1;
break;
}
if(flag)
break;
}
if(flag&&!flag2)
{
flag=0;
flag2=1;
continue;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum+=dis[i][j];
if(sum==n*(n-1)/2&&!flag2)
{
for(v=0;v<d;v++)
for(e=v+1;e<d;e++)
if(!dis[letter[v]-'A'][letter[e]-'A'])
{
char temp;
temp=letter[v];
letter[v]=letter[e];
letter[e]==temp;
}
letter[d]='/0';
flag1=2;
j=t+1;
cout<<"Sorted sequence determined after "<<j<<" relations: ";
flag=1;
sum=0;
}
if(flag&&!flag2)
{
flag=0;
flag2=1;
continue;
}
}
if(flag1==1)
{
flag1=0;
flag2=0;
sum=0;
cout<<endl;
continue;
}
if(flag1==2)
{
flag1=0;
flag2=0;
sum=0;
cout<<letter<<"."<<endl;
continue;
}
if(sum!=n*(n-1)/2)
{
cout<<"Sorted sequence cannot be determined."<<endl;
sum=0;
}
}
return 0;

}

别人的程序代码:(基本算法相同,可是他的代码好短啊!)

#include "iostream"
#include "memory"

using namespace std;

int main(){
int n,m,i,j,k,l,d[26][26];
int flag,inco;//flag标志已经找到序列,inco标志出现冲突
char c1,c,c2;
char letter[27];
int count;
while(cin>>n>>m && n && m){
memset(d,0,sizeof(d));
for(i=0;i<n;i++)letter[i]=(char)('A'+i);
letter[i]=inco=flag=0;
for(i=0;i<m;i++){
cin>>c1>>c>>c2;
if(flag || inco)continue;
d[c1-'A'][c2-'A']=1;
for(j=0;j<n;j++)
for(k=0;k<n;k++)
for(l=0;l<n;l++)
if(d[l][j] && d[j][k])d[l][k]=1;
count=0;
for(j=0;j<n;j++){
for(k=j;k<n;k++){
if(d[j][k] && d[k][j]){
cout<<"Inconsistency found after "<<i+1<<" relations./n";
inco=1;break;
}
if(d[j][k] || d[k][j])count++;
}
if(inco)break;
}
if(inco)continue;
if(count==n*(n-1)/2){
cout<<"Sorted sequence determined after "<<i+1<<" relations: ";
flag=1;
}
}
if(inco)continue;
if(count!=n*(n-1)/2){
cout<<"Sorted sequence cannot be determined./n";
continue;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(d [letter[j]-'A'][letter[i]-'A'] ){
letter[i] ^= letter[j];
letter[j] = letter[i]^letter[j];
letter[i] ^= letter[j];
}
}
}
cout<<letter<<"./n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: