directly to the reverse order on the line ... Time complexity O (NLOGN)
-------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;#define IDX (c) ((c)-' A ')#define LOWBIT (x) ((x) &-(x))Const INT charset = +;const int MAXN = 1000009; stack<int> Let[charset];int N;A long long ans = 0;Char A[MAXN], B[MAXN];struct BIT {int B[MAXN];BIT () {memset (b, 0, sizeof B);}inline void Add (int p) {For (++p; p <= N; p + = Lowbit (p))b[p]++;}inline int sum (int p) {int ret = 0;For (++p; p; p-= Lowbit (p))ret + = b[p];return ret;}} bit;int main () {cin >> N;scanf ("%s", A); scanf ("%s", B);for (int i = 0; i < N; i++)Let[idx (A[i])].push (i);for (int i = N-1; ~i; i--) {int t = LET[IDX (B[i])].top (); Let[idx (B[i])].pop ();ans + = bit.sum (t);Bit.add (t);}cout << ans << "\ n";return 0;}
-------------------------------------------------------------------------
2789: [Poi2012]letters time limit: Sec Memory Limit: MB
Submit: Solved: 144
[Submit] [Status] [Discuss] Description
Give two strings A and B with the same length and uppercase letters, guaranteeing that each letter in a and b appears the same number of times.
Now each time you can swap two characters in a, the minimum number of times you need to swap can make a into B.
Input
The first line is a positive integer n (2<=n<=1,000,000) that represents the length of the string.
The second and third lines each have a string of length n and contain only uppercase English letters.
Outputa non-negative integer that represents the minimum number of interchanges. Sample Input3
Abc
Bca
Sample Output2HINT
BCA, ABC, BAC
Source
Acknowledgement Oimaster
Bzoj 2789: [Poi2012]letters (BIT)