题目链接:https://vjudge.net/contest/336724#problem/C
题意:只能进行一种操作,选定一个x,最后x个字符翻转,然后把翻转后的字符放到最前面,求是否能在6100次内把s串通过上述操作变成t串
#include<bits/stdc++.h> using namespace std; #define ll long long int n; char s[6110][2100],t[2100]; int A[456],ans[6410],tot,cnt; int judge() { for(int i=0;i<n;i++) A[s[0][i]]++,A[t[i]]--; for(int i='a';i<='z';i++) if(A[i]!=0) return 0; return 1; } void rev(int x) { int r=0; for(int i=n-1;i>=n-x;i--) { s[tot+1][r++]=s[tot][i]; } for(int i=0;i<n-x;i++) { s[tot+1][r++]=s[tot][i]; } ans[++tot]=x; } void head() { for(int i=0;i<n;i++) { if(s[tot][i]==t[cnt]) { rev(n-i-1); rev(1); ++cnt; return ; } } } void zheng() { rev(n-cnt); for(int i=0;i<n;i++) { if(s[tot][i]==t[cnt]) { // printf("-->%d %c %c\n",i,t[cnt],s[tot][i]); rev(n-i-1); rev(1); ++cnt; return ; } } } void fan() { for(int i=cnt;i<n;i++) { if(s[tot][i]==t[cnt]) { rev(n-i); rev(i-cnt); rev(++cnt); return ; } } } int main() { scanf("%d%s%s",&n,s[0],t); if(judge()==0) { printf("-1\n"); return 0; } head(); int f=0; while(cnt<n) { if(f%2==0) zheng(); else fan(); f++; } if(f%2) rev(n); // for(int i=0;i<=tot;i++) // { // printf("%s\n",s[tot]); // } printf("%d\n",tot); for(int i=1;i<=tot;i++) printf("%d%s",ans[i],i==tot?"\n":" "); } /* 10 3526140897 1234567890 */