nowcoder3274D binary
problem
给定一个01串s,定义rev(x)表示逐位翻转(0变1,1变0)x后并删去前导零后所得到的串。好的串定义如下:
s是好的串
如果x是好的串,则rev(x)也是好的串
如果a,b是好的串,则a+b(a,b按顺序拼接)也是好的串
你需要判断串t是否为好的
s,t保证不含前导零
solution
将s不断翻转,直到消失,中间过程中得到的串都是好的。这个比较显然。
然后将上面得到的某个串(假设为a)分别翻转(但是保留前导0)得到b,那么b也是好的。因为如果我们再拼接的时候想拼接上一个b串,那么就可以先在之前的串上加一个形如\(111000\)的串,然后rev一下,然后拼接上a,然后rev一下。这样就可以拼接上一个b串了。
具体实现,因为可以\(n^2\)dp。为了方便操作,可以先将s和t都反过来。这样所有的后缀就都变成了前缀。用\(f[i]\)表示前i个串是否可以拼接出来。如果\(f[i]\)可以拼接出来的话,那么就枚举一个\(j\in[1,n](n为s长度)\)。看\(t_i...t_{i+j}\)是否可以通过上述方式得到,并且更新f[i+j]的值即可。
code
/*
* @Author: wxyww
* @Date: 2019-12-21 08:07:44
* @Last Modified time: 2019-12-21 08:18:05
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1010;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0'; c = getchar();
}
return x * f;
}
char s[N],t[N];
int f[N];
int main() {
int T = read();
while(T--) {
scanf("%s",s + 1);
scanf("%s",t + 1);
int n = strlen(s + 1),m = strlen(t + 1);
memset(f,0,sizeof(f));
reverse(s + 1,s + n + 1);
reverse(t + 1,t + m + 1);
f[0] = 1;
for(int i = 0;i < m;++i) {
if(!f[i]) continue;
int k = t[i + 1] ^ s[1];
for(int j = 1;j <= n && i + j <= m;++j) {
if((s[j] ^ t[i + j]) != k) break;
if(s[j] != s[j + 1]) f[i + j] = 1;
}
}
puts(f[m] ? "YES" : "NO");
}
return 0;
}
nowcoder3274D binary的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- Rsync实现文件的同步
故事背景:我们公司是做新零售的,需要对发布的每台机器进行文件的同步更新,所以我这里做了一个小小的调研 技术调研:linux之间同步文件有两种方式rsync与scp. sync和scp在文件夹均不存在时 ...
- SQL Server之批量清理数据库的死锁
DECLARE killspid CURSOR FOR (SELECT CONVERT(VARCHAR(100), request_session_id) FROM sys.dm_tran_l ...
- 关于字符串的格式化----format与%
格式化字符串一般有两种方法 1.%(d整数,s字符,f浮点数) 2.format 用处极为广泛且限制不多 注意:第一种对于数组的传递会报TypeError,所以必须传递数组 a = (1, 2, 3) ...
- mysql登录错误:'Access denied for user 'root'@'localhost'
首先是不知道怎么忽然mysql用命令行,workbench都登录不了,都提示'Access denied for user 'root'@'localhost'. 数据库卸载重装了几次都不行.好像感觉 ...
- 生产环境Shell脚本Ping监控主机是否存活(多种方法)
在网上针对shell脚本ping监控主机是否存活的文档很多,但大多都是ping一次就决定了状态,误报率会很高,为了精确判断,ping三次不通再发告警,只要一次ping通则正常.于是,今天中午抽出点时间 ...
- 如何在Python中调用打包好的Jar文件?
首先是在anaconda中进入我这个项目对应的一个环境,然后在这个环境中下载并且安装jpype.那么就可以直接import了.但是这里出现了一系列的问题 第一个问题,getDefaultJVM()报错 ...
- Java类BufferedImage
https://docs.oracle.com/javase/7/docs/api/java/awt/image/BandedSampleModel.html
- centos安装jdk10
下载一个jdk10文件到linux : wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=ac ...
- 【pat】algorithm常用函数整理
reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...
- go语言之数组
1.go语言的数组和python的列表不一样,python的列表没有限定类型,而go的数组限定的类型,理由是这样的,请看下面的图 go语言的数组会数组中第一个值的内存地址,在我们上面的例子中,数组中的 ...