大意: 给定两个字符串$a,b$, 每个字符为$0-9$, 每次操作将$a$中相邻两位加$1$或减$1$, 操作后每个数仍要为$0-9$, 求最少操作使$a$变成$b$.
先不考虑范围, 判断是否成立. 然后暴力输出方案即可
#include <iostream> #include <cstdio> #define REP(i,a,n) for(int i=a;i<=n;++i) using namespace std; typedef long long ll; const int N = 1e6+10; int n; char s[N], t[N]; ll ans, a[N]; void dfs(int x, int w) { if (s[x+1]+w<'0'||s[x+1]+w>'9') dfs(x+1,-w); printf("%d %d ",x,w); s[x] += w; s[x+1] += w; if (!--ans) exit(0); } int main() { scanf("%d%s%s", &n, s+1, t+1); REP(i,1,n) { a[i] = -a[i-1]+t[i]-s[i]; ans += abs(a[i]); } if (a[n]) return puts("-1"),0; printf("%lld ", ans); if (!ans) return 0; ans = min(ans, (ll)1e5); REP(i,1,n-1) { while (s[i]!=t[i]) dfs(i,t[i]>s[i]?1:-1); } }