您的位置:首页 > 其它

zoj 3960 What Kind of Friends Are You?

2017-04-27 17:00 369 查看
题目链接:What Kind of Friends Are You?

题目大意:给你一个T,代表T组数据,然后每组数据一个n一个q,代表n个朋友q个问题,然后是一个姓名的集合,代表当前所知道姓名的集合,然后是q次查询,每次查询都有一个姓名集合,之后是一个n*q的矩阵,代表第i次问询的姓名在第j个集合里出现没有,出现为一,否则为零,问是否存在且只存在一个姓名满足这个条件,如果存在则输出那个姓名,否则输出Let's go to the library!!

题目思路:模拟或者算二进制,模拟代码太挫,一直没过,然后换了个写法,我们可以看到q的值是小于等于21的,所以可以直接把每个数是否存在某个集合里面当成二进制来算,然后和矩阵每一行二进制算的值比较,相同就加加,如果只有一个就输出那个数字,否则就输出Let's go to the library!!具体看代码吧,很水的一道题,想了很久的解法

转化为二进制来算的想法

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int t,n,q,tot,c,poo[32],num;
string tol[1005],str;
map<string,int>mp;

int main(){
ios::sync_with_stdio(false);
poo[0] = 1;
for(int i = 1;i < 31;i++)
poo[i] = poo[i-1]*2;
cin>>t;
while(t--){
mp.clear();
cin>>n>>q;
cin>>tot;
for(int i = 0;i < tot;i++)
cin>>tol[i];
for(int i = 0;i < q;i++){
cin>>c;
for(int j = 0;j < c;j++){
cin>>str;
mp[str] += poo[i];
}
}
ll sum = 0;
for(int i = 0;i < n;i++){
sum = 0;
for(int j = 0;j < q;j++){
cin>>num;
if(num == 1)
sum += poo[j];
}
int ct = 0;
for(int k = 0;k < tot;k++){
if(mp[tol[k]] == sum) ct++;
}
if(ct != 1) cout<<"Let's go to the library!!"<<endl;
else {
for(int k = 0;k < tot;k++)
if(mp[tol[k]] == sum)
{cout<<tol[k]<<endl;break;}
}
}
}
return 0;
}


模拟的解法

#include <bits/stdc++.h>

using namespace std;

void solve(){
int n,q,m;
int mat[205][205];
memset(mat,0,sizeof(mat));
scanf("%d%d%d",&n,&q,&m);
string s[m+2];
map<string,int>mp;
for(int i = 0;i < m;i++){
cin>>s[i];
mp[s[i]] = i;
}
for(int i = 0;i < q;i++){
int k;cin>>k;
string tmp;
for(int j = 0;j < k;j++){
cin>>tmp;
mat[mp[tmp]][i] = 1;
}
}
int tmat[205];
for(int i = 0;i < n;i++){
for(int j = 0;j < q;j++) cin>>tmat[j];
int cnt = 0,ans = -1;
for(int j = 0;j < m;j++){
for(int k = 0;k < q;k++){
if(mat[j][k] != tmat[k]) break;
if(k == q-1){
ans = j;
cnt++;
}
}
}
if(cnt != 1) cout<<"Let's go to the library!!"<<endl;
else cout<<s[ans]<<endl;
}
}

int main(){
int t;
cin>>t;
while(t--) solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: