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.
输入输出样例
3
ABC
BCA
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的更多相关文章
- 洛谷P3531 [POI2012]LIT-Letters
题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...
- 【字符串】【hash】【倍增】洛谷 P3502 [POI2010]CHO-Hamsters 题解
这是一道字符串建模+图论的问题. 题目描述 Byteasar breeds hamsters. Each hamster has a unique name, consisting of lo ...
- Bzoj 2789: [Poi2012]Letters 树状数组,逆序对
2789: [Poi2012]Letters Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 278 Solved: 185[Submit][Stat ...
- BZOJ 2789: [Poi2012]Letters( BIT )
直接求逆序对就行了...时间复杂度O(nlogn) ------------------------------------------------------------------------- ...
- 【BZOJ2789】[Poi2012]Letters 树状数组
[BZOJ2789][Poi2012]Letters Description 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符 ...
- BZOJ2789 : [Poi2012]Letters
按照顺序依次找到b串每个字母最后的位置,然后求逆序对. #include<cstdio> #define N 1000010 int n,i,j,k,g[26],nxt[N],bit[N] ...
- BZOJ2789 [Poi2012]Letters 【树状数组】
题目链接 BZOJ 题解 如果我们给\(A\)中所有字母按顺序编号,给\(B\)中所有字母编上相同的号码 对于\(B\)中同一种,显然号码应该升序 然后求逆序对即可 #include<algor ...
- 【树状数组】bzoj2789 [Poi2012]Letters
处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #includ ...
- 【bzoj2789】[Poi2012]Letters 树状数组求逆序对
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...
随机推荐
- PXE自动化安装CentOS6/7
服务器为centos7 安装前准备:关闭防火墙和SELINUX 虚拟机准备第二块网卡,设置主机模式,关闭虚拟机网络配置中主机模式的DHCP功能,并设置静态IP nmcli c a con-name e ...
- 一张思维导图带你梳理HashMap相关知识
HashMap可以说是java中最常见也是最重要的key-value存储结构类,很多程序员可能经常用,但是不一定清楚这个类背后的数据结构和相关操作原理,为了复习HashMap相关的知识,今天花了一天的 ...
- 6-2 python 操作数据库mysql
1.python操作MySQL的 查询(select操作) 步骤 import pymysql # 1.建立数据库连接 conn = pymysql.connect(host='118.24.3.40 ...
- python json.dumps raise TypeError(repr(o) + " is not JSON serializable") TypeError: 0 is not JSON serializable
出错如题. 这个问题有可能是因为python的json.dumps没法识别dump内容里的某些数据类型导致的.我的问题是因为dict中含有numpy.int64,numpy.float等类型导致的,需 ...
- vue本人常用插件汇总(常更新)
1. 移动端UI插件 mint-ui http://mint-ui.github.io/#!/zh-cn 2.vue状态管理vuex,持久化插件:vuex-persist https://github ...
- 内置函数系列之 filter
filter 过滤 基本语法: s = filter(function,iterable) 将可迭代对象的每一个元素,传进函数中,根据函数中的判断条件,返回True或False 返回True的是保留的 ...
- input标签中的name
<input>标签是java web的jsp页面中最常用的标签,特别是用来在页面和servlet中传递内容, 但是我们看到<input>标签中有很多内容,这边我们只提一下主要的 ...
- JZOJ 3534. 【NOIP2013提高组day1】货车运输
Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的 ...
- SpringCloud框架搭建+实际例子+讲解+系列五
(4)服务消费者,面向前端或者用户的服务 本模块涉及到很多知识点:比如Swagger的应用,SpringCloud断路器的使用,服务API的检查.token的校验,feign消费者的使用.大致代码框架 ...
- LeetCode summary
https://www.programcreek.com/2013/08/leetcode-problem-classification/ https://medium.com/algorithms- ...