Files
python/GESP/Level5/Sample.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
654 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int main() {
string a = "pple";
string b = "apple";
int cnt = 0;
if (a.size() > b.size())
swap(a, b);
if (a.size() - b.size() > 1)
return -1;
// 保证a是短串,b是长串
int p = 0;
for (int i = 0; i < a.size();) {
// 遍历短串中每一个char
// 此时,长串中的位置是p
if (a[i] != b[p]) {
// 对不上的时候,p指针移动一位
p++;
cnt++;
if (cnt > 1)
return -1;
} else {
// 对上的时时候,p指针与i指针同步移动
p++;
i++;
}
}
return 0;
}