1 条题解
-
0
练68.4 雷同检测
解题思路
按整行读取两个答案串,对相同位置逐字符比较,输出匹配下标+1。只检查两串都存在的位置。
正确性说明
依题目给出的对应关系逐项处理,位置与原输入保持明确对应。每一步完成当前项的全部贡献,结束后得到所有位置或字符的目标结果。
易错点
空格也作为字符参与比较;位置从1起。
复杂度
时间O(L),空间O(L)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string a, b; getline(cin, a); getline(cin, b); bool first = true; for (size_t i = 0; i < min(a.size(), b.size()); i++) if (a[i] == b[i]) { if (!first) cout << " "; cout << i + 1; first = false; } cout << "\n"; return 0; }
- 1