What’s In A Name?
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2083 Accepted: 741
Description
The FBI is conducting a surveillance of a known criminal hideout which serves as a communication center for a number of men and women of nefarious intent. Using sophisticated decryption software and good old fashion wiretaps, they are able to decode any e-mail messages leaving the site. However, before any arrest warrants can be served, they must match actual names with the user ID’s on the messages. While these criminals are evil, they’re not stupid, so they use random strings of letters for
their ID’s (no dillingerj ID’s found here). The FBI knows that each criminal uses only one ID. The only other information they have which will help them is a log of names of the people who enter and leave the hideout. In many cases, this is enough to link the names to the ID’s.
Input
Input consists of one problem instance. The first line contains a single positive integer n indicating the number of criminals using the hideout. The maximum value for n will be 20. The next line contains the n user ID’s, separated by single spaces. Next will be the log entries in chronological order. Each entry in the log has the form type arg , where type is either E, L or M: E indicates that criminal arg has entered the hideout; L indicates criminal arg has left the hideout; M indicates a message was intercepted from user ID arg. A line containing only the letter Q indicates the end of the log. Note that not all user ID’s may be present in the log but each criminal name will be guaranteed to be in the log at least once. At the start of the log, the hideout is presumed to be empty. All names and user ID’s consist of only lowercase letters and have length at most 20. Note: The line containing only the user ID’s may contain more than 80 characters.
Output
Output consists of n lines, each containing a list of criminal names and their corresponding user ID’s, if known. The list should be sorted in alphabetical order by the criminal names. Each line has the form name:userid , where name is the criminal’s name and userid is either their user ID or the string ??? if their user ID could not be determined from the surveillance log.
Sample Input
7
bigman mangler sinbad fatman bigcheese frenchie capodicapo
E mugsy
E knuckles
M bigman
M mangler
L mugsy
E clyde
E bonnie
M bigman
M fatman
M frenchie
L clyde
M fatman
E ugati
M sinbad
E moriarty
E booth
Q
Sample Output
bonnie:fatman
booth:???
clyde:frenchie
knuckles:bigman
moriarty:???
mugsy:mangler
ugati:sinbad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
 
using namespace std;
 
int n ;
int sum = 0; //
int is_in[25] ;
char name[25][25], id[25][25];
int g[25][25]; //
int inde[25];  //iinde[i]-1
int r[25]; //
 
const int MAXN = 25;
int uN,vN;
int linker[MAXN];
bool used[MAXN];
bool dfs(int u){
    int v;
    for(v=0;v<vN;v++)
        if(g[u][v]&&!used[v]){
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v])){
                linker[v]=u;
                return true;
            }
        }
    return false;
}
 
int hungary(){
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++){
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    }
    //cout << "res=" << res << endl;
    return res;
}
 
void solve(){
    int i, j, k;
    for(i = 0; i < n; i ++){
        for(j = 0; j < n; j ++){
            if(g[i][j]){
                g[i][j] = 0;
                if(hungary() < n){
                    g[i][j] = 1;
                    break;
                }
                g[i][j] = 1;
            }
        }
        //cout << "j=" << j << endl;
        if(j == n){
            inde[i] = -1;
        }
        else{
            inde[i] = j;
        }
    }
}
 
    inline int cmp(int i,int j){
       if (memcmp(name[i],name[j],sizeof name[i]) <= 0) return 1;
       else return 0;
    }
 
int main(){
    int i, j, k;
    scanf("%d", &n);
    uN = n;
    vN = n;
    int sum = 0;//
    memset(g, 1, sizeof(g));
    for(i = 0; i < n; i ++){
        scanf("%s", id[i]);
    }
    scanf("\n");
    char tmp;
    char stmp[25];
    for (scanf("%c",&tmp); tmp != 'Q'; scanf("%c",&tmp)){
        scanf("%s\n", stmp);
        switch(tmp){
            case 'E':
                for(i = 0; i < sum; i ++){
                    if(strcmp(stmp, name[i]) == 0)
                        break;
                }
                if(i == sum)
                    strcpy(name[sum ++], stmp);
                is_in[i] = 1;
                break;
            case 'L':
                for(i = 0; i < sum; i ++){
                    if(strcmp(stmp, name[i]) == 0)
                        break;
                }
                is_in[i] = 0;
                break  ;
            case 'M':
                for(i = 0; i < n; i ++){
                    if(strcmp(stmp, id[i]) == 0)
                        break;
                }
                for(j = 0; j < n; j ++){
                    if(!is_in[j])
                        g[j][i] = 0; //ij
                }
                break   ;
        }
    }
    solve();
    for(i = 0; i < n; i ++){
        r[i] = i;
    }
    sort(r, r + n, cmp);
    for(i = 0; i < n; i ++){
        printf("%s:", name[r[i]]);
        if(inde[r[i]] == -1)
            printf("???\n" )  ;
        else
            printf("%s\n", id[inde[r[i]]]);
    }
    return 0;
}