给定一个n个数字的数组。任务是从给定的对数中找出可以配对的对数,这些对数将包含从0到9的所有数字。
例子:
Input : num[][] = { “129300455”, “5559948277”, “012334556”, “56789”, “123456879” }
Output : 5
{“129300455”, “56789”}, { “129300455”, “123456879”}, {“5559948277”, “012334556”},
{“012334556”, “56789”}, {“012334556”, “123456879”} are the pair which contain all the digits from 0 to 9 on concatenation.
注意:每个数字中的数字可以是10 ^ 6。
想法是将每个数字表示为10位的掩码,这样,如果它至少包含数字i ,则第i位将在掩码中设置。
例如,
令n = 4556120然后第0,第1,第2,第4,第5,第6位将在掩模来设置。
因此,mask =(0001110111) 2 =(119) 10
现在,对于从0到2 ^ 10 – 1的每个掩码m ,我们将存储其掩码等于m的数字的计数。
因此,我们将创建一个数组,例如cnt [],其中cnt [i]存储其掩码等于i的数字的数量。伪代码为此:
for (i = 0; i < (1 << 10); i++)
cnt[i] = 0;
for (i = 1; i <= n; i++)
{
string x = p[i];
int mask = 0;
for (j = 0; j < x.size(); j++)
mask |= (1 << (x[j] - '0';);
cnt[mask]++;
}
如果在两个数字的掩码的按位或中设置了0到9的每个位,则一对数字将具有从0到9的所有数字,即等于(1111111111) 2 sub)=(1023)10
现在,我们将遍历所有按位或等于1023的掩码对,并添加许多方法。
以下是此方法的实现:
C++
// C++ Program to find number of pairs whose
// concatenation contains all digits from 0 to 9.
#include
using namespace std;
#define N 20
// Function to return number of pairs whose
// concatenation contain all digits from 0 to 9
int countPair(char str[N][N], int n)
{
int cnt[1 << 10] = { 0 };
// making the mask for each of the number.
for (int i = 0; i < n; i++) {
int mask = 0;
for (int j = 0; str[i][j] != '\0'; ++j)
mask |= (1 << (str[i][j] - '0'));
cnt[mask]++;
}
// for each of the possible pair which can
// make OR value equal to 1023
int ans = 0;
for (int m1 = 0; m1 <= 1023; m1++)
for (int m2 = 0; m2 <= 1023; m2++)
if ((m1 | m2) == 1023) {
// finding the count of pair
// from the given numbers.
ans += ((m1 == m2) ?
(cnt[m1] * (cnt[m1] - 1)) :
(cnt[m1] * cnt[m2]));
}
return ans / 2;
}
// Driven Program
int main()
{
int n = 5;
char str[][N] = { "129300455", "5559948277",
"012334556", "56789", "123456879" };
cout << countPair(str, n) << endl;
return 0;
}
Java
// Java Program to find number of pairs whose
// concatenation contains all digits from 0 to 9.
class GFG
{
static final int N = 20;
// Function to return number of pairs whose
// concatenation contain all digits from 0 to 9
static int countPair(char str[][], int n)
{
int[] cnt = new int[1 << 10];
// making the mask for each of the number.
for (int i = 0; i < n; i++)
{
int mask = 0;
for (int j = 0; j < str[i].length; ++j)
mask |= (1 << (str[i][j] - '0'));
cnt[mask]++;
}
// for each of the possible pair which can
// make OR value equal to 1023
int ans = 0;
for (int m1 = 0; m1 <= 1023; m1++)
for (int m2 = 0; m2 <= 1023; m2++)
if ((m1 | m2) == 1023)
{
// finding the count of pair
// from the given numbers.
ans += ((m1 == m2) ? (cnt[m1] * (cnt[m1] - 1)) :
(cnt[m1] * cnt[m2]));
}
return ans / 2;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
char str[][] = { "129300455".toCharArray(),
"5559948277".toCharArray(),
"012334556".toCharArray(),
"56789".toCharArray(),
"123456879".toCharArray() };
System.out.print(countPair(str, n) + "\n");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 Program to find
# number of pairs whose
# concatenation contains
# all digits from 0 to 9.
N = 20
# Function to return number
# of pairs whose concatenation
# contain all digits from 0 to 9
def countPair(st, n):
cnt = [0] * (1 << 10)
# Making the mask for
# each of the number.
for i in range (n):
mask = 0
for j in range (len(st[i])):
mask |= (1 << (ord(st[i][j]) - ord('0')))
cnt[mask] += 1
# for each of the possible
# pair which can make OR
# value equal to 1023
ans = 0
for m1 in range(1024):
for m2 in range (1024):
if ((m1 | m2) == 1023):
# Finding the count of pair
# from the given numbers.
if (m1 == m2):
ans += (cnt[m1] * (cnt[m1] - 1))
else:
ans += (cnt[m1] * cnt[m2])
return ans // 2
# Driven Program
if __name__ == "__main__":
n = 5
st = ["129300455", "5559948277",
"012334556", "56789", "123456879"]
print(countPair(st, n))
# This code is contributed by Chitranayal
C#
// C# Program to find number of pairs whose
// concatenation contains all digits from 0 to 9.
using System;
class GFG
{
static readonly int N = 20;
// Function to return number of pairs whose
// concatenation contain all digits from 0 to 9
static int countPair(String []str, int n)
{
int[] cnt = new int[1 << 10];
// making the mask for each of the number.
for (int i = 0; i < n; i++)
{
int mask = 0;
for (int j = 0; j < str[i].Length; ++j)
mask |= (1 << (str[i][j] - '0'));
cnt[mask]++;
}
// for each of the possible pair which can
// make OR value equal to 1023
int ans = 0;
for (int m1 = 0; m1 <= 1023; m1++)
for (int m2 = 0; m2 <= 1023; m2++)
if ((m1 | m2) == 1023)
{
// finding the count of pair
// from the given numbers.
ans += ((m1 == m2) ? (cnt[m1] * (cnt[m1] - 1)) :
(cnt[m1] * cnt[m2]));
}
return ans / 2;
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
String []str = {"129300455",
"5559948277",
"012334556",
"56789",
"123456879" };
Console.Write(countPair(str, n) + "\n");
}
}
// This code is contributed by Rajput-Ji
5
复杂度: O(n + 2 ^ 10 * 2 ^ 10)