题目描述:
给定01序列S,序列S是优秀的01序列,优秀的01序列定义如下:
1 如果序列S,T是优秀的,则序列S+T是优秀的,+被定义为按数学连接两个序列,即”010”+”110”=”010110”
2 如果序列S是优秀的,则序列rev(S)也是优秀的。rev(S) 被定义为按位翻转(0变1,1变0)序列S,并删去前导零。例如rev(” 1100101”)=“11010”
现在请你判断序列T是不是优秀的
输入描述
第一行数据组数T,表示有T组数据
每组数据的第一行是一个01序列,表示序列S。第二行是另一个01序列,表示序列T 1<=|S|,|T|<=1000,S,T不含前导零。
输出描述
对于每组数据,一行输出 YES或 NO,表示序列T是不是优秀的(大小写敏感)
思路:当时没有理解题意,想了好久也没弄明白优秀序列是什么,交了卷就突然明白了。。。比如S = 10101010是优秀的,res(S) = 1010101也是优秀的,一直推下去,就能根据S得到好多优秀序列,然后根据第一条性质,优秀序列加优秀序列还是优秀序列,看T是不是能由这些组成,类似leetcode139。
import sys
n = [int(x) for x in sys.stdin.readline().strip().split()]
S = sys.stdin.readline().strip().split()
T = sys.stdin.readline().strip().split()
S = str(S[0])
T = str(T[0])
def rev(string):
if not string:
return None
temp = ''
for i in string:
if i == '1':
temp = temp + '0'
else:
temp = temp + '1'
l = len(temp)
flag = 0
for i in range(l):
if temp[i] == '1':
string = temp[i:]
flag = 1
break
if flag == 0:
return []
return string
excellent = []
while S:
excellent.append(S)
S = rev(S)
#leetcode139,给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一 个或多个在字典中出现的单词。
def wordBreak(s, wordDict):
len_dic = len(wordDict)
len_str = len(s)
dp = [0] * len_str
for i in range(len_str):
for j in range(len_dic):
len_j = len(wordDict[j])
if i + 1 >= len_j and wordDict[j] == s[i + 1 - len_j:i + 1] and (i + 1 - len_j == 0 or dp[i - len_j] == 1):
dp[i] = 1
break
return dp[len_str - 1] == 1
if wordBreak(T, excellent):
print('Yes')
else:
print('No')