Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void update(vector<int> &fenwick, int index, int value) {
- while (index < fenwick.size()) {
- fenwick[index] += value;
- index += index & -index;
- }
- }
- int getSum(const vector<int> &fenwick, int index) {
- int sum = 0;
- while (index) {
- sum += fenwick[index];
- index -= index & -index;
- }
- return sum;
- }
- int main() {
- int n; cin >> n;
- string s, t; cin >> s >> t;
- vector<int> positions[26];
- for (int i = n-1; i >= 0; --i) positions[s[i]-'A'].push_back(i);
- vector<int> fenwick(1+n);
- long long moves = 0;
- for (int i = 0; i < n; ++i) {
- int cur = positions[t[i]-'A'].back();
- positions[t[i]-'A'].pop_back();
- moves = moves + (cur+getSum(fenwick, n-cur))-i;
- update(fenwick, n-cur, +1);
- }
- cout << moves << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement