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