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 ...
随机推荐
- 构建Electron的常见问题(Mac)
背景 起因是产品的需求,需要更换Electron为底层平台,但因为会有不少定制化的功能要实现,必须自己实现此类内容,所以也就导致必须自己编译Electron的源代码. 整个构建过程,看Electron ...
- [Go] 实现websocket服务端
直接使用官方子包可以实现websocket协议, golang.org/x/net/websocket 如果在这个目录没有这个包就去github下载net库,放进这个目录$GOPATH/src/gol ...
- Self Service Password 密码策略
1.在活动目录中新建一个用户,并赋予域管理员权限:2.拷贝conf目录下的config.inc.php为config.inc.local.php:3.按自己的实际情况及要求修改config.inc.l ...
- 清除Windows系统图标缓存
如果改变程序图标重新编译之后看到的图标并未改变,这可能不windows缓存了之前的图标导致的,需要清除Window的图标缓存来显示正确的图标. 下面是清除Windows系统图标缓存的批处理代码: re ...
- BZOJ2820/LG2257 YY的GCD 莫比乌斯反演
问题描述 BZOJ2820 LG2257 题解 求 \(\sum\limits_{i=1}^{n}{\sum\limits_{j=1}^{m}{[gcd(i,j)==p]}}\) ,其中 \(p\)为 ...
- Docker和ASP.NET Core
Docker和ASP.NET Core Docker 正在逐渐成为容器行业的事实标准,受到 Windows 和 Linux 生态系统领域最重要供应商的支持. (Microsoft 是支持 Docker ...
- ubuntu 安装谷歌浏览器
如何在ubuntu 中安装谷歌浏览器, 对于一个刚刚接触ubuntu的人来说,希望安装软件更加的简单,明了.最好能够像在windows下面安装软件一样. 先来介绍一下如何快速的安装谷歌浏览器,以下的方 ...
- jQuery-文件上传问题解决
后端要求文件上传需传参数为二进制流,用form-data方式传递,如下图所示: 为了满足该输入参数要求,上传代码如下: <input type="file" id=" ...
- 【xAsset框架】HFS 轻量级HTTP Server快速入门指南
一.引子 最近马三有幸参与开发了一个简易轻量的Unity资源管理框架 xAsset ,xasset 提供了一种使用资源路径的简单的方式来加载资源,简化了Unity项目资源打包,更新,加载,和回收的作业 ...
- php中文乱码原因和维修方法
一.首先是PHP网页的编码 1.如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312”),静态页面添加,所有文 ...