【思路要点】
- 将一组 S 2 [ i ] , T 2 [ i ] S_2[i],T_2[i] S2[i],T2[i] 看做一条边 T 2 [ i ] → S 2 [ i ] T_2[i]\rightarrow S_2[i] T2[i]→S2[i] ,则使得 S 1 [ i ] S_1[i] S1[i] 成为 T 1 [ i ] T_1[i] T1[i] 的交换过程对应了一条 S 1 [ i ] S_1[i] S1[i] 到 T 1 [ i ] T_1[i] T1[i] 的增广路,我们的要求即为经过最少的边完成所有增广。
- 最小费用流即可。
- 时间复杂度 O ( N + M + M i n c o s t F l o w ( σ , σ 2 ) ) O(N+M+MincostFlow(\sigma,\sigma^2)) O(N+M+MincostFlow(σ,σ2)) ,其中 σ = 26 \sigma=26 σ=26 。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const int MAXQ = 1e5 + 5; const int MAXP = 30; const int INF = 1e9; typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } struct edge {int dest, flow, pos, cost; }; vector <edge> a[MAXP]; int n, m, s, t, tot, flow, cost; int dist[MAXP], path[MAXP], home[MAXP]; void FlowPath() { int p = t, ans = INF; while (p != s) { ans = min(ans, a[path[p]][home[p]].flow); p = path[p]; } flow += ans; cost += ans * dist[t]; p = t; while (p != s) { a[path[p]][home[p]].flow -= ans; a[p][a[path[p]][home[p]].pos].flow += ans; p = path[p]; } } bool spfa() { static int q[MAXQ]; static bool inq[MAXP]; static int l = 0, r = 0; for (int i = 0; i <= r; i++) dist[q[i]] = INF; q[l = r = 0] = s, dist[s] = 0, inq[s] = true; while (l <= r) { int tmp = q[l]; for (unsigned i = 0; i < a[tmp].size(); i++) if (a[tmp][i].flow != 0 && dist[tmp] + a[tmp][i].cost < dist[a[tmp][i].dest]) { dist[a[tmp][i].dest] = dist[tmp] + a[tmp][i].cost; path[a[tmp][i].dest] = tmp; home[a[tmp][i].dest] = i; if (!inq[a[tmp][i].dest]) { q[++r] = a[tmp][i].dest; inq[q[r]] = true; } } l++, inq[tmp] = false; } return dist[t] != INF; } void addedge(int x, int y, int z, int c) { a[x].push_back((edge){y, z, a[y].size(), c}); a[y].push_back((edge){x, 0, a[x].size() - 1, -c}); } char ps[MAXN], pt[MAXN], es[MAXN], et[MAXN]; int cns[MAXP], cnt[MAXP], edge[MAXP][MAXP]; int main() { freopen("string.in", "r", stdin); freopen("string.out", "w", stdout); scanf("\n%s", ps + 1); scanf("\n%s", pt + 1); scanf("\n%s", es + 1); scanf("\n%s", et + 1); n = strlen(ps + 1), m = strlen(es + 1); s = 0, t = 27; for (int i = s; i <= t; i++) dist[i] = INF; for (int i = 1; i <= n; i++) { cns[ps[i] - 'a' + 1]++; cnt[pt[i] - 'a' + 1]++; } for (int i = 1; i <= m; i++) edge[et[i] - 'a' + 1][es[i] - 'a' + 1]++; for (int i = 1; i <= 26; i++) { addedge(s, i, cns[i], 0); addedge(i, t, cnt[i], 0); } for (int i = 1; i <= 26; i++) for (int j = 1; j <= 26; j++) addedge(i, j, edge[i][j], 1); while (spfa()) FlowPath(); if (flow == n) writeln(cost); else writeln(-1); return 0; }