CodeForces

( ext{Description})

传送门

( ext{Solution})

观察法就是坠 ( ext{diao}) 的!

首先这题目看上去就像是构造题,(k) 的范围是 (6100),那么合理推测次数在 (3 imes n) 级别。

以前做过一道很像的题也是 ( ext{CF}) 的,改天记起来放上题号吧。

增量法,我们每次将一个字符放在正确的顺序上(或者说构造一个 (t) 的前缀,依次往里面添加字符),尝试在 (3) 次操作内完成这个操作。

(s') 为将 (s) 翻转,若有状态 ( ext{AxBC})(其中 ( ext{A,B}) 无序,( ext{C}) 是已经排好的 (t) 的前缀,( ext x) 是这次要放在 ( ext C) 之后的字符)。

我们可以:

[ ext{AxBC} ightarrow ext{C'B'Ax} ightarrow ext{xC'B'A} ightarrow ext{A'BCx} ]

就是先转 ( ext{BC}),再转 ( ext x),最后转整个串。

( ext{Code})

#include <cstdio>

#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

#include <iostream>
using namespace std;

const int maxn=2005;

int n,t1[30],t2[30],sta[maxn*3],tp;
char s[maxn],t[maxn];

int main() {
	n=read(9);
	scanf("%s %s",s+1,t+1);
	rep(i,1,n) ++t1[s[i]-'a'],++t2[t[i]-'a'];
	rep(i,0,25) if(t1[i]^t2[i]) return puts("-1"),0;
	rep(i,1,n) {
		int j=n-i+1;
		while(s[j]^t[i]) --j;
		if(j==n) continue;
		sta[++tp]=n-j,sta[++tp]=1,sta[++tp]=n;
		rep(k,1,j-1>>1) swap(s[k],s[j-k]);
		int tmp=s[j];
		rep(k,j,n-1) s[k]=s[k+1];
		s[n]=tmp;
	}
	print(tp,'
');
	rep(i,1,tp) print(sta[i],' '); puts("");
	return 0;
}
原文地址:https://www.cnblogs.com/AWhiteWall/p/14175963.html