[语言月赛 202402] 翻转和反转
题目背景
bj12z_jiasiyuan 喜欢翻转与反转。
题目描述
bj12z_jiasiyuan 有一个长度为 n n n 的 01 01 01 串 s s s,第 i i i 位表示为 s i s_i si。他要操作 q q q 次,每次操作的内容如下:
-
1 \textbf{1} 1,将 01 01 01 串 s s s 翻转,即将字符串中的字符顺序颠倒过来。例如 s s s 串原来是
10010
,翻转(颠倒)后变为01001
。 -
2 \textbf{2} 2,将 01 01 01 串 s s s 反转,即逐位取反。对于每个 i i i( 1 ≤ i ≤ n 1\leq i\leq n 1≤i≤n),如果 s i = 0 s_i = 0 si=0,则令 s i s_i si 变成 1 1 1;否则令 s i s_i si 变成 0 0 0。
请你告诉 bj12z_jiasiyuan 操作 q q q 次后的 01 01 01 串 s s s。
输入格式
输入共三行。
第一行两个整数
n
,
q
n,q
n,q。
第二行一个长度为
n
n
n 的
01
01
01 串
s
s
s。
第三行一个长度为
q
q
q 的字符串
w
w
w,其中
w
i
w_i
wi 表示第
i
i
i 次操作的内容。如果
w
i
=
1
w_i=1
wi=1,表示进行翻转操作;如果
w
i
=
2
w_i=2
wi=2,表示进行反转操作。
输出格式
输出操作 q q q 次后的 01 01 01 串 s s s。
样例 #1
样例输入 #1
7 10
1001100
1222121222
样例输出 #1
1100110
样例 #2
样例输入 #2
20 18
01100001111110010100
122121212112211221
样例输出 #2
11010110000001111001
样例 #3
样例输入 #3
18 18
101111010011101101
122222221112211211
样例输出 #3
101111010011101101
提示说明
数据规模与约定
- 对于 50 % 50\% 50% 的数据, 1 ≤ n ≤ 1 0 3 1 \leq n \leq 10^3 1≤n≤103, 1 ≤ q ≤ 1 0 3 1 \leq q \leq 10^3 1≤q≤103。
- 对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105, 1 ≤ q ≤ 1 0 5 1 \leq q \leq 10^5 1≤q≤105。
代码内容
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
string s,w;
ll n,q;
cin>>n>>q;
cin>>s>>w;
ll res,ans;
res=ans=0;
for(auto t:w)
{
if(t=='1') res++;
else ans++;
}
if(res%2)
reverse(s.begin(),s.end());
if(ans%2)
{
for(auto u:s)
{
u=(u-'0')^1+'0';
cout<<u;
}
}
if(ans%2==0)
cout<<s<<endl;
return 0;
}