1 条题解
-
0
小 Z 的金手指(Golden Finger) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <string> #include <vector> #include <numeric> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; string a, b; if (!(cin >> n >> a >> b)) return 0; vector<int> p(26); iota(p.begin(), p.end(), 0); auto find = [&](int x) { while (p[x] != x) x = p[x] = p[p[x]]; return x; }; int ans = 0; for (int i = 0; i < n; ++i) { int u = a[i] - 'a'; int v = b[i] - 'a'; int ru = find(u), rv = find(v); if (ru != rv) { p[ru] = rv; ++ans; } } cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 8813
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者