HackerRank Hiring Contest - B Winning Lottery Ticket

本文介绍了一个算法挑战,目标是找出所有可能的彩票对,这些彩票对通过组合它们的ID能够覆盖从0到9的所有数字至少一次。文章提供了一个具体的实现方案,包括状态压缩和遍历策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The SuperBowl Lottery is about to commence, and there are several lottery tickets being sold, and each ticket is identified with a ticket ID. In one of the many winning scenarios in the Superbowl lottery, a winning pair of tickets is:

  • Concatenation of the two ticket IDs in the pair, in any order, contains each digit from  to  at least once.

For example, if there are  distinct tickets with ticket ID  and  is a winning pair. 

NOTE: The ticket IDs can be concantenated in any order. Digits in the ticket ID can occur in any order. 

Your task is to find the number of winning pairs of distinct tickets, such that concatenation of their ticket IDs (in any order) makes for a winning scenario. Complete the function winningLotteryTicket which takes a string array of ticket IDs as input, and return the number of winning pairs. 

Input Format

The first line contains  denoting the total number of lottery tickets in the super bowl. 
Each of the next  lines contains a string, where string on a  line denotes the ticket id of the  ticket.

Constraints

  •  length of  
  • sum of lengths of all 
  • Each ticket id consists of digits from 

Output Format

Print the number of pairs in a new line.

Sample Input 0

5
129300455 
5559948277
012334556 
56789
123456879

Sample Output 0

5

Explanation 0

Pairs of distinct tickets that make for a winning scenario are : 

Ticket ID 1 Ticket ID 2 Winning Pair

Notice that each winning pair has digits from  to  atleast once, and the digits in the ticket ID can be of any order. Thus, the number of winning pairs is .



题意:

找出有几对数字,组合后包含0-9;


POINT:

先状态压缩。缩成1023个状态。

然后就for(n)*for(1023)开始暴力。

注意1023这个状态,要C(2,x)。不要重复计算了。



#include <iostream>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e6+6;
const int inf = 0x3f3f3f3f;
int num[2000];
int ss[maxn];
int main()
{

	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		char s[maxn];
		scanf("%s",s);
		int len=strlen(s);
		int now = 0;
		for(int i=0;i<len;i++){
			now=now|(1<<(s[i]-'0'));
		}
		num[now]++;
		ss[i]=now;
	}
	LL ans = 0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=1050;j++){
			if(ss[i]==j) continue;
			if((ss[i]|j)==(1<<10)-1){
				ans+=(LL)num[j];
			}
		}
	}
	LL x = num[1023];
	ans=ans/2+x*(x-1)/2;
	printf("%lld\n",ans);

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值