2019年1月10日15:39:23 去掉了所有不必要的循环区间 还是超时 本地运行大概3s
#!/bin/python3 import math import os import random import re import sys # Complete the sherlockAndAnagrams function below. def compareSubString(string1,string2): num_string1 = [0]*26 num_string2 = [0]*26 for index in range (len(string1)): temp = ord(string1[index]) - ord('a') num_string1[temp] +=1 temp = ord(string2[index]) - ord('a') num_string2[temp] +=1 if num_string1 == num_string2: return True else: return False def sherlockAndAnagrams(s): #s <= 100 n = len(s) count = 0 for index in range(n-1): #last one have no mean for length in range(1, n - index): #only length between [1,n-1] passible have Anagrams tempstring1 = s[index : index + length] if (index + 1 <= n - length ): for index2 in range(index + 1, n - length +1): tempstring2 = s[index2: index2 + length] if compareSubString(tempstring1 , tempstring2): count +=1 return count if __name__ == '__main__': # fptr = open(os.environ['OUTPUT_PATH'], 'w') q = 1 for q_itr in range(q): s = 'zjekimenscyiamnwlpxytkndjsygifmqlqibxxqlauxamfviftquntvkwppxrzuncyenacfivtigvfsadtlytzymuwvpntngkyhw' result = sherlockAndAnagrams(s) print(result)
------
注意 anagrammatic 是拼字游戏的意思,任意调换顺序可以组成对方都成立,不是简单的回文数字。
------
1.第一种解法想省些时间 结果都比测试集少了20多个 应该是有些规则题目没有描述清楚。
5
ifailuhkqqhucpoltgtyovarjsnrbfpvmupwjjjfiwwhrlkpekxxnebfrwibylcvkfealgonjkzwlyfhhkefuvgndgdnbelgruel
gffryqktmwocejbxfidpjfgrrkpowoxwggxaknmltjcpazgtnakcfcogzatyskqjyorcftwxjrtgayvllutrjxpbzggjxbmxpnde
mqmtjwxaaaxklheghvqcyhaaegtlyntxmoluqlzvuzgkwhkkfpwarkckansgabfclzgnumdrojexnrdunivxqjzfbzsodycnsnmw
ofeqjnqnxwidhbuxxhfwargwkikjqwyghpsygjxyrarcoacwnhxyqlrviikfuiuotifznqmzpjrxycnqktkryutpqvbgbgthfges
zjekimenscyiamnwlpxytkndjsygifmqlqibxxqlauxamfviftquntvkwppxrzuncyenacfivtigvfsadtlytzymuwvpntngkyhw
399
471
370
403
428
#!/bin/python3 import math import os import random import re import sys # Complete the sherlockAndAnagrams function below. def sherlockAndAnagrams(s): n = len(s) count = 0 # len = 1 && multi in string for i in range(n): for j in range(n): if j > i: if s[j] == s[i]: count += 1 if j - i > 1: count += 1 # repeat substring && huiwen substring for start in range(n): for end in range(n): if end - start > 1: # substring len > 1 substring_num = 0 reverse_substring_num = 0 str1 = s[start:end] str2 = s[end:] str3 = str1[::-1] substring_num = str2.count(str1) reverse_substring_num = str2.count(str3) count += substring_num if str1 != str3: # avoid kkk count += reverse_substring_num return count if __name__ == '__main__': s = 'zjekimenscyiamnwlpxytkndjsygifmqlqibxxqlauxamfviftquntvkwppxrzuncyenacfivtigvfsadtlytzymuwvpntngkyhw' result = sherlockAndAnagrams(s) # fptr = open(os.environ['OUTPUT_PATH'], 'w') # # q = int(input()) # # for q_itr in range(q): # s = 'abba' # # result = sherlockAndAnagrams(s) # # fptr.write(str(result) + '\n') # # fptr.close()
2.看s 最大也就100 直接列出所有子串排序求和就行 但是发现会超时,本地运行需要十几秒。。。:
#!/bin/python3 import math import os import random import re import sys # Complete the sherlockAndAnagrams function below. def sherlockAndAnagrams(s): n = len(s) count = 0 #find all substring arr = [] for i in range(n): for j in range(n+1): if j > i: arr.append(s[i:j]) # count count = 0 for i in range(len(arr)): for j in range(len(arr)): if j>i and len(arr[i] )== len(arr[j]): str1 = sorted(arr[i]) str2 = sorted(arr[j]) if str1 == str2 : count += 1 return count if __name__ == '__main__': s = 'mqmtjwxaaaxklheghvqcyhaaegtlyntxmoluqlzvuzgkwhkkfpwarkckansgabfclzgnumdrojexnrdunivxqjzfbzsodycnsnmw' result = sherlockAndAnagrams(s) # fptr = open(os.environ['OUTPUT_PATH'], 'w') # # q = int(input()) # # for q_itr in range(q): # s = 'abba' # # result = sherlockAndAnagrams(s) # # fptr.write(str(result) + '\n') # # fptr.close()
3.优化了下 改成字符串 还是超时:
#!/bin/python3 import math import os import random import re import sys # Complete the sherlockAndAnagrams function below. def sherlockAndAnagrams(s): n = len(s) count = 0 #find all substring arr = [] for i in range(n): for j in range(n+1): if j > i: arr.append(s[i:j]) # count count = 0 # for i in range(len(arr)): # for j in range(len(arr)): # if j>i and len(arr[i] )== len(arr[j]): # str1 = sorted(arr[i]) # str2 = sorted(arr[j]) # if str1 == str2 : # count += 1 for i in range(len(arr)): temp = "".join(sorted(arr[i])) arr[i] = temp for i in range(len(arr)): for j in range(i,len(arr)): if j>i and len(arr[i] )== len(arr[j]) and arr[i] == arr[j]: count += 1 return count if __name__ == '__main__': s = 'ifailuhkqqhucpoltgtyovarjsnrbfpvmupwjjjfiwwhrlkpekxxnebfrwibylcvkfealgonjkzwlyfhhkefuvgndgdnbelgruel' result = sherlockAndAnagrams(s) print(result) # fptr = open(os.environ['OUTPUT_PATH'], 'w') # # q = int(input()) # # for q_itr in range(q): # s = 'abba' # # result = sherlockAndAnagrams(s) # # fptr.write(str(result) + '\n') # # fptr.close()