解法
很简单,设置两个指针和,表示扫描到的第为和的第位。
考虑这两个字符。
- :此时可以直接匹配令
- :此时,所以无法添加,只能删除,令,答案
- :此时同上,只能添加,令,答案
- 其他情况:此时无论如何都无法达成目标,直接判定无解。 此外,若或,可以视为或空格,以免特判。
代码
//#define NDEBUG
#include<bits/extc++.h>
#define _fo(i, l, o, s, ...) for (auto i = l; o; i += (s))
#define fo(...) _fo(__VA_ARGS__, 1)
#define _up(i, l, r, s, ...) _fo(i, l, i <= r, s)
#define up(...) _up(__VA_ARGS__, 1)
#define _dn(i, l, r, s, ...) _fo(i, l, i >= r, -(s))
#define dn(...) _dn(__VA_ARGS__, 1)
#ifdef NDEBUG
#define print(...) (void)0
#else
#define print(...) printf(__VA_ARGS__)
#endif
using ll = long long;
using ull = unsigned long long;
using namespace std;
const char C = 'A';
int ans, n, m;
string S, T;
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> S >> T;
n = S.size(); m = T.size();
S += " ";T += " ";
for (int i = 0, j = 0; i<n || j<m;)
if (S[i] == T[j]) i++, j++;
else if (S[i] == C) i++, ans++;
else if (T[j] == C) j++, ans++;
else return cout << "-1", 0;
cout << ans;
return 0;
}