首页 > 试题广场 >

优秀的01序列

[编程题]优秀的01序列
  • 热度指数:774 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定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。
,S,T不含前导零。


输出描述:
对于每组数据,一行输出"YES"或者"NO",表示序列T是不是优秀的。(大小写敏感)
示例1

输入

1
1100
110011

输出

YES
示例2

输入

1
1000
100001111

输出

NO
def reverse(s):
    rev = []
    for c in s:
        if c == '0':
            rev.append('1')
        else:
            rev.append('0')
    return ''.join(rev).lstrip('0')

T = int(input())
for _ in range(T):
    s = input()
    t = input()
    # 根据判别条件构造可能的优秀序列
    result_pool = set()
    # 反转得到的优秀序列
    while s:
        result_pool.add(s)
        s = reverse(s)
    # "1"可以生成任意序列,因此单独拿出来判断一下,反转中有"1"T一定是优秀序列
    if '1' in result_pool&nbs***bsp;t in result_pool:
        # 如果T在反转的优秀序列中,则输出YES
        print('YES')
    else:
        # 否则检查拼接得到的序列是否优秀
        result_pool = list(result_pool)
        # 拼接得到优秀序列
        flag = False
        for i in range(len(result_pool) - 1):
            for j in range(i, len(result_pool)):
                if result_pool[i] + result_pool[j] == t&nbs***bsp;result_pool[j] + result_pool[i] == t:
                    flag = True
                    print('YES')
                    break
            if flag:
                break
        else:
            print('NO')

编辑于 2021-04-08 14:38:38 回复(0)
#python3
"""
rev----将序列x取反并将前置0去掉
cur----判断t是否存在于s及rev(s)的集合中或者s及rev(s)的两两拼接是否等于t
"""
def rev(x):
    j = 0
    y = ""
    for i in x:
        if i == "1" and j == 0:
            continue
        j += 1
        if i =="0":
            y += "1"
        else:
            y += "0"
    return y
def cur(s,t):
    tmp = set()
    while s:
        tmp.add(s)
        s1 = rev(s)
        s = s1
    if "1" in tmp or t in tmp:
        return("YES")
    tmp = list(tmp)
    for i in range(len(tmp)-1):
        for j in range(i,len(tmp)):
            if tmp[i]+tmp[j] == t and tmp[j]+tmp[i] == t:
                return("YES")
    return("NO")
n = int(input())
for _ in range(n):
    s = input()
    t = input()
    print(cur(s,t))

编辑于 2020-08-08 18:01:34 回复(7)
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < T; i++) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            System.out.println(solution(s1, s2));
        }
    }

    public static String solution (String s1, String s2) {
        ArrayList<String> list = new ArrayList<String>();
        String tmp = s1;
        while (tmp.length() > 1) {
            StringBuilder rev = new StringBuilder();
            for (int i = 0; i < tmp.length(); i++) {
                if (tmp.charAt(i) == '0')
                    rev.append('1');
                else
                    rev.append('0');
            }
            while (rev.length() > 1 && rev.charAt(0) == '0') {
                rev.deleteCharAt(0);
            }
            String l = rev.toString();
            tmp = l;
            if (!l.equals(""))
                list.add(l);
        }
        //如果出现了“1”,那么就需要额外加上一个“0”
        if (list.contains("1"))
            list.add("0");

        int index = 0;
        while (index < s2.length()) {
            int n = 0;
            if (index + s1.length() <= s2.length() && s2.substring(index, index + s1.length()).equals(s1))
                index += s1.length();
            else if (index < s2.length()) {
                for (int i = 0; i < list.size(); i++) {
                    String ss = list.get(i);
                    if (index + ss.length() <= s2.length() && s2.substring(index, index + ss.length()).equals(ss)) {
                        index += ss.length();
                        break;
                    }
                    else
                        n++;
                }
                if (n == list.size())
                    return "NO";
            }
            else
                return "NO";
        }
        return "YES";
    }
}

发表于 2020-04-06 18:07:33 回复(1)
动态规划
package LeetCode;
 
import java.util.List;
 
/*
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
说明:
拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:
输入: s = "leetcode", wordDict = ["leet", "code"]
输出: true
解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。
示例 2:
输入: s = "applepenapple", wordDict = ["apple", "pen"]
输出: true
解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。
     注意你可以重复使用字典中的单词。
示例 3:
输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
输出: false
 */
public class WordBreak {
    public boolean wordBreak(String s, List<String> wordDict) {
        int n=s.length();
        boolean[] dp=new boolean[n+1];
        dp[0]=true;
        for (int i=1;i<=n;i++){
            for (int j = 0; j <i ; j++) {
                if (dp[j]&&wordDict.contains(s.substring(j,i))){
                    dp[i]=true;
                    break;
                }
            }
        }
        return dp[n];
 
    }
}

发表于 2020-06-02 16:02:40 回复(1)
#只要s的末尾是"10"或“01”,那么s一直取反就能得到"1",“1"可以构成任何序列,可以节省判断时间

import
sys
n = int(input())

def rev(s):
    ret = ""
    cnt = 0
    for i in s:
        if i=="1" and cnt==0:
            continue
        if i=="1":
            ret += "0"
        else:
            ret += "1"
        cnt += 1
    return ret
def cur(s,t):
    temp = set()
    if s[-2:]=="10"or s[-2:]=="01":
        return "YES"
    while s:
        # print(s)
        temp.add(s)
        s = rev(s)
    # print(temp)
    if t in temp:
        return "YES"
    temp = list(temp)
    for i in range(len(temp)):
        for j in range(i-1,len(temp)):
            if temp[i]+temp[j]==t or temp[j]+temp[i]==t:
                return "YES"
    return "NO"

for i in range(n):
    s = input()
    t = input()
    print(cur(s,t))
发表于 2023-08-31 12:07:39 回复(0)
就我一个人没看懂题啥意思????
发表于 2020-09-12 04:02:56 回复(0)
#cur----将序列x取反并将前置0去掉
#res----判断t是否存在于s及rev(s)的集合中或者s及rev(s)的两两拼接是否等于t
def rev(x):
    j = 0
    y = ""
    for i in x:
        if j==0 and i == "1":
            continue
        j+=1
        if i == "0":
            y+="1"
        else:
            y+="0"
    return y
 
def cur(s,t):
    tmp = set()
    while s:
        tmp.add(s)
        s1 = rev(s)
        s = s1
    if "1" in tmp&nbs***bsp;t in tmp:
        return('YES')
    tmp = list(tmp)
    for i in range(len(tmp)-1):
        for j in range(i,len(tmp)):
            if tmp[i]+tmp[j]==t&nbs***bsp;tmp[j]+tmp[i] == t:
                return('YES')
    return('NO')
 
n = int(input())
for _ in range(n):
    s = input()
    t = input()
    print(cur(s, t))


发表于 2020-08-08 09:50:11 回复(0)
这里主要找出由S衍生的一次优秀01序列,将其转化成T是否由S的衍生优秀序列组成的问题。但是如果S的衍生序列中如果包含“1”这个序列,说明他可以构成任何序列,因此“1”的前一个序列可能是“11..10”这种东西,所以和"1"可以构成任何序列
发表于 2020-03-09 22:25:42 回复(3)