B. Obtaining the String(模拟)
比较水的模拟
思路:就是模拟题意
注意:把数组开大点,开始wa了几次就是这个原因
#include<iostream> #include<string> #include<cstdio> using namespace std; string a, b; int aa[100000], t, n; bool flag1; int main(){ cin>>n; cin>>a>>b; int p=0, q=0; while(q<n){ bool flag=0; while(q<n&&a[p]==b[q]){++p; ++q;} while(p<n&&a[p]!=b[q]){++p; flag=1;} if(flag&&p==n){flag1=1; break;} for(int i=p;i>q;--i){ swap(a[i], a[i-1]); aa[t++]=i; } p=q; } if(flag1)cout<<-1<<endl; else { cout<<t<<endl; for(int i=0;i<t;++i) cout<<aa[i]<<" "; cout<<endl; } }