Serval and Shift-Shift-Shift
有趣的构造题。
考虑到两个字符串最多有
一个数异或
接着考虑左移和右移如何构造出
如果
如果
如果
当两个字符串一个全
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
int t, n, x, y;
string a, b;
vector<int> ans;
void op(string& s, int k)
{
if (!k) return;
ans.emplace_back(k);
string g;
for (int i = 0; i <= n; i++) g += "0";
for (int i = 1; i <= n; i++)
{
if (i - k >= 1 and i - k <= n) g[i - k] = s[i];
}
for (int i = 1; i <= n; i++)
{
if (g[i] - '0')
{
s[i] = ((s[i] - '0') xor 1) + '0';
}
}
}
void pre()
{
op(a, x - y);
x = y;
}
void solve()
{
for (int i = x + 1; i <= n; i++)
{
if (a[i] != b[i])
{
op(a, -i + x);
}
}
}
void solve2()
{
int lpos = n;
for (int i = n; i >= 1; i--)
{
if (a[i] - '0')
{
lpos = i;
break;
}
}
for (int i = x; i >= 1; i--)
{
if (a[i] != b[i])
{
op(a, lpos - i);
}
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while (t--)
{
cin >> n;
cin >> a >> b;
a = " " + a, b = " " + b;
ans.clear();
x = 0, y = 0;
for (int i = 1; i <= n; i++)
{
if (!x and a[i] == '1') x = i;
if (!y and b[i] == '1') y = i;
}
if (x and !y) cout << "-1\n";
else if (!x and y) cout << "-1\n";
else if (a == b) cout << "0\n";
else
{
if (x > y) pre();
solve();
if (x < y) solve2();
cout << ans.size() << "\n";
for (auto &i : ans) cout << i << " ";
cout << "\n";
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现