题意:
给定两个|L|≤105的长度相等的且只含有a,b字母的字符串S,T,每次操作只能交换两个相邻的字母,问最少需要多少次操作使得S变成T,无解输出−1
分析:
无解的情况显然,字母个数不等
由于只有两种字母,我们只考虑一种,因为一旦其中一个字母的全部到达正确的位置,那么字符串必然相等了
考虑记录所有a字母的位置,对应次序的a,肯定对应交换操作,即不存在交叉
假如交换操作存在交叉,那么我们多走了两次交叉的距离,证明不优,所以我们假设的贪心成立
代码:
//
// Created by TaoSama on 2015-12-14
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
char s[N], t[N];
int a[N], b[N], cnt1, cnt2;
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int T; scanf("%d", &T);
while(T--) {
scanf("%s%s", s, t);
cnt1 = cnt2 = 0;
for(int i = 0; s[i]; ++i) {
if(s[i] == 'a') a[++cnt1] = i;
if(t[i] == 'a') b[++cnt2] = i;
}
if(cnt1 != cnt2) {
puts("-1");
continue;
}
long long ans = 0;
for(int i = 1; i <= cnt1; ++i)
ans += abs(a[i] - b[i]);
printf("%lld\n", ans);
}
return 0;
}