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到凌晨了...实在不知道了。

Posted by litter at 2009-08-24 01:17:07 on Problem 3487
#include <iostream>
#include <string.h>
using namespace std;

const int NUM=300;
int N;

struct Person
{
    bool be;//此位置有没有人
    bool marry;//是否订婚
    int step;//被拒绝到哪个位置
    char spouse;
    char per[NUM];//偏爱序列
}person[NUM];

void initialize()
{
    int i;
    for(i=0;i<NUM;i++)
    {
        person[i].be=0;
        person[i].marry=0;
        person[i].step=0;
        person[i].spouse='\0';
        memset(person[i].per,'\0',sizeof(person[i].per));
    }
}
void marryed(char a,char b)//a男 b女
{
    person[a].marry=1;
    person[b].marry=1;
    person[a].spouse=b;
    person[b].spouse=a;
}
bool prefer(int mm,int gg)
{
    char a=person[gg].spouse;int i;
    for(i=0;;i++)
    if(person[gg].per[i]==mm) return 1;
    else if(person[gg].per[i]==a) return 0;
}

void choose(char mm)
{
    char gg=person[mm].per[person[mm].step]; //下次该表白的男士
    if(!person[gg].marry)
    {
        marryed(gg,mm);
    }
    else
    {
        if(prefer(mm,gg))//男士悔婚,so 他的对象pre现在未婚;
        {
           char pre=person[gg].spouse;//pre为前未婚妻
             person[pre].spouse='\0';
           person[pre].marry=0;
           marryed(gg,mm);//成全新好上的
           choose(pre);//让被甩的再找一个,此处递归应该没有错啊。
        }
        else
        {
           person[mm].step++;//继续往下一个寻找对象
           choose(mm);
        }
    }
}

int main()
{
    int cases;
    scanf("%d",&cases);
    while(cases--)
  {
    scanf("%d",&N);
    initialize();
    int i;
    for(i=0;i<2*N;i++)
    {
        char name;
        scanf(" %c",&name);
        person[name].be=true;
    }
    getchar();
    for(i=0;i<2*N;i++)
    { 
        char per[NUM];
        scanf("%s",per);
        int j;
        for(j=2;j<strlen(per);j++)
        person[per[0]].per[j-2]=per[j];
    }
    for(i='A';i<='Z';i++)
    if(person[i].be&&!person[i].marry) choose(i);
    for(i='a';i<='z';i++)
    if(person[i].be) printf("%c %c\n",i,person[i].spouse);
    putchar('\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