C. Word Game
文章目录
题目
Three guys play a game: first, each person writes down nn distinct words of length 33. Then, they total up the number of points as follows:
- if a word was written by one person — that person gets 3 points,
- if a word was written by two people — each of the two gets 1 point,
- if a word was written by all — nobody gets any points.
In the end, how many points does each player have?
Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤10001≤n≤1000) — the number of words written by each person.
The following three lines each contain nn distinct strings — the words written by each person. Each string consists of 33 lowercase English characters.
Output
For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the ii-th integer should be the number of points earned by the ii-th guy.
Example
input
Copy
3
1
abc
def
abc
3
orz for qaq
qaq orz for
cod for ces
5
iat roc hem ica lly
bac ter iol ogi sts
bac roc lly iol iat
output
Copy
1 3 1
2 2 6
9 11 5
Note
In the first test case:
- The word abcabc was written by the first and third guys — they each get 11 point.
- The word defdef was written by the second guy only — he gets 33 points.
题解
可以用map来做;
先提供一个暴力做法,它的时间复杂度是O(n^2),这题无疑是过不了的;
#include<bits/stdc++.h>
using namespace std;
bool find1(string a,vector<string> b){
auto x = find(b.begin() ,b.end() , a);
if(x!=b.end())return true;
else return false;
}
void solve()
{
vector<string> a, b ,c,d;
int n ;
cin>>n;
for(int i = 0 ;i < n; i++ )
{
string str;
cin>>str;
a.push_back(str);
d.push_back(str);
}
for(int i = 0 ;i < n; i++ )
{
string str;
cin>>str;
b.push_back(str);
d.push_back(str);
}
for(int i = 0 ;i < n; i++ )
{
string str;
cin>>str;
c.push_back(str);
d.push_back(str);
}
int x=0 , y=0 , z=0 ;
sort(d.begin(),d.end());
d.erase(unique(d.begin(),d.end()) ,d.end());
/*A*/
for(int i = 0 ;i < n; i++ )
{
if( find1(a[i],b) && find1(a[i],c) )continue;
else if(!find1(a[i],b) && !find1(a[i],c) )
{
x+=3;
}
else if(find1(a[i],b) && !find1(a[i],c) )
{
x+=1;
}
else if(!find1(a[i],b) && find1(a[i],c) )
{
x+=1;
}
}
/*B*/
for(int i = 0 ;i < n; i++ )
{
if( find1(b[i],a) && find1(b[i],c) )continue;
else if(!find1(b[i],a) && !find1(b[i],c) )
{
y+=3;
}
else if(find1(b[i],a) && !find1(b[i],c) )
{
y+=1;
}
else if(!find1(b[i],a) && find1(b[i],c) )
{
y+=1;
}
}
/*C*/
for(int i = 0 ;i < n; i++ )
{
if( find1(c[i],b) && find1(c[i],a) )continue;
else if(!find1(c[i],b) && !find1(c[i],a) )
{
z+=3;
}
else if(find1(c[i],b) && !find1(c[i],a) )
{
z+=1;
}
else if(!find1(c[i],b) && find1(c[i],a) )
{
z+=1;
}
}
cout<<x<<" "<<y<<" "<<z<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}
接着用map优化这个问题:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
map<string , int> mp;
string a[4][1010];
int n;
cin>>n;
for(int i = 1 ; i <= 3 ; i++ )
{
for(int j = 1 ; j <= n ; j++ )
{
cin>>a[i][j];
mp[a[i][j]]++;
}
}
for(int i = 1 ; i <= 3 ; i++)
{
int cnt = 0;
for(int j = 1 ; j <= n ; j++)
{
if(mp[a[i][j] ] == 1)cnt+=3;
else if(mp[a[i][j]] == 2)cnt+=1;
}
cout<<cnt<<" ";
}
}
}
}