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. stm32+lwip(三):TCP测试

    我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...

  2. struts2学习笔记一

    一.框架概述 1.框架的意义与作用: 所谓框架,就是把一些繁琐的重复性代码封装起来,使程序员在编码中把更多的经历放到业务需求的分析和理解上面. 特点:封装了很多细节,程序员在使用的时候会非常简单. 2 ...

  3. PHP.41-TP框架商城应用实例-后台16-商品属性2-AJAX添加、删除

     添加商品属性 思路:根据[后台15]类型表{id,type_name}与属性表{id,attr_name,attr_type,attr_option_values,type_id} 1.建表商品属性 ...

  4. c# string.format和tostring()

    字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0:D3}", ...

  5. php简易实现计划任务

    index.php <?php function ceshi(){ $wan = file_get_contents('./wangt_index.txt',true); $jifen = $w ...

  6. .NET基础知识之八——深拷贝,浅拷贝

    目录 1.概念 2.使用赋值符号"=" 3.浅复制 4.深复制 5.问题一:如果类里面嵌套有多个类,然后嵌套类里面又嵌套类,那么像上面实现深拷贝的方法还能用吗? 6.问题二:实现深 ...

  7. MySQL源码中的String

    适用于离开作用域就销毁的字符串.

  8. 做模态弹框的时候,防止背景滚动方法 移动端 html5

    $(window.document).bind("touchmove", function() { return false; });

  9. Laxcus大数据管理系统2.0(3)- 第一章 基础概述 1.2 产品特点

    1.2 产品特点 Laxcus大数据管理系统运行在计算机集群上,特别强调软件对分布资源可随机增减的适应性.这种运行过程中数据动态波动和需要瞬时感知的特点,完全不同与传统的集中处理模式.这个特性衍生出一 ...

  10. python第一天(安装运行python)

    1. 安装Python 3.7 目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的.由于3.x版越来越普及,我们的教程将以最新的Python 3.7版本为基础.请确保你 ...