链接:
题意:
DongDong是一个喜欢密码学的女孩子,她养的萨摩耶叼着一张带着加密信息的纸条交给了她,如果她不能破解这张密码,萨摩耶是不会高兴的。
给定n,m,给出长度为n的01串,每次向后移动一位,移动m-1次,最后求出这n+m-1位每一位的异或值(0^0=0,1^1=0,0^1=1)成为密码。(如下图这样,此时n=6,m=3)
思路:
从前往后找,可以看出原串的m位置到n位置中的p位置是根据答案串来得到。
即p位置为0即答案串p-m+1到p位置中1的个数为偶数,为1则为奇数。
两边的位置串同理,只不过范围是从1-p位置跟p-n位置1的个数。
代码:
#includeusing namespace std; typedef long long LL;const int MAXN = 3e5 + 10;const int MOD = 1e9 + 7;int n, m, k, t; int main(){ cin >> n >> m; string s; cin >> s; int cnt = 0; string res; for (int i = 0;i < n;i++) { if (i >= m && res[i-m] == '1') cnt--; if (s[i] == '0') { if (cnt % 2 == 0) res += '0'; else { res += '1'; cnt++; } } else { if (cnt%2 == 1) { res += '0'; } else { res += '1'; cnt++; } } } cout << res << endl; return 0;}