博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P3531 [POI2012]LIT-Letters
阅读量:4685 次
发布时间:2019-06-09

本文共 2301 字,大约阅读时间需要 7 分钟。

题目描述

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:
3ABCBCA
输出样例#1:
2

说明

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

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

 

Solution:

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

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

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

代码:

 

#include
#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=1000005;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

 

 

 

 

转载于:https://www.cnblogs.com/five20/p/9057568.html

你可能感兴趣的文章
修改node节点名称
查看>>
Java 文件下载
查看>>
图论——读书笔记 (深度优先搜索)
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
不要过早追求通用
查看>>
带ifrmae的弹窗
查看>>
20172310 2017-2018《程序设计与数据结构》(下)第二周学习总结
查看>>
C#中webBrowser加载页面中的不同域的iFrame的源代码的取得
查看>>
iOS/Android 微信及浏览器中唤起本地APP
查看>>
[Usaco2005 nov]Grazing on the Run 边跑边吃草 BZOJ1742
查看>>
flex中dragdrop不响应的原因
查看>>
.Net学习笔记----2015-07-08(基础复习和练习01)
查看>>
1#Two Sum(qsort用法)
查看>>
Spark 各个组件关系
查看>>
Android Studio之could not reserve enough space for object heap
查看>>
pass
查看>>
给自己网站配置 https,http2 ,gzip压缩
查看>>
Linux发展历程
查看>>
centos7.3下curl支持https协议
查看>>