1350. Canteen
Time limit: 1.0 second Memory limit: 64 MB
It’s dangerous to eat in a canteen — you may be poisoned by a not fresh food. One may fall into a coma because of the canteen’s chicken and the other feels OK. And vice versa. The food is cooked from
M
different food stuffs. There are
N
different food stuffs in the menu but not all of them are at the distribution. Assume that
K + 1 students eat the food and we know for each student what products may poison him. The first student eats and he is not poisoned. How the dinner affect on the other students?
Input
The first line contains an integer
N
(1 ≤
N ≤ 100). The next
N
lines contain the food stuffs names — non-empty sequences of Latin letters and digits with length not more than 40 symbols. Then there is a number
K
(1 ≤
K ≤ 100) and
K + 1 blocks describing the menu food stuffs dangerous for the canteen visitors afterwards. The
i
th
block starts with a line with an integer
Ni — an amount of dangerous food stuffs and then there are
Ni
lines with the names of those dangerous stuffs (0 ≤
Ni ≤
N). The first block describes the food stuffs dangerous for the first student, the next
K
blocks — for the rest ones. The input ends with the line containing an integer
M
(0 ≤
M ≤
N).
Output
K
lines — the
i
th
line should contain:
- YES, if the dinner is harmless for the (i + 1)st student,
- NO, if among the food stuffs there is a dangerous one for the (i + 1)st student,
- MAYBE, if there may be different situations under the given conditions.
Sample
input | output |
---|---|
7 Rafinad Kefir Pastila Smetana Chokolade Kljukva Imbir 3 3 Rafinad Kefir Imbir 1 Rafinad 3 Kefir Kljukva Smetana 2 Imbir Smetana 3 | YES NO MAYBE |
Problem Author: Pavel Egorov
Problem Source: USU Junior Championship March'2005
***********************************************************************************************
不愧为大水题,一次AC,(用字典树)
不愧为大水题,一次AC,(用字典树)
***********************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 int n,k,i,j; 9 char str[1000][105]; 10 char c[1000]; 11 int stu[1000]; 12 int food[1000]; 13 int stufod[1000][1000]; 14 struct tree 15 { 16 int ve; 17 struct tree *p[131]; 18 tree() 19 { 20 int i; 21 ve=-1; 22 for(i=0;i<131;i++) 23 p[i]=NULL; 24 } 25 }; 26 int ans; 27 tree *root; 28 int insert(char *s) 29 { 30 int len=strlen(s); 31 tree *r=root; 32 for(int ig=0;ig<len;ig++) 33 { 34 if(r->p[s[ig]]==NULL) 35 r->p[s[ig]]=new tree(); 36 r=r->p[s[ig]]; 37 } 38 if(r->ve==-1) 39 { 40 r->ve=ans; 41 ans++; 42 } 43 return r->ve; 44 } 45 int b; 46 int ms; 47 int sum=0; 48 int main() 49 { 50 cin>>n; 51 root=new tree(); 52 getchar(); 53 ans=1; 54 for(i=0;i<n;i++) 55 scanf("%s",str[i]); 56 cin>>k; 57 cin>>b; 58 for(j=0;j<b;j++) 59 { 60 scanf("%s",c); 61 food[insert(c)]=1; 62 } 63 for(i=0;i<n;i++) 64 { 65 if(food[insert(str[i])]==0) 66 { 67 food[insert(str[i])]=0; 68 sum++; 69 } 70 } 71 for(i=1;i<=k;i++) 72 { 73 cin>>stu[i]; 74 for(j=1;j<=stu[i];j++) 75 { 76 scanf("%s",c); 77 stufod[i][j]=insert(c); 78 } 79 80 } 81 cin>>ms; 82 for(i=1;i<=k;i++) 83 { 84 int gs=1; 85 int suma=0; 86 for(j=1;j<=stu[i];j++) 87 { 88 if(food[stufod[i][j]]==0) 89 suma++; 90 } 91 if(suma>0) 92 { 93 if(sum-suma<ms) 94 cout<<"NO"<<endl; 95 else 96 cout<<"MAYBE"<<endl; 97 } 98 else 99 cout<<"YES"<<endl; 100 } 101 return 0; 102 103 }