1772 C. Word Game

C. Word Game

题目

传送门:Problem - C - Codeforces

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<<" ";
		}
	}
} 

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值