HR_Sherlock and Anagrams_TIMEOUT[UNDONE]
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()
HR_Sherlock and Anagrams_TIMEOUT[UNDONE]的更多相关文章
- IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...
- C++ Standard Template Library STL(undone)
目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...
- Codeforces 1154 - A/B/C/D/E/F/G - (Undone)
链接:https://codeforces.com/contest/1154 A - Restoring Three Numbers - [水] #include<bits/stdc++.h&g ...
- Gym 101606 - A/B/C/D/E/F/G/H/I/J/K/L - (Undone)
链接:https://codeforces.com/gym/101606 A - Alien Sunset 暴力枚举小时即可. #include<bits/stdc++.h> using ...
- Codeforces 677 - A/B/C/D/E - (Undone)
链接: A - Vanya and Fence - [水] AC代码: #include<bits/stdc++.h> using namespace std; ; int n,h; in ...
- Codeforces 1114 - A/B/C/D/E/F - (Undone)
链接:http://codeforces.com/contest/1114 A - Got Any Grapes? 题意:甲乙丙三个人吃葡萄,总共有三种葡萄:绿葡萄.紫葡萄和黑葡萄,甲乙丙三个人至少要 ...
- 2016ACM/ICPC亚洲区沈阳站 - A/B/C/E/G/H/I - (Undone)
链接:传送门 A - Thickest Burger - [签到水题] ACM ICPC is launching a thick burger. The thickness (or the heig ...
- Codeforces 1043 - A/B/C/D/E/F - (Undone)
链接:http://codeforces.com/contest/1043 A - Elections - [水水水水题] 题意: 我和另一个人竞争选举,共有 $n$ 个人投票,每个人手上有 $k$ ...
- VS中特殊的注释——TODO/UNDONE/HACK的使用
在代码的后面添加形如下面注释: //TODO: (未实现)…… //UNDONE:(没有做完)…… //HACK:(修改)…… 等到再次打开VS的时候,找到 :视图>任务列表 即可显示所有带有T ...
随机推荐
- Java Core - 创建对象的两种方式
一.通过new关键字创建对象 Hello hello = null; // 声明一个引用 hello = new Hello(); // 创建对象 以上两行代码相当于 Hello hello ...
- linux下jenkins的时区设置问题
https://blog.csdn.net/king_wang10086/article/details/76178711 [root@jenkins ~]# yum install -y ntpda ...
- 获取环境变量,0x000000cb 操作系统找不到已输入的环境选项
include "stdafx.h" #include <Windows.h> #include <iostream> #pragma warning(di ...
- linux 服务器名 访问 shh免密码登录
以根用户登录,或者登录后切换到根用户,然后在提示符下输入hostname命令,可以看出当前系统的主机名为localhost.localdomain. 更改/etc/sysconfig下的netwo ...
- Html5使用canvas作图线宽很粗
自己使用canvas画图是碰到的问题,在这里记录一下.我把lineWidth设置为1,但是很粗,而且发虚.代码如下: <script type="text/javascript&quo ...
- [转帖]linux 清空history以及记录原理
linux 清空history以及记录原理 自己的linux 里面总是一堆 乱七八槽输错的命令 用这个办法 可以清空 linux的内容. 清爽一些. 1.当前session执行的命令,放置缓存中,执行 ...
- [转帖]AMOLED的技术和OLED有哪些联系和区别
AMOLED的技术和OLED有哪些联系和区别 https://display.ofweek.com/2018-06/ART-11000-2300-30243226.html 硬件资料 导读: ?虽然L ...
- Hbase数据结构模型
- kibana——es的批量操作
一·_mget: 1.创建的索引如下: 2.批量查询: #查询两个 GET _mget { "docs":[ { "_index":"testdb&q ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...