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 ...
随机推荐
- 当Java遇见了Html--Jsp详解篇
###一.什么是Jsp jsp是一种基于文本的程序,全名java server page,其特点是html和java程序共存.执行时jsp会被运行容器编译,编译后的jsp跟servlet一样,因此js ...
- 11-UITableView
UITableView 掌握 设置UITableView的dataSource.delegate UITableView多组数据和单组数据的展示 UITableViewCell的常见属性 UITabl ...
- lintcode 110最小路径和
最小路径和 描述 笔记 数据 评测 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径. 注意事项 你在同一时间只能向下或者向右移动一步 您在真实的面试中是否遇到过 ...
- 利用Selenium+java实现淘宝自动结算购物车商品(附源代码)
转载请声明原文地址! 本次的主题是利用selenium+java实现结算购买购物车中的商品. 话不多说,本次首先要注意的是谷歌浏览器的版本,浏览器使用的驱动版本,selenium的jar包版本. ...
- 基于mybatis设计简单OA系统问题2
1.<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <fm ...
- AngularJS常见面试题
本文引自:https://segmentfault.com/a/1190000005836443 问题来源:如何衡量一个人的 AngularJS 水平? ng-if 跟 ng-show/hide 的区 ...
- php正则 与 js正则
PHP中的正则表达式函数 在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现 ...
- 宁夏邀请赛F FLOYD
Moving On Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn.Each ci ...
- HyperLedger Fabric 1.4 区块链技术发展(1.3)
区块链技术发展经历区块链1.0(数字货币).区块链2.0(数字资产与智能合约)和区块链3.0(各种行业分布式应用落地)三个阶段.区块链在应用上分为公有链(PublicBlockChains).联盟链( ...
- Flask错误收集 【转】
感谢大佬 ---> 原文链接 一.pydev debugger: process XXXXX is connecting 这个错误网上找了很多资料都无法解决,尝试过多种方法后,对我来说,下面这个 ...