Home > other >  Help me algorithm problem!
Help me algorithm problem!

Time:01-08


Has been a timeout, how to optimize?
The class Solution:
L={}
Def equationsPossible (self, equations: List (STR)) - & gt; Bool:
For I in range (27) :
Self. L [CRH (word (' a ') + I)]=CRH (word (' a ') + I)
T=[]
For I in equations:
If '==' in I:
L=i. plit ('==')
Self. Found (l [0], l [1])
The else:
T.a ppend (i. plit ('!='))
For I in T:
If the self. The aaa (self. L [I [0]])==self. The aaa (self) L [I [1]]) :
Return False
Return True

Def found (self, the pre, last) :
If self. L [last]==last:
Self. L [last]=self. L (pre)
The else:
The last=self. Aaa (last)
Self. L [last]=self. L (pre)
Def aaa (self, the last) :
While the self L [last]!=last:
The last=self. L [last]
Return the last

CodePudding user response:

First==relationship can be passed, that is, a==b, implied a=b==c=c, we can set up a table of ta=[], and then input the==the letters on both sides of the join ta, then!=the letters out and on both sides of the test: test the two letters are in the ta, if is false; If not, then continue to test next!=the letters, on both sides of the code:
Def solution (input_alg) :
Ta=[]
For x in input_alg:
If len (x)!=4:
Return False
If x [1-3]=='==' :
X_split=x.s plit ('==')
If x_split [0] not in ta:
Ta. Append (x_split [0])
If x_split [1] not in ta:
Ta. Append (x_split [1])
For x in input_alg:
If x [1-3]=='!=':
X_split=x.s plit ('!=')
If x_split [0] in ta and x_split [1] in ta:
Return False
Return True


CodePudding user response:

Made a mistake, transfer is not necessarily value in a sequence,

CodePudding user response:


Def solution (input_alg) :
Ta={}
For x in input_alg:
If len (x)!=4:
Return False
If x [1-3]=='==' :
X_split=x.s plit ('==')
U=x_split [0]; V=x_split [1]
If u in ta and v in ta:
U_lab=ta [u]
V_lab=ta [v]
If u_lab==v_lab: continue
For v1 in ta:
If ta/v1==v_lab:
Ta/v1=u_lab
Elif u in ta and v not in ta:
=ta ta [v] [u]
Elif v in ta and u not in ta:
=ta ta [u] [v]
The else:
Ta [u]=u; Ta [v]=u
For x in input_alg:
If x [1-3]=='!=':
X_split=x.s plit ('!=')
U=x_split [0]; V=x_split [1]
If u in ta and v in ta and ta==ta [v] [u] :
Return False
Return True
Define a dictionary in the ta, will play the same '==' letters on both sides of the label, and can be passed, transmission rule is if a==b, the tag dictionary of ta and b the same letter must be exceptional a label, then use contain '!='equation to check, if there is a certain'!='letters on both sides of the label is the same, then the equations,

CodePudding user response:

 def f (eqs) : 
S=the set ()
Sp=[(eq [0], eq [1, 1], eq [1]) for eq in eqs]
[s.u pdate ((x, y)) for x, e, y in sp if e=='==']
Return all ([not (in x and y in s or s x==y) for x, e, y in sp if e=='!='])

A!=f ([' a '])
F ([' a!=b])
F ([' a==b ', 'b==c', 'c!=a'])
F ([' a!=b ', 'b!=c, c!=' a '])
F ([' a==b ', 'b==c', 'c==a'])
F ([' a==b ', 'b!=a'])
F ([' a==b ', 'b!=c])
F ([' a==b ', 'b==c', 'c!=d', 'd!=a'])
F ([' a!=b ', 'b!=c', 'c!=d', 'd==a'])

Results: FTFTTFTTT

CodePudding user response:

Try f ([' a==b ', 'b==c', 'c!=e', 'd==e']), the result of your function for f, in fact is T,
  • Related