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
Inputcopy | Outputcopy |
---|---|
6 abcdef abdfec | 4 3 5 4 5 |
Sample 2
Inputcopy | Outputcopy |
---|---|
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] << " ";
}
}
还是有挺多失误的吧,一开始数组范围取的太小,提高做题的速度吧,加油!