①做这道题的时候没有认真思考,以为跟双栈排序一样然后就蛋疼了,蛋疼了好久才发现原来不一样!由于可以从栈A到栈B,所以在栈A中的元素是不一定有序的,而栈B中的元素才是一定有序的;我一开始误以为了二者均是有序的。
②这道题显然应该是DFS-ID,我却一上来就写了个BFS,导致爆掉了。
③DFS-ID的上限3*N才是比较合适的,因为其实这个题26好像是搜不了的。。所以用26*3的话。。呵呵。
#include<iostream> using namespace std; #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> char s[6][10]={" A B\n"," A C\n"," A D\n"," B C\n"," B D\n"," C D\n"}; int tmp[4][30],change[200000],m,order[30],n; bool flag; struct S{ int s,t; }b[10]={(S){0,1},(S){0,2},(S){0,3},(S){1,2},(S){1,3},(S){2,3}}; inline void prt(int x){ int a=tmp[b[change[x]].s][++tmp[b[change[x]].s][0]]=tmp[b[change[x]].t][tmp[b[change[x]].t][0]--]; if(x)prt(x-1); printf("%c%s",(char)(a+'a'),s[change[x]]); } inline void dfs(int x){ if(n-tmp[3][0]>m-x)return; if(tmp[3][0]==n){ prt(x-1); exit(0); } if(x==m)return; for(int i=0;i<6;++i){ if((n&1)&&i==0&&tmp[1][0]&&(!tmp[2][0]||(tmp[1][0]==2&&tmp[2][0]==2)))continue; if(tmp[b[i].s][0]){ if(b[i].t==3&&(tmp[3][0]?order[tmp[3][tmp[3][0]]]+1!=order[tmp[b[i].s][tmp[b[i].s][0]]]:order[tmp[b[i].s][tmp[b[i].s][0]]]!=1))continue; tmp[b[i].t][++tmp[b[i].t][0]]=tmp[b[i].s][tmp[b[i].s][0]--]; change[x]=i; dfs(x+1); tmp[b[i].s][++tmp[b[i].s][0]]=tmp[b[i].t][tmp[b[i].t][0]--]; } } } int main(){ freopen("track.in","r",stdin); freopen("track.out","w",stdout); int i; char s[30]; scanf("%d%s",&n,s); for(i=0;i<n;++i)order[s[i]-'a']=n-i; tmp[0][0]=n; for(i=0;i<n;++i)tmp[0][i+1]=i; while(m<3*n){ ++m; dfs(0); } printf("NO"); }