-
Notifications
You must be signed in to change notification settings - Fork 22
/
1015B- Obtaining the String.cpp
68 lines (52 loc) · 1.17 KB
/
1015B- Obtaining the String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
int flag=0;
int moves[10005],cnt=0;
string reshape(string text, int i, int pos){
for(i=i;i>pos;i--){
moves[cnt++]=i;
char tmp=text[i];
text[i]=text[i-1];
text[i-1]=tmp;
}
return text;
}
int solve(string text, string pattern){
int offset=0;
for(int pos=0;pos<pattern.length();pos++){
for(int i=pos;i<text.length();i++){
if(pattern[pos]==text[i]){
offset+=i-pos;
text= reshape(text,i,pos);
break;
}
}
}
return offset;
}
main(){
int len;
string text, pattern;
cin>>len>>text>>pattern;
string t_text=text, t_pattern=pattern;
sort(t_text.begin(),t_text.end());
sort(t_pattern.begin(),t_pattern.end());
if(t_text!=t_pattern){
cout<<"-1"<<endl;
return 0;
}
int offset= solve(text,pattern);
if(offset==0){
cout<<"0"<<endl;
return 0;
}
if(cnt>10000){
cout<<"-1"<<endl;
return 0;
}
cout<<offset<<endl;
cout<<moves[0];
for(int i=1;i<cnt;i++)
cout<<" "<<moves[i];
puts("");
}