題目連接spa
第一眼看出來可能和求逆序對差很少。code
主要是交換相鄰字符這兒看出來是逆序對,就是有一點不同,它排序的規則就是按字符串B的順序。排序
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; const ll MAXN = 1e6 + 5; inline ll read() { ll k = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { k = (k << 1) + (k << 3) + (ch & 15); ch = getchar(); } if (f == -1) k = ~k + 1; return k; } inline void write(ll x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } ll tree[MAXN],n,ans,sign[30][MAXN],d[30],a[MAXN]; char s1[MAXN],s2[MAXN]; ll lowbit(ll x){ return x & (-x); } void update(ll x,ll v){ for (int i = x; i <= n ; i += lowbit(i)) tree[i] += v; } ll sum(ll x){ ll res = 0; for (int i = x; i > 0 ; i -= lowbit(i)) res += tree[i]; return res; } int main(){ scanf("%d",&n); scanf("%s",s1); scanf("%s",s2); for(int i = 0 ; i <= n - 1; i++) sign[s1[i] - 'A'][ ++ sign[s1[i]-'A'][0]] = i; for(int i = 0 ; i <= n - 1; i++) a[i + 1] = sign[s2[i] - 'A'][ ++ d[s2[i]-'A']] + 1; for(int i = 1 ; i <= n ; i++){ ans += sum(n) - sum(a[i] - 1); update(a[i],1); } write(ans); return 0; }