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 ;
}

最新文章

  1. CSS 3学习——animation动画
  2. Linux下使用Eclipse开发Hadoop应用程序
  3. 学生管理系统&lt;分层开发&gt;
  4. JQuery源码之“名叫extend的继承”
  5. MySQL 定时器EVENT学习
  6. 配置普通用户可以运行saltstack的模块
  7. poj 1088 滑雪(区间dp+记忆化搜索)
  8. Java基础知识拾遗(一)
  9. Swift基础之Delegate方法的使用
  10. windows10 conda python多版本切换
  11. SVN搭建外网远程访问
  12. [EFCore]EntityFrameworkCore Code First 当中批量自定义列名
  13. 组建自己的局域网(可以将PC机实现为服务器)
  14. Xcode真机调试失败:The identity used to sign the executable is no longer valid
  15. ubuntu中python3安装package
  16. 怎么检测自己fastq的Phred类型 | phred33 phred64
  17. Single Number III leetcode java
  18. PG cannot execute UPDATE in a read-only transaction | How to add column if not exists on PostgreSQL
  19. [SQL Server 2014] SQL Server 2014新特性探秘
  20. C#内存释放(垃圾回收)

热门文章

  1. 006---hashlib模块
  2. [BZOJ1455]罗马游戏(左偏树)
  3. java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
  4. MySQL源码中的String
  5. 【APUE】Chapter13 Daemon Processes
  6. Google序列化库FlatBuffers 1.1发布,及与protobuf的比较
  7. C#文件重命名的代码
  8. vue2.0 $emit $on组件通信
  9. 12.0 Excel表格读取
  10. spring环境搭建(简单实例)