P3531 [POI2012]LIT-Letters

题目描述

Little Johnny has a very long surname.

Yet he is not the only such person in his milieu.

As it turns out, one of his friends from kindergarten, Mary, has a surname of the same length, though different from Johnny's.

Moreover, their surnames contain precisely the same number of letters of each kind - the same number of letters A, same of letters B, and so on.

Johnny and Mary took to one another and now often play together.

One of their favourite games is to gather a large number of small pieces of paper, write successive letters of Johnny's surname on them, and then shift them so that they obtain Mary's surname in the end.

Since Johnny loves puzzles, he has begun to wonder how many swaps of adjacent letters are necessary to turn his surname into Mary's. For a child his age, answering such question is a formidable task.

Therefore, soon he has asked you, the most skilled programmer in the kindergarten, to write a program that will help him.

给出两个长度相同且由大写英文字母组成的字符串A、B,保证A和B中每种字母出现的次数相同。

现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B。

输入输出格式

输入格式:

In the first line of the standard input there is a single integer () denoting the length of Johnny's surname.

The second line contains Johnny's surname itself, i.e., contains its successive letters (without spaces).

The third line contains Mary's surname in the same format: a string of letters (with no spaces either).

Both strings consist only of capital (upper-case) letters of the English alphabet.

In tests worth 30% of points it additionally holds that .

输出格式:

Your program should print a single integer to the standard output: the minimum number of swaps of adjacent letters that transforms Johnny's surname into Mary's.

输入输出样例

输入样例#1:
3
ABC
BCA
输出样例#1:
2

说明

给出两个长度相同且由大写英文字母组成的字符串A、B,保证A和B中每种字母出现的次数相同。

现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B。

Solution:

  本题将串转为数组,以目标串为样本,将操作串排序离散(记录每个字符在目标串中的位置)。

  然后每次交换相邻两数,等价于冒泡排序,即求离散后的数组中逆序对的个数。

  那么树状数组求一波就$OK$了。

代码:

 1 // luogu-judger-enable-o2
 2 #include<bits/stdc++.h>
 3 #define il inline
 4 #define ll long long
 5 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 6 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
 7 using namespace std;
 8 const int N=1000005;
 9 char s[N],t[N];
10 int n,c[N];
11 ll node[N],ans;
12 struct node{
13     int v,id;
14     bool operator<(const node a)const{return v==a.v?id<a.id:v<a.v;}
15 }a[N],b[N];
16 il void update(int k){while(k<=n){node[k]++;k+=k&-k;}}
17 il ll query(int k){
18     ll sum=0;
19     while(k){
20         sum+=node[k];
21         k-=k&-k;
22     }
23     return sum;
24 }
25 int main(){
26     scanf("%d",&n);
27     scanf("%s%s",s+1,t+1);
28     For(i,1,n){
29         a[i].v=s[i]-'A'+1;a[i].id=i;
30         b[i].v=t[i]-'A'+1;b[i].id=i;
31     }
32     sort(a+1,a+n+1);sort(b+1,b+n+1);
33     For(i,1,n)c[a[i].id]=b[i].id; 
34     Bor(i,1,n){
35         update(c[i]);
36         ans+=query(c[i]-1);
37     }
38     cout<<ans;
39     return 0;
40 }

猜你喜欢

转载自www.cnblogs.com/five20/p/9057568.html