B. Obtaining the String
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings ss and tt. Both strings have length nn and consist of lowercase Latin letters. The characters in the strings are numbered from 11 to nn.

You can successively perform the following move any number of times (possibly, zero):

  • swap any two adjacent (neighboring) characters of ss (i.e. for any i={1,2,…,n−1}i={1,2,…,n−1} you can swap sisi and si+1)si+1).

You can't apply a move to the string tt. The moves are applied to the string ss one after another.

Your task is to obtain the string tt from the string ss. Find any way to do it with at most 104104 such moves.

You do not have to minimize the number of moves, just find any sequence of moves of length 104104 or less to transform ss into tt.

Input

The first line of the input contains one integer nn (1≤n≤501≤n≤50) — the length of strings ss and tt.

The second line of the input contains the string ss consisting of nn lowercase Latin letters.

The third line of the input contains the string tt consisting of nn lowercase Latin letters.

Output

If it is impossible to obtain the string tt using moves, print "-1".

Otherwise in the first line print one integer kk — the number of moves to transform ss to tt. Note that kk must be an integer number between 00and 104104 inclusive.

In the second line print kk integers cjcj (1≤cj<n1≤cj<n), where cjcj means that on the jj-th move you swap characters scjscj and scj+1scj+1.

If you do not need to apply any moves, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.

Examples
input

Copy
6
abcdef
abdfec
output

Copy
4
3 5 4 5
input

Copy
4
abcd
accd
output

Copy
-1
Note

In the first example the string ss changes as follows: "abcdef" →→ "abdcef" →→ "abdcfe" →→ "abdfce" →→ "abdfec".

In the second example there is no way to transform the string ss into the string tt through any allowed moves.

题意:只能交换s1的相邻元素,为最少需要次可以将s1换成s2

题解:暴力就行,如果不相等,从当前位置一直找,找到相等的元素然后一个个换过来

代码如下

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+;
int a[];
int b[];
int ans[ maxn];
int main(){ int n;
char str1[];
char str2[];
cin>>n>>str1>>str2;
for(int i=;i<n;i++){
a[str1[i]-'a'+]++;
b[str2[i]-'a'+]++;
}
int flag=;
int m=;
for(int i=;i<=;i++){
if(a[i]!=b[i]){
flag=;
}
}
if(flag==){
puts("-1");
}else{
for(int i=;i<n;i++){
if(str1[i]==str2[i]){
continue;
}else{
for(int j=i; j<n; j++)
if(str1[j] == str2[i]){
for(int k=j-; k>=i; k--){
ans[m++]=k+;
swap(str1[k], str1[k+]);
}
break;
}
}
}
printf("%d\n",m);
for(int i=;i<m;i++){
printf("%d ",ans[i]);
}
puts("");
}
return ;
}

codeforces 1015B的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. QOS-基本拥塞管理机制(PQ CQ WFQ RTPQ)

    QOS-基本拥塞管理机制(PQ CQ WFQ RTPQ) 2018年7月7日    20:29 拥塞:是指当前供给资源相对于正常转发处理需要资源的不足,从而导致服务质量下降的一种现象 拥塞管理概述: ...

  2. [Cracking the Coding Interview] 4.3 List of Depths

    Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth. ...

  3. 怎么实现hibernate悲观锁和乐观锁?

    隔离级别的安全控制是整体一个大的方面,而锁机制更加的灵活,它执行的粒度可以很小,可以在一个事务中存在. Hibernate悲观锁是依靠底层数据库的锁机制实现,在查询query.setLockMode( ...

  4. linux c fprintf()

    #include<stdio.h> #include<unistd.h> #include<time.h> int main(int argc,char *argv ...

  5. grunt in webstorm

    1.install grunt sudo npm install -g grunt-cli npm install grunt --save-dev

  6. mock.js中新增测试接口无效,返回404

    项目是使用的npm+vue+mock模拟数据 我碰到的是在mock配置文件中新增接口,但是接口在测试时无效,返回404.但是在前端代码中把新接口换成配置文件中之前就有的,然后测试就正常了. 所以按问题 ...

  7. 第一篇 Charles的配置及相关使用

    // Charles Proxy License // 适用于Charles任意版本的注册码,谁还会想要使用破解版呢. // Charles 4.2目前是最新版,可用.   Registered Na ...

  8. 搞ACM的伤不起

    劳资六年前开始搞ACM啊!!!!!!!!!!  从此踏上了尼玛不归路啊!!!!!!!!!!!!  谁特么跟劳资讲算法是程序设计的核心啊!!!!!!  尼玛除了面试题就没见过用算法的地方啊!!!!!!  ...

  9. servlet入门(1)

    第一个servlet类 1.编写一个java类,继承HttpServlet类 2.重写doget和dopost方法 3.Servlet程序在tomcat服务器运行 第一步:找到server窗口,并新建 ...

  10. penLDAP学习笔记

    LDAP协议 目录是一组具有类似属性.以一定逻辑和层次组合的信息.常见的例子是通讯簿,由以字母顺序排列的名字.地址和电话号码组成.目录服务是一种在分布式环境中发现目标的方法.目录具有两个主要组成部分: ...