题目描述

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. 关于js的严格模式

    最近在看你不知道js,补充自己的js基础,加深理解.在读的过程中写点笔记. 严格模式下与非严格模式的区别 . 严格模式是es5新增的,es6是默认为严格模式的!js默认状态下是非严格模式的!   一般 ...

  2. Oracle多表连接方法

    笛卡尔连接[结果集为各表记录的乘积] SELECTt * FROM table_1, table_2, table_n SELECTt * FROM table_1 CROSS JOIN table_ ...

  3. Windows平台下源码分析工具

    最近这段时间在阅读 RTKLIB的源代码,目前是将 pntpos.c文件的部分看完了,准备写一份文档记录下这些代码的用处.处理过程.理论公式来源.注意事项,自己还没有弄明白的地方.目前的想法是把每一个 ...

  4. mysql基础 日期类型

  5. LeetCode962. 最大宽度坡

    问题:最大宽度坡 给定一个整数数组 A,坡是元组 (i, j),其中  i < j 且 A[i] <= A[j].这样的坡的宽度为 j - i. 找出 A 中的坡的最大宽度,如果不存在,返 ...

  6. 7-4 python 接口开发(提供mock服务)

    1.登录接口开发(数据存在数据库中)  接口开发做mock(模拟功能) tools.py import pymysql def my_db(sql): conn = pymysql.connect(h ...

  7. Sublime Text3的快捷键和插件

    今天重装了一下Sublime Text3,发现了一个不错的网站,关于Sublime Text3的插件安装介绍的很详细,还有右键增强菜单和浏览器打开快捷键的创建.奉上链接 http://www.cnbl ...

  8. java实时监听日志写入kafka

    目的 实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整) ...

  9. [bzoj1999][noip2007]Core树网的核

    好久没写题解了.这题不算太水就写一下题解. 话说回来,虽然不水但是挺裸.可以说题意即一半题解了. 我猜粘了题面也没有人去看的,所以直接人话题意了. 给一棵树,点数1e6,(当年noip的n当然是只有3 ...

  10. Java虚拟机之搜索class文件

    Java命令 Java虚拟机的工作是运行Java应用程序.和其他类型的应用程序一样,Java应用程序也需要一个入口点,这个入口点就是我们熟知的main()方法.如果一个类包含main()方法,这个类就 ...