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.
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.
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.
6
abcdef
abdfec
4
3 5 4 5
4
abcd
accd
-1
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
题解:暴力就行,如果不相等,从当前位置一直找,找到相等的元素然后一个个换过来
代码如下
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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+5; int a[27]; int b[27]; int ans[ maxn]; int main(){ int n; char str1[100]; char str2[100]; cin>>n>>str1>>str2; for(int i=0;i<n;i++){ a[str1[i]-'a'+1]++; b[str2[i]-'a'+1]++; } int flag=1; int m=0; for(int i=1;i<=26;i++){ if(a[i]!=b[i]){ flag=0; } } if(flag==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[m++]=k+1; swap(str1[k], str1[k+1]); } break; } } } printf("%d\n",m); for(int i=0;i<m;i++){ printf("%d ",ans[i]); } puts(""); } return 0; }