题目描述

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:

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

  1. 2

说明

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

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

Solution:

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

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

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

代码:

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

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. Mysql url参数浅析

    驱动包用的是mysql-connector-java-8.0.11.jar 新版的驱动类改成了com.mysql.cj.jdbc.Driver //北京时间东八区 serverTimezone=GMT ...

  2. Linux赋予普通用户root权限

    本文以新建用户admin来举例,请自行替换自己需要的用户 方法一: vi编辑 /etc/sudoers 文件,找到 root    ALL=(ALL)     ALL,在下面添加一行,如下所示: ## ...

  3. spring-AspectJ

    动态代理 ProxyFactoryBean织入切面数量太多不利于围护 BeanNameAutoProxyCreater-------------根据Bean名称创建代理 DefaultAdvisorA ...

  4. Q&A - 如何获取ip地址所在地

    获取其IP地址后,传入以下URL,并请求该URL,该请求会响应一个JSON格式的数据包,该IP地址的所在地均在这个数据包内   http://int.dpool.sina.com.cn/iplooku ...

  5. centos7 多网卡修改默认路由

    最近在virtualbox里搭了一个centos7的虚拟机,但是网络这一块总是有问题. 单网卡下的问题: 1.当我配置连接方式为NAT网络地址转换的时候,虚拟机可以访问外网.但是在网络地址转换的情况下 ...

  6. node服务端渲染(完整demo)

    简介 nodejs搭建多页面服务端渲染 技术点 koa 搭建服务 koa-router 创建页面路由 nunjucks 模板引擎组合html webpack打包多页面 node端异步请求 服务端日志打 ...

  7. MySQL数据库 : 自关联,视图,事物,索引

    自关联查询(自身id关联自身id(主键),查询的时候可以逻辑分为两个表,然后分别起一个别名来区分) select * from areas as cityinner join areas as pro ...

  8. keil 使用C++编程主要要点

    1.中断处理,添加一下宏定义.如果不添加,中断服务函数不会链接到下载文件中:发生中断后,会停留在xxx.s文件的 "B ."语句. #ifdef __cplusplus exter ...

  9. RHCE考试

    RHCSA_PDF版传送门:https://files.cnblogs.com/files/zhangjianghua/RHCSA%E8%AF%95%E9%A2%98.pdf RHCE_PDF版传送门 ...

  10. 15.9,python操作redis集群

      上代码 .对redis的单实例进行连接操作 python3 >>>import redis >>>r = redis.StrictRedis(host=, db ...