网易2020笔试真题 优秀的01序列

题目描述:
给定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')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值