天天看點

2010華南農業大學校賽-Join a guild

描述

Almost every MMORPG(Massively Multiplayer Online Role-Playing Game) has a guild(公會) system. Every player can join one guild, and every guild can contain many players. Now you are designing a guild request system. Assume letter is player. Number is guild. Give you some joining event and request about a player or a guild. Your system tells me which guild the player belongs to or lists the guild’s member.

輸入

The first line of the input is an integer n (1<=n<=5000), means the total operation.

There are only two kind of operation which format like “J p g” and “R q”.

(0<=p<=9), (a<=g<=z)

輸出

The operation begins with “J” means the play “p” join the guild “g”. This operation has no output.

The operation begins with “R” means the request arguments is “q”.

If “q” is an integer, your system should list the guild-“q”‘s member in lexicographic order. If nobody in guild-“q” then output “empty”.

If “q” is a letter, your system should output the guild id which the player-“q” belongs to.

If the player didn’t join any guild, output “solo”.

樣例輸入

15

J a 1

R c

R 2

J a 2

J b 2

R a

R b

R 1

R 2

J c 3

J b 3

J c 3

R b

R 3

R 2

樣例輸出

solo

empty

2

2

empty

ab

3

bc

a

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    char str[5000];
    int i;
}q[100];
int cmp(const void *a,const void *b)
{
    return strcmp((*(node*)a).str,(*(node*)b).str);
}
int fun(char x)
{
    x=x-'0';
    if(q[x].i==0){cout<<"empty"<<endl;return 0;}
    //qsort(q,q[x].i,sizeof(q[x]),cmp);
    for(int i=0;i<q[x].i;i++)
        for(int j=i;j<q[x].i;j++)
           if(q[x].str[i]>q[x].str[j])
             {
                    char t;
                    t=q[x].str[i];
                    q[x].str[i]=q[x].str[j];
                    q[x].str[j]=t;
             }
    for(int i=0;i<q[x].i;i++)
        cout<<q[x].str[i];
    cout<<endl;

}
int fun1(char x)
{
   for(int i=0;i<10;i++)
   {
       if(q[i].i==0)continue;
       for(int j=0;j<=q[i].i;j++)
          if(int(q[i].str[j])==int(x))return i;
   }
   return -1;
}
void cz(char x)
{
  for(int i=0;i<10;i++)
   {
       if(q[i].i==0)continue;
       for(int j=0;j<=q[i].i;j++)
          if(int(q[i].str[j])==int(x))
          {
              while(j<=q[i].i-1)
                {
                    q[i].str[j]=q[i].str[j+1];
                     j++;
                }
              q[i].i--;
              break;
          }
   }
}
int main()
{
    int n,y;
    char m,x;
    while(cin>>n)
    {
        memset(q,0,sizeof(q));
        for(int i=0;i<n;i++)
        {
            cin>>m;
            if(m=='J')
            {
                cin>>x>>y;
                cz(x);
                q[y].str[q[y].i++]=x;

            }
            if(m=='R')
            {
                cin>>x;
                if(x>='0'&&x<='9')
                {
                    fun(x);
                }
                else
                {   int z=fun1(x);
                   if(z>=0)
                   cout<<z<<endl;
                   else cout<<"solo"<<endl;
                }
            }
        }
    }
    return 0;
}