寒假试水4F题

        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, \dots, n - 1\}i={1,2,…,n−1} you can swap s_isi​ and s_{i + 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 10^4104 such moves.

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

Input

The first line of the input contains one integer nn (1 \le n \le 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 00 and 10^4104 inclusive.

In the second line print kk integers c_jcj​ (1 \le c_j < n1≤cj​<n), where c_jcj​ means that on the jj-th move you swap characters s_{c_j}scj​​ and s_{c_j + 1}scj​+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.

Sample 1

InputcopyOutputcopy
6
abcdef
abdfec
4
3 5 4 5 

Sample 2

InputcopyOutputcopy
4
abcd
accd
-1

Note

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

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

分析:

        关于英语题,(一个学日语的渣渣)我真的不想多说什么,翻译也完全看不懂,我就知道这个题是一个字符串匹配的题目:

        1.清楚两个字符串之间是如何交换才能相等

        2.写清楚特殊情况的条件(“-1”,“0”的条件

代码如下:

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
char a[60];char b[60];
int v = 0; int ans1 = 0;
int cnt = 0; int k = 0;
int n;
int s[10010];
int vis[100] = { 0 };
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] == b[i]) {
			cnt++;
		}
		for (int j = 0; j < n; j++) {
			if (vis[j] == 0 && b[i] == a[j]) {
				k++;
				vis[j] = 1;
				break;
			}
		}
	}
	if (k != n) {
		cout << -1 << endl;
		return 0;
	}
	if (cnt == n) {
		cout << 0 << endl;
		return 0;
	}
	for (int i = 0; i < n; i++) {
		if (a[i] == b[i]) {
			continue;
	}
		for (int j = i; j < n; j++) {
			if (b[i] == a[j]) {
				int h = j;
				while (b[i] != a[i]) {
					ans1++;
					swap(a[h-1], a[h]);
					s[v++] = h;
					h--;	
				}
			}
		}
	}
	cout << ans1 << endl;
	for (int i = 0; i <v; i++) {
		cout << s[i] << " ";
	}
}

        还是有挺多失误的吧,一开始数组范围取的太小,提高做题的速度吧,加油!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟一淼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值