题目描述

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$了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
char s[N],t[N];
int n,c[N];
ll node[N],ans;
struct node{
int v,id;
bool operator<(const node a)const{return v==a.v?id<a.id:v<a.v;}
}a[N],b[N];
il void update(int k){while(k<=n){node[k]++;k+=k&-k;}}
il ll query(int k){
ll sum=;
while(k){
sum+=node[k];
k-=k&-k;
}
return sum;
}
int main(){
scanf("%d",&n);
scanf("%s%s",s+,t+);
For(i,,n){
a[i].v=s[i]-'A'+;a[i].id=i;
b[i].v=t[i]-'A'+;b[i].id=i;
}
sort(a+,a+n+);sort(b+,b+n+);
For(i,,n)c[a[i].id]=b[i].id;
Bor(i,,n){
update(c[i]);
ans+=query(c[i]-);
}
cout<<ans;
return ;
}

P3531 [POI2012]LIT-Letters的更多相关文章

  1. 洛谷P3531 [POI2012]LIT-Letters

    题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...

  2. 【字符串】【hash】【倍增】洛谷 P3502 [POI2010]CHO-Hamsters 题解

        这是一道字符串建模+图论的问题. 题目描述 Byteasar breeds hamsters. Each hamster has a unique name, consisting of lo ...

  3. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  4. BZOJ 2789: [Poi2012]Letters( BIT )

    直接求逆序对就行了...时间复杂度O(nlogn) ------------------------------------------------------------------------- ...

  5. 【BZOJ2789】[Poi2012]Letters 树状数组

    [BZOJ2789][Poi2012]Letters Description 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符 ...

  6. BZOJ2789 : [Poi2012]Letters

    按照顺序依次找到b串每个字母最后的位置,然后求逆序对. #include<cstdio> #define N 1000010 int n,i,j,k,g[26],nxt[N],bit[N] ...

  7. BZOJ2789 [Poi2012]Letters 【树状数组】

    题目链接 BZOJ 题解 如果我们给\(A\)中所有字母按顺序编号,给\(B\)中所有字母编上相同的号码 对于\(B\)中同一种,显然号码应该升序 然后求逆序对即可 #include<algor ...

  8. 【树状数组】bzoj2789 [Poi2012]Letters

    处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #includ ...

  9. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

随机推荐

  1. 高封装的property方法

    class Person(): def __init__(self): self.__age = 0 def set_age(self, age): if age < 0 or age > ...

  2. 返回固定页面的web服务器

    import socket def handle_client(socket_con): """ 接收来自客户端的请求,并接收请求报文,解析,返回 "" ...

  3. js延迟加载的方式有哪些?

    共有:defer和async.动态创建DOM方式(用得最多).按需异步载入js defer属性:(页面load后执行) HTML 4.01 为 <script>标签定义了 defer属性. ...

  4. Python代码结构——顺序、分支、循环

    ## 顺序结构 - 按照从上到下的顺序,一条语句一条语句的执行,是最基本的结构 ## 分支结构 if condition: statement statement ... elif condition ...

  5. 边缘检测 opencv

    本次实验使用了两种方法进行了边缘检测,分别使用到了opencv中的两个API函数为Canny()和Sobel()函数.实验后加了Scharr滤波器,它其实是基于Sobel()函数的. 这三个API中的 ...

  6. C# Dictionary的遍历理解

    C# Dictionary容器类的理解 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/det ...

  7. SpringMVC 集成 Velocity 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...

  8. python2.7写入文件时指定编码为utf-8

    python3.0可以这样写 f = open('ufile.log', 'w', 'utf-8')   但在python2.7中open()没有编码参数,如上那样写会报错,可以使用如下模块 impo ...

  9. wxPython 安装 及参考文档

    三种操作平台上的安装方法 1.windows 和 mac pip install -U wxPython 2.linux pip install -U -f https://extras.wxpyth ...

  10. 求:斐波那契数列的第n项

    def he (n): if n < 3 : return 1 return he(n-1)+he(n-2)print(he(n))