CodeForces 1015B Obtaining the String

Description

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 ssto tt. Note that kk must be an integer number between 00 and 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.

Input

6
abcdef
abdfec

Output

4
3 5 4 5

Input

4
abcd
accd

Output

-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 ttthrough any allowed moves.

解:

先判断两字符串含有相同的元素,之后遍历寻找不相等的,找到不等的就从后一个个往前换。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
const ll mod = 1000000007;
int a[27], b[27], ans[N];
char str1[100], str2[100];
int main()
{
    int n;
    cin >> n >> str1 >> str2;
    for(int i = 0; i < n; ++i)
    {
        a[str1[i]-'a'+1]++;
        b[str2[i]-'a'+1]++;
    }
    bool f = 1;
    int cnt = 0;
    for(int i = 1; i <= 26; ++i)
        if(a[i] != b[i])
            f = 0;
    if(f == 0)
        puts("-1");
    else
    {
        for(int i = 0; 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-1; k >= i; --k)
                        {
                            ans[cnt++] = k+1;
                            swap(str1[k], str1[k+1]);
                        }
                        break;
                    }
                }
            }
        }
        printf("%d\n", cnt);
        for(int i = 0; i < cnt; ++i)
            printf("%d ", ans[i]);
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值