给定一个字符串数组arr[] ,其中每个arr[i]的形式为“i==j”或“i!=j” ,其中i和j是表示它们之间关系的变量,任务是检查是否可以为满足所有关系的变量赋值。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {“i==j”, ” j!=i”}
Output: No
Explanation:
First relation holds true for values i = 1 and j = 1, but the second relation fails for the same set of values. Therefore, print No.
Input: arr[] = {“p==q”, “q==r”, “p==r”]
Output: Yes
Explanation:
The assignment of the value 1 in p, q and r satisfies all 3 relations. Therefore, print “Yes”.
方法:解决给定问题的方法是使用联合查找算法。思路是遍历字符串数组,遍历每一个等式关系,对两个变量进行联合运算。遍历之后,去每个字符串中的每个非等式关系,检查两个变量的父变量是否相同。请按照以下步骤解决问题:
- 用0和一个变量answer初始化一个大小为26的数组parent[]为true以存储所需的结果。
- 使用变量i遍历数组parent[] ,并将parent[i]设置为i 。
- 现在,遍历每个字符串S字符串的阵列中,并且如果S的值[I] [1]是‘=’,然后在S执行联合操作[I] [0 – ‘A’]和S [I] [ 3 – ‘a’] 。
- 再次,遍历每个字符串S字符串的阵列中并执行以下操作:
- 如果S[i][1] 的值等于‘!’ ,将S[i][0 – ‘a’]和S[i][3 – ‘a’]的父节点分别存储在X和Y 中。
- 如果X的值等于Y ,则将答案设置为false 。
- 经过上述步骤,如果答案的值为真则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find parent of each node
int find(int v, vector& parent)
{
// If parent[v] is not v, then
// recursively call to find the
// parent of parent[v]
if (parent[v] != v)
return parent[v] = find(parent[v],
parent);
// Otherwise, return v
return v;
}
// Function to perform union of the
// two variables
void unions(int a, int b,
vector& parent)
{
// Stores the parent of a and b
int x = find(a, parent);
int y = find(b, parent);
// If they are not equal, make
// parent[x] = parent[y]
if (x != y)
parent[x] = parent[y];
}
// Funtion to check whether it is
// possible to assign values to
// variables to satisfy relations
bool equationsPossible(
vector& relations)
{
// Initialize parent array as 0s
vector parent(26, 0);
// Iterate in range [0, 25]
// and make parent[i] = i
for (int i = 0; i < 26; i++) {
parent[i] = i;
}
// Store the size of the string
int n = relations.size();
// Traverse the string
for (auto string : relations) {
// Check if it is of the
// form "i==j" or not
if (string[1] == '=')
// Take union of both
// the variables
unions(string[0] - 'a',
string[3] - 'a',
parent);
}
// Traverse the string
for (auto string : relations) {
// Check if it is of the
// form "i!=j" or not
if (string[1] == '!') {
// Store the parent of
// i and j
int x = find(string[0] - 'a',
parent);
int y = find(string[3] - 'a',
parent);
// If they are equal,
// then return false
if (x == y)
return false;
}
}
return true;
}
// Driver Code
int main()
{
vector relations
= { "i==j", "j!=i" };
if (equationsPossible(relations)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find parent of each node
static int find(int v, ArrayList parent)
{
// If parent[v] is not v, then
// recursively call to find the
// parent of parent[v]
if (parent.get(v) != v)
{
parent.set(v, find(parent.get(v), parent));
return parent.get(v);
}
// Otherwise, return v
return v;
}
// Function to perform union of the
// two variables
static void unions(int a, int b,
ArrayList parent)
{
// Stores the parent of a and b
int x = find(a, parent);
int y = find(b, parent);
// If they are not equal, make
// parent[x] = parent[y]
if (x != y)
parent.set(x, parent.get(y));
}
// Funtion to check whether it is
// possible to assign values to
// variables to satisfy relations
static boolean equationsPossible(ArrayList relations)
{
// Initialize parent array as 0s
ArrayList parent = new ArrayList();
for(int i = 0; i < 26; i++)
parent.add(0);
// Iterate in range [0, 25]
// and make parent[i] = i
for(int i = 0; i < 26; i++)
{
parent.set(i, i);
}
// Store the size of the string
int n = relations.size();
// Traverse the string
for(String str : relations)
{
// Check if it is of the
// form "i==j" or not
if (str.charAt(1) == '=')
// Take union of both
// the variables
unions((int)str.charAt(0) - 97,
(int)str.charAt(3) - 97,
parent);
}
// Traverse the string
for(String str : relations)
{
// Check if it is of the
// form "i!=j" or not
if (str.charAt(1) == '!')
{
// Store the parent of
// i and j
int x = find((int)str.charAt(0) - 97,
parent);
int y = find((int)str.charAt(3) - 97,
parent);
// If they are equal,
// then return false
if (x == y)
return false;
}
}
return true;
}
// Driver Code
public static void main (String[] args)
{
ArrayList relations = new ArrayList(
Arrays.asList("i==j", "j!=i"));
if (equationsPossible(relations))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by rag2127
Python3
# Python3 program for the above approach
# Function to find parent of each node
def find(v, parent):
# If parent[v] is not v, then
# recursively call to find the
# parent of parent[v]
if (parent[v] != v):
parent[v] = find(parent[v], parent)
return parent[v]
# Otherwise, return v
return v
# Function to perform union of the
# two variables
def unions(a, b, parent):
# Stores the parent of a and b
x = find(a, parent)
y = find(b, parent)
# If they are not equal, make
# parent[x] = parent[y]
if (x != y):
parent[x] = parent[y]
# Funtion to check whether it is
# possible to assign values to
# variables to satisfy relations
def equationsPossible(relations):
# Initialize parent array as 0s
parent = [0]*(26)
# Iterate in range [0, 25]
# and make parent[i] = i
for i in range(26):
parent[i] = i
# Store the size of the string
n = len(relations)
# Traverse the string
for string in relations:
# Check if it is of the
# form "i==j" or not
if (string[1] == '='):
# Take union of both
# the variables
unions(ord(string[0]) - ord('a'),ord(string[3]) - ord('a'), parent)
# Traverse the string
for string in relations:
# Check if it is of the
# form "i!=j" or not
if (string[1] == '!'):
# Store the parent of
# i and j
x = find(ord(string[0]) - ord('a'), parent)
y = find(ord(string[3]) - ord('a'), parent)
# If they are equal,
# then return false
if (x == y):
return False
return True
# Driver Code
if __name__ == '__main__':
relations = ["i==j", "j!=i"]
if (equationsPossible(relations)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find parent of each node
static int find(int v, List parent)
{
// If parent[v] is not v, then
// recursively call to find the
// parent of parent[v]
if (parent[v] != v)
return parent[v] = find(parent[v],
parent);
// Otherwise, return v
return v;
}
// Function to perform union of the
// two variables
static void unions(int a, int b,
List parent)
{
// Stores the parent of a and b
int x = find(a, parent);
int y = find(b, parent);
// If they are not equal, make
// parent[x] = parent[y]
if (x != y)
parent[x] = parent[y];
}
// Funtion to check whether it is
// possible to assign values to
// variables to satisfy relations
static bool equationsPossible(List relations)
{
// Initialize parent array as 0s
List parent = new List();
for(int i=0;i<26;i++)
parent.Add(0);
// Iterate in range [0, 25]
// and make parent[i] = i
for (int i = 0; i < 26; i++) {
parent[i] = i;
}
// Store the size of the string
int n = relations.Count;
// Traverse the string
foreach( string str in relations) {
// Check if it is of the
// form "i==j" or not
if (str[1] == '=')
// Take union of both
// the variables
unions((int)str[0] - 97,
(int)str[3] - 97,
parent);
}
// Traverse the string
foreach (string str in relations) {
// Check if it is of the
// form "i!=j" or not
if (str[1] == '!') {
// Store the parent of
// i and j
int x = find((int)str[0] - 97,
parent);
int y = find((int)str[3] - 97,
parent);
// If they are equal,
// then return false
if (x == y)
return false;
}
}
return true;
}
// Driver Code
public static void Main()
{
List relations = new List{ "i==j", "j!=i" };
if (equationsPossible(relations)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
// This code is contributed by bgangwar59.
No
时间复杂度: O(N*log M),其中M是 arr[] 中唯一变量的数量。
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live