Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

各位老兄,我WA的不行了。自己从ACM SITE上下载的数据都是对的。为什么一直是WA,到底哪里错了呢?谢谢

Posted by dljlw at 2008-03-10 17:12:04 on Problem 1094
#include<stdio.h>
#include<string.h>
#define f(c) c-'A'+1

char s[5];
char rns[30];
int map[30][30];
int hash[30],front[30];
int list[1000][2];

int solve(int n)
{
    int tmp[30];
    memset(rns,0,sizeof(rns));
    int r=-1,i,j,c=0,t=0;
    for(i=1;i<=26;i++){if(hash[i])t++;tmp[i]=hash[i];}
    for(i=1;i<=26;i++)
    {
        if(front[i] && tmp[i]==0)
        {
            tmp[i]=r;
            r=i;
        }
    }
    if(r==-1)return -1;
    for(i=1;i<=26;i++){for(j=1;j<=26;j++)if(map[i][j]==map[j][i] && map[i][j])break;if(j<=26)return -1;}
    while(r!=-1)
    {
        rns[c++]=(char)(r+'A'-1);
        i=r;
        r=tmp[r];
        for(j=1;j<=26;j++)
        {
            if(map[i][j])
            {
                tmp[j]--;
                if(tmp[j]==0){
                    tmp[j]=r;
                    r=j;
                }
            }
        }
    }
    //printf("%d,%d,%s\n",c,t,rns);
    for(i=0;i<c-1;i++)
    {
        if(map[f(rns[i])][f(rns[i+1])]==0)break;
    }
    if(i==c-1 && c==n && rns[i]<=(n+'A'-1))return 1;
    else if(c<t)return -1;
}

int main()
{
    int n,m,i,j;
    while(scanf("%d%d",&n,&m),m)
    {
        getchar();
        memset(map,0,sizeof(map));
        memset(hash,0,sizeof(hash));
        memset(front,0,sizeof(front));
        for(i=0;i<m;i++)
        {
            scanf("%s",s);
            list[i][0]=f(s[0]);list[i][1]=f(s[2]);
        }
        int ok=0;
        for(i=0;i<m;i++)
        {
            front[list[i][0]]++;hash[list[i][1]]++;
            map[list[i][0]][list[i][1]]=1;
            int k=solve(n);
            if(k==1){
                printf("Sorted sequence determined after %d relations: %s.\n",i+1,rns);
                ok=1;
                break;
            }
            else if(k==-1){
                printf("Inconsistency found after %d relations.\n",i+1);
                ok=1;
                break;
            }
        }
        if(ok==0)printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator