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 zwliu at 2012-07-04 20:31:01 on Problem 3487
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define maxn 28
int n, id[231];
struct M_G{
    int now, rank[maxn], ans;
    int get[34];
    char name[2];
}m[maxn], g[maxn];

void init(){
    scanf("%d", &n);
    for(int i=0; i<n; ++i) {
        scanf("%s", m[i].name);
        id[m[i].name[0] ]=i;
        m[i].now=0;
        m[i].ans=-1;
    }
    for(int i=0; i<n; ++i) {
        scanf("%s", g[i].name);
        id[g[i].name[0] ]=i;
        g[i].ans=-1;
    }
    char str[33];
    getchar();
    for(int i=0; i<n; ++i) {
        gets(str);
        int x, size=strlen(str), j=0, k=0;
        while(j < size){
            if(str[j]>='a' && str[j]<='z') x=id[str[j] ];
            if(str[j]>='A' && str[j]<='Z')
              m[x].rank[k++]=id[str[j] ];
            j++;
        }
    }
    for(int i=0; i<n; ++i) {
         gets(str);
         int x, size=strlen(str), j=0, k=0;
         while(j < size) {
              if(str[j]>='A' && str[j]<='Z') x=id[str[j] ];
              if(str[j]>='a' && str[j]<='z')
                 g[x].rank[k++]=id[str[j] ];
              ++j;
         }
    }
    /*for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j)  cout << g[i].rank[j];
        cout << endl;
    }*/
   // while(1);
}

void solve(){
    while(1) {
        for(int i=0; i<n; ++i) g[i].get[0]=0;
        for(int i=0; i<n; ++i) if(m[i].ans == -1) {
           int now=m[i].now;
           int y=m[i].rank[now];
           g[y].get[++g[y].get[0] ]=i;
           m[i].now++;
        }

        for(int i=0; i<n; ++i) if(g[i].get[0]){
            for(int j=0; j<n; ++j){
                int  x=g[i].rank[j], k;
                for(k=1; k<=g[i].get[0]; ++k)
                  if(x == g[i].get[k]) break;
                if(k <= g[i].get[0])
                {
                    if(g[i].ans == -1)  {
                        g[i].ans=x;
                        m[x].ans=i;
                    }
                    else {
                        int y=g[i].ans;
                        m[y].ans=-1;
                        g[i].ans=x;
                        m[x].ans=i;
                    }
                    break;
                }
            }
        }
        bool ff=1;
        for(int i=0; i<n; ++i)  if(m[i].ans == -1) {
            ff=0; break;
        }
        if( ff ) break;
    }
    for(int i=0; i<n; ++i)
      printf("%s %c\n", m[i].name, m[i].ans+'A');
}


int main(int argc, char**argv){
    int t; scanf("%d", &t);
    while( t-- ) {
        init();
        solve();
        if(t) puts("");
    }
    system("pause");
    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