Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中查找字符串的有效方法_Python_Algorithm_Python 2.7 - Fatal编程技术网

在python中查找字符串的有效方法

在python中查找字符串的有效方法,python,algorithm,python-2.7,Python,Algorithm,Python 2.7,给你两个字符串,AA和BB。查找是否存在子字符串 它同时出现在AA和BB中 所有字符串仅包含小写拉丁字母 上面,你看。我编写了以下程序来解决这个问题: T = int(raw_input()) for t in xrange(T): s1 = raw_input() s2 = raw_input() length1 = len(s1) length2 = len(s2) checked = list() if length1<length2

给你两个字符串,AA和BB。查找是否存在子字符串 它同时出现在AA和BB中

所有字符串仅包含小写拉丁字母

上面,你看。我编写了以下程序来解决这个问题:

T = int(raw_input())

for t in xrange(T):
    s1 = raw_input()
    s2 = raw_input()
    length1 = len(s1)
    length2 = len(s2)
    checked = list()
    if length1<length2:
        for letter in s1:
            if len(checked)== 26:
                break
            if letter in checked:
                next
            checked.append(letter)
            if letter in s2:
                print "YES"
                break
        else:
            print "NO"
    else:
        for letter in s2:
            if letter in checked:
                next
            if len(checked)==26:
                break
            checked.append(letter)
            if letter in s1:
                print "YES"
                break
        else:
            print "NO"
T=int(原始输入()
对于X范围内的t(t):
s1=原始输入()
s2=原始输入()
长度1=长度(s1)
长度2=长度(s2)
选中=列表()
如果长度1您的错误在这里:

if letter in checked:
    next
在Python中。如果字母in checked:next
是一个no op,那么使用
就如同使用
pass
一样,因为它只引用函数对象而不调用它。它肯定不会继续到下一个循环迭代

因此,无论选中的
字母的结果是什么,您都会继续将
字母
添加到选中的
中。由于选中的
是一个列表,而不是一个集合,因此您将向列表中添加重复项,并很容易得到26个以上的条目

使用:

并考虑使用一套用于“代码>检查< /COD> > < <代码> > <代码>成员资格测试的O(1)操作,而不是O(n).< 说到集合,这基本上是一个集合交问题;

s1
s2
中是否都出现了单个字母。您正在正确测试集合是否不相交;因此,请使用。在最坏的情况下,这将执行O(N*M)循环,但循环使用C代码:

print('NO' if set(s1).isdisjoint(s2) else 'YES')
如果不使用新集合,则只返回布尔值
set(s1)
在所有
s1
上循环以生成集合,
set.isdisjoint()
将在找到匹配项后尽早退出,每个匹配测试针对集合为O(1)

您可以看到基于长度交换
s1
s2
是否会改善您的测试计时:

if len(s1) > len(s2):
    s1, s2 = s2, s1
print('NO' if set(s1).isdisjoint(s2) else 'YES')

可以使用集合并查看交点是否大于零:

"yes" if set(s1).intersection(s2) else "no"
要避免重复整个列表,请执行以下操作:

"yes" if any(c in s1 for c in s2) else "no"
根据s1和s2的大小,您可能会受益于将其缩小为集合:

"yes" if any(c in set(s1) for c in set(s2)) else "no"

非常感谢。对不起,我没有这样做:考虑使用一个用于检查的集合来进行成员资格测试。我可以请你再解释一下使用示例代码的集合,或者让我参考一篇关于它的文章吗?@Abraham:and;Python列表需要O(N)个时间来测试元素是否存在,集合可以在固定时间内完成。@Abraham:so
checked=set()
创建集合,并
checked.add(letter)
向集合添加新字母<代码>如果选中字母:继续
跳过循环,因为您已经测试了字母。未意识到
isdisjoint()
存在。回答得好!
"yes" if any(c in set(s1) for c in set(s2)) else "no"