链接:https://ac.nowcoder.com/acm/contest/904/A

来源:牛客网

DongDong破密码

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 131072K,其他语言262144K

64bit IO Format: %lld

题目描述

DongDong是一个喜欢密码学的女孩子,她养的萨摩耶叼着一张带着加密信息的纸条交给了她,如果她不能破解这张密码,萨摩耶是不会高兴的。

给定n,m,给出长度为n的01串,每次向后移动一位,移动m-1次,最后求出这n+m-1位每一位的异或值(00=0,11=0,0^1=1)成为密码。(如下图这样,此时n=6,m=3)

输入描述:

第一行两个整数,n和m

第二行一个01串(共n+m-1位)

2<=n+m<=1000000

输出描述:

第一行输出答案:长度为n的01串(保证存在答案)

示例1

输入

复制

6 3

11010110

输出

复制

101010

说明

见题目描述

题意:



思路:

这种题肯定是要用到异或的性质的。

可以看我这一篇博客:https://www.cnblogs.com/qieqiemin/p/11290854.html

接下来我们来看这题

由图我们可以看到,原始字符串ans的第一个字符一定和结果字符串str的第一个字符相等,那么我们可以直接得来值。

再看图中我蓝色圈的两个,每一个ans字符与m-1个他之前的字符异或得到结果字符,图中右边的蓝色框向下度,好左边的蓝色框向左读是一样的。那么我们就可以维护一下 一个数值now 表示当前字符的前面 m-1个字符(不足m-1个的话,用0补充,即异或0也是数值不变。)的异或结果。

为什么可以维护? 因为我们是要从ans的第一个字符开始找,从左到右的过程是很容易维护前面状态的。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,m;
string str;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>n>>m;
cin>>str;
string ans="";
int now;
if(str[0]=='0')
{
ans.pb('0');
now=0;
}else
{
ans.pb('1');
now=1;
}
for(int i=1;i<min(n,m);++i)
{
if(str[i]=='1')
{
if(now==0)
{
ans.pb('1');
}else
{
ans.pb('0');
// now=0;
}
now=1;
}else
{
if(now==0)
{
ans.pb('0');
}else
{
ans.pb('1');
// now=1;
}
now=0;
}
}
now=0;
for(int i=1;i<min(n,m);i++)
{
now^=(ans[i]-'0');
}
for(int i=m;i<n;++i)
{
if(str[i]=='1')
{
if(now==1)
{
ans.pb('0');
}else
{
ans.pb('1');
}
}else
{
if(now==1)
{
ans.pb('1');
}else
{
ans.pb('0');
}
}
now^=(ans[i-m+1]-'0');
now^=(ans[i]-'0');
}
cout<<ans<<endl;
return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

牛客练习赛47 A DongDong破密码 (异或性质,递推)的更多相关文章

  1. 牛客练习赛47 E DongDong数颜色 (树状数组维护区间元素种类数)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  2. 牛客练习赛47 E DongDong数颜色 (树上启发式合并)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  3. 牛客练习赛47 D DongDong坐飞机 (分层最短路)

    链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  4. 牛客练习赛B题 筱玛的排列(找递推规律)

    链接:https://ac.nowcoder.com/acm/contest/342/B来源:牛客网 筱玛的排列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语 ...

  5. 牛客练习赛47 DongDong数颜色 (莫队算法)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  6. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  7. 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B

    牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...

  8. 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A

    牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...

  9. 最小生成树--牛客练习赛43-C

    牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 ​ 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...

随机推荐

  1. LinkedList,ArrayList,Vector,HashMap,HashSet,HashTable之间的区别与联系

    在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...

  2. 自动轮询的recycleView

    import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.A ...

  3. pandas分组运算(groupby)

    1. groupby() import pandas as pd df = pd.DataFrame([[1, 1, 2], [1, 2, 3], [2, 3, 4]], columns=[" ...

  4. 微信小程序UI学习

    1.大纲: 2.flex的布局: 3.相对定位和绝对定位: position: relative   相对定位 position: absolute  绝对定位

  5. Service Mesh体验

    前言# 计算机软件技术发展到现在,软件架构的演进无不朝着让开发者能够更加轻松快捷地构建大型复杂应用的方向发展.容器技术最初是为了解决运行环境的不一致问题而产生的,随着不断地发展,围绕容器技术衍生出来越 ...

  6. Egret入门学习日记 --- 第五篇(书中 3.5节 内容)

    第五篇(书中 3.5节 内容) 今天得把昨天的问题解决了才行. 去了Q群,碰到一位大大,他给我解惑了.Thanks♪(・ω・)ノ 这是我之前按照书上写的方式写的,并没有效果. 然后大大给我解答了: 后 ...

  7. DMA(Direct Memory Access直接存储器访问)总结

    转载于http://blog.csdn.net/peasant_lee/article/details/5594753 DMA一种高速的数据传输操作,允许在外部设备和存储器之间直接读写数据,不需要CP ...

  8. python基础之字符串索引与切片

    字符串索引与切片:切片后组成新字符串与原字符串无关系增:str1+str2查:str1[index] str1[start_index:end_index]1,索引从0开始2,根据索引获取元素:索引超 ...

  9. python 基础复习

    1.简述cpu.内存.硬盘的作用 cpu (1)cpu:处理逻辑运算.算术运算 (2)cpu:接受指令传给电脑硬件,让其运行 内存: (1)内存:从硬盘中读取数据,供其cpu调取指令运行,短暂的存贮数 ...

  10. 交换机安全学习笔记 第二章 MAC地址泛洪攻击

    本文为书中相关知识的摘要,由于书中以思科设备为配置依据,所以笔记中补充了华为.H3C设备的相关配置.华为设备配置参考华为S2352EI 产品版本:V100R005C01文档版本:02.  H3C配置参 ...