*** sortejun.c	2006-06-08 17:57:23+09	1.3
--- sortejun.c	2007-02-08 18:05:15+09	1.7
***************
*** 1,5 ****
  /* 
!  * $Id: sortejun.c,v 1.3 2006-06-08 17:57:23+09 shige Exp $
   * »³¤ËÇÛ¤ë·Á¼°¤Î¥½¡¼¥È¤Î¼ê½ç¤ÎºîÀ®¥Ä¡¼¥ë
   * shige
   * sortejun.c
--- 1,5 ----
  /* 
!  * $Id: sortejun.c,v 1.7 2007-02-08 18:05:15+09 shige Exp $
   * »³¤ËÇÛ¤ë·Á¼°¤Î¥½¡¼¥È¤Î¼ê½ç¤ÎºîÀ®¥Ä¡¼¥ë
   * shige
   * sortejun.c
***************
*** 94,100 ****
  void putarray2(int N,int inmode,Indata b[],char *title);
  int getK(int blocks,int M);
  int getOptM(int K,int N,int M,int a[],int p[],int L,int mode,
! 	       int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd,int *CdM);
  int getRunmode(int N,int M,int a[],int p[],int L,int mode,
  	       int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd);
  void putheader(int outmode[],int N,int M,int inmode,int L,int mode,
--- 94,100 ----
  void putarray2(int N,int inmode,Indata b[],char *title);
  int getK(int blocks,int M);
  int getOptM(int K,int N,int M,int a[],int p[],int L,int mode,
! 	    int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd,int *CdM);
  int getRunmode(int N,int M,int a[],int p[],int L,int mode,
  	       int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd);
  void putheader(int outmode[],int N,int M,int inmode,int L,int mode,
***************
*** 103,108 ****
--- 103,109 ----
  	     int bn[],int cdmode,int num);
  void putbody(int outmode[],int N,int M,int path[],int inmode,Indata b[],
  	     int te[],int cdmode,int num);
+ void putshlesstop(int M,int K,int cdmode,int cdlast);
  void putshlesshead(int num,int cdmode);
  void putshless(int groupnum,int N,int path[],int inmode,Indata b[],int te[],
  	       int cdmode,int num);
***************
*** 432,447 ****
  
  /* opt==Opt_M ¤Î¤È¤­¤Î M (Åù) ¤Î¼è¤êÄ¾¤· */
  int getOptM(int K,int N,int M,int a[],int p[],int L,int mode,
! 	       int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd,int *CdM)
  {
      int newK,m;
      int blocks,cdmode,cdlast,isadd;
      
      *CdM=M;
      if(M==2) return K;
      for(m=M-1;m>=2;m--){
! 	if((newK=getRunmode(N,m,a,p,L,mode,&blocks,&cdmode,&cdlast,&isadd))<0)
! 	  return newK;
  	if(newK>K) break;
      }
      if(m==M-1) return K;
--- 433,450 ----
  
  /* opt==Opt_M ¤Î¤È¤­¤Î M (Åù) ¤Î¼è¤êÄ¾¤· */
  int getOptM(int K,int N,int M,int a[],int p[],int L,int mode,
! 	    int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd,int *CdM)
  {
      int newK,m;
      int blocks,cdmode,cdlast,isadd;
+     int tmpa[MaxN];
      
      *CdM=M;
      if(M==2) return K;
+     cparray(N,a,tmpa);
      for(m=M-1;m>=2;m--){
! 	newK=getRunmode(N,m,tmpa,p,L,mode,&blocks,&cdmode,&cdlast,&isadd);
! 	if(newK<0) return newK;
  	if(newK>K) break;
      }
      if(m==M-1) return K;
***************
*** 449,462 ****
      return getRunmode(N,M,a,p,L,mode,Blocks,Cdmode,Cdlast,IsAdd);
  }
  
! /* ¼ÂºÝ¤ÎÆ°ºî¥â¡¼¥É¤ò·èÄê¤¹¤ë */
  int getRunmode(int N,int M,int a[],int p[],int L,int mode,
  	       int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd)
  {
      int K,k1,k2,k3,k4;
!     int rev=0;
      int flag;
  
      if((*Blocks=getblocks(N,a,p))<0) return -1;
      *Cdmode=mode; *Cdlast=L; *IsAdd=0;
      k1=getK(*Blocks,M);
--- 452,468 ----
      return getRunmode(N,M,a,p,L,mode,Blocks,Cdmode,Cdlast,IsAdd);
  }
  
! /* ¼ÂºÝ¤ÎÆ°ºî¥â¡¼¥É¤ò·èÄê¤¹¤ë 
!  * ¤â¤Ã¤È simple ¤Ë¤Ç¤­¤Ê¤¤¤« ?
!  */
  int getRunmode(int N,int M,int a[],int p[],int L,int mode,
  	       int *Blocks,int *Cdmode,int *Cdlast,int *IsAdd)
  {
      int K,k1,k2,k3,k4;
!     int rev;
      int flag;
  
+     rev=0;
      if((*Blocks=getblocks(N,a,p))<0) return -1;
      *Cdmode=mode; *Cdlast=L; *IsAdd=0;
      k1=getK(*Blocks,M);
***************
*** 651,658 ****
--- 657,667 ----
  	    else printf(")\n"); /* µ¯¤³¤é¤Ê¤¤¤Ï¤º */
  	}
      }
+     else if(outmode[0]==Omode_shless)
+       putshlesstop(M,K,cdmode,cdlast);
  }
  
+ /* num<0 ¤Î¤È¤­¤Ï mode = cdlast, ¤½¤ì°Ê³°¤Ï mode=cdmode */
  void puthistory(int outmode[],int N,int path[],int inmode,Indata b[],
  	     int bn[],int cdmode,int num)
  {
***************
*** 664,669 ****
--- 673,679 ----
      }
  }
  
+ /* num<0 ¤Î¤È¤­¤Ï mode = cdlast, ¤½¤ì°Ê³°¤Ï mode=cdmode */
  void putbody(int outmode[],int N,int M,int path[],int inmode,Indata b[],
  	     int te[],int cdmode,int num)
  {
***************
*** 680,708 ****
      }
  }
  
  void putshlesshead(int num,int cdmode)
  {
      printf("%s [%d ²óÌÜ³«»Ï]\n",SHLESSHEAD,num);
!     printf("%s %d ka i me ka i si",AUCONN,num); /* ¡Ö%d ²óÌÜ³«»Ï¡× */
!     if(num==1){
! 	printf(" null null ka mi wa null"); /* ¡Ö»æ¤Ï¡¢¡× */
! 	if(cdmode==Mode_ura) printf(" u ra ga e si na ga ra");
! 	  /* ¡ÖÎ¢ÊÖ¤·¤Ê¤¬¤é¡× */
! 	else printf(" o mo te no ma ma"); /* ¡ÖÉ½¤Î¤Þ¤Þ¡× */
! 	printf(" si te i si ta ba n go u no ya ma ni ku ba ttsu te ku da sa i");
! 	/* ¡Ö»ØÄê¤·¤¿ÈÖ¹æ¤Î»³¤ËÇÛ¤Ã¤Æ²¼¤µ¤¤¡£¡× */
!     }
!     printf(" %s\n",AUPLAY);
  }
  
  void putshless(int groupnum,int N,int path[],int inmode,Indata b[],int te[],
  	       int cdmode,int num)
  {
      int j,k,max=0;
      int tej,pathj;
      int runmode,lastj;
  
!     if(cdmode==Mode_ura) runmode=1; /* Àµ½ç */
      else if(num%2==0) runmode=2; /* µÕÅ¾ */
      else runmode=3; /* È¿Å¾ */
      if(runmode==3){
--- 690,745 ----
      }
  }
  
+ void putshlesstop(int M,int K,int cdmode,int cdlast)
+ {
+     printf("%s É¬Í×²ó¿ô = [%d]¡¢°ìÈÖ¾å = [%s]\n ",SHLESSHEAD,
+ 	   K,(cdlast==Last_incr)?"ºÇ¾®":"ºÇÂç");
+     printf("%s",AUCONN);
+     printf(" i chi ba n u e o null"); /* ¡Ö1 ÈÖ¾å¤ò¡× */
+     if(cdlast==Last_incr) printf(" sa i sho o"); /* ¡ÖºÇ¾®¡× */
+     else printf(" sa i da i"); /* ¡ÖºÇÂç¡× */
+     printf(" ni su ru no ni null"); /* ¡Ö¤Ë¤¹¤ë¤Î¤Ë¡× */
+     /* ### K ¤Ï 2 ·å¤Î¿ô¤Î¤ß support ### */
+     if(K>=20) printf(" %d",K/10);
+     if(K>=10) printf(" 10");
+     if(K%10) printf(" %d",K%10);
+     printf(" ka i ka ka ri ma su"); /* ¡Ö¤«¤«¤ê¤Þ¤¹¡× */
+     printf(" %s\n",AUPLAY);
+ 
+     printf("%s »³¤Î¿ô = [%d]¡¢ÇÛÉÛ = [%s]\n",SHLESSHEAD,
+ 	M,(cdmode==Mode_ura)?"Î¢ÊÖ¤·":"É½¤Î¤Þ¤Þ");
+     printf("%s",AUCONN);
+     printf(" ka mi wa null"); /* ¡Ö»æ¤Ï¡¢¡× */
+     if(cdmode==Mode_ura) 
+       printf(" u ra ga e si na ga ra"); /* ¡ÖÎ¢ÊÖ¤·¤Ê¤¬¤é¡× */
+     else printf(" o mo te no ma ma"); /* ¡ÖÉ½¤Î¤Þ¤Þ¡× */
+     printf(" si te i si ta ba n go u no ya ma ni ku ba ttsu te ku da sa i");
+     /* ¡Ö»ØÄê¤·¤¿ÈÖ¹æ¤Î»³¤ËÇÛ¤Ã¤Æ²¼¤µ¤¤¡£¡× */
+     printf(" %s\n",AUPLAY);
+ }
+ 
  void putshlesshead(int num,int cdmode)
  {
+     if(num<0) num=-num;
      printf("%s [%d ²óÌÜ³«»Ï]\n",SHLESSHEAD,num);
!     printf("%s %d ka i me ka i si %s\n",AUCONN,num,AUPLAY);
!     /* ¡Ö%d ²óÌÜ³«»Ï¡× */
  }
  
+ /* num<0 ¤Î¤È¤­¤Ï te==NULL */
  void putshless(int groupnum,int N,int path[],int inmode,Indata b[],int te[],
  	       int cdmode,int num)
  {
      int j,k,max=0;
      int tej,pathj;
      int runmode,lastj;
+     int rev;
  
!     if(num<0){ 
! 	runmode=0; /* ºÇ¸å¤ÎÂçÊÖ¤· */
! 	rev = (cdmode==Mode_omote && (-num)%2==0);
!     }
!     else if(cdmode==Mode_ura) runmode=1; /* Àµ½ç */
      else if(num%2==0) runmode=2; /* µÕÅ¾ */
      else runmode=3; /* È¿Å¾ */
      if(runmode==3){
***************
*** 712,718 ****
  	lastj=0;
  	printf("%s",SHLESSHEAD);
  	for(j=1;j<=N;j++){
! 	    if(runmode==1){ pathj=path[j-1]; tej=te[j-1]+1; }
  	    else if(runmode==2){ pathj=path[N-j]; tej=te[N-j]+1; }
  	    else{ pathj=path[j-1]; tej=max-te[j-1]+1; }
  	    if(inmode==Input_number)
--- 749,760 ----
  	lastj=0;
  	printf("%s",SHLESSHEAD);
  	for(j=1;j<=N;j++){
! 	    if(runmode==0){ 
! 		if(rev) pathj=path[N-j];
! 		else pathj=path[j-1];
! 		tej=1; 
! 	    }
! 	    else if(runmode==1){ pathj=path[j-1]; tej=te[j-1]+1; }
  	    else if(runmode==2){ pathj=path[N-j]; tej=te[N-j]+1; }
  	    else{ pathj=path[j-1]; tej=max-te[j-1]+1; }
  	    if(inmode==Input_number)
***************
*** 722,728 ****
  	    if(j%groupnum==0){
  		printf("\n%s",AUCONN);
  		for(k=lastj+1;k<=j;k++){
! 		    if(runmode==1) tej=te[k-1]+1;
  		    else if(runmode==2) tej=te[N-k]+1;
  		    else tej=max-te[k-1]+1;
  		    printf(" %d",tej);
--- 764,771 ----
  	    if(j%groupnum==0){
  		printf("\n%s",AUCONN);
  		for(k=lastj+1;k<=j;k++){
! 		    if(runmode==0) tej=1;
! 		    else if(runmode==1) tej=te[k-1]+1;
  		    else if(runmode==2) tej=te[N-k]+1;
  		    else tej=max-te[k-1]+1;
  		    printf(" %d",tej);
***************
*** 736,742 ****
  	if(lastj<N){
  	    printf("\n%s",AUCONN);
  	    for(k=lastj+1;k<=N;k++){
! 		if(runmode==1) tej=te[k-1]+1;
  		else if(runmode==2) tej=te[N-k]+1;
  		else tej=max-te[k-1]+1;
  		printf(" %d",tej);
--- 779,786 ----
  	if(lastj<N){
  	    printf("\n%s",AUCONN);
  	    for(k=lastj+1;k<=N;k++){
! 		if(runmode==0) tej=1;
! 		else if(runmode==1) tej=te[k-1]+1;
  		else if(runmode==2) tej=te[N-k]+1;
  		else tej=max-te[k-1]+1;
  		printf(" %d",tej);
***************
*** 747,753 ****
      else{
  	printf("%s",SHLESSHEAD);
  	for(j=1;j<=N;j++){
! 	    if(runmode==1){ pathj=path[j-1]; tej=te[j-1]+1; }
  	    else if(runmode==2){ pathj=path[N-j]; tej=te[N-j]+1; }
  	    else{ pathj=path[j-1]; tej=max-te[j-1]+1; }
  	    if(inmode==Input_number)
--- 791,802 ----
      else{
  	printf("%s",SHLESSHEAD);
  	for(j=1;j<=N;j++){
! 	    if(runmode==0){
! 		if(rev) pathj=path[N-j];
! 		else pathj=path[j-1];
! 		tej=1; 
! 	    }
! 	    else if(runmode==1){ pathj=path[j-1]; tej=te[j-1]+1; }
  	    else if(runmode==2){ pathj=path[N-j]; tej=te[N-j]+1; }
  	    else{ pathj=path[j-1]; tej=max-te[j-1]+1; }
  	    if(inmode==Input_number)
***************
*** 758,764 ****
  	}
  	printf("\n%s",AUCONN);
  	for(j=1;j<=N;j++){
! 	    if(runmode==1) tej=te[j-1]+1;
  	    else if(runmode==2) tej=te[N-j]+1;
  	    else tej=max-te[j-1]+1;
  	    for(k=1;k<=-groupnum;k++) printf(" null");
--- 807,814 ----
  	}
  	printf("\n%s",AUCONN);
  	for(j=1;j<=N;j++){
! 	    if(runmode==0) tej=1;
! 	    else if(runmode==1) tej=te[j-1]+1;
  	    else if(runmode==2) tej=te[N-j]+1;
  	    else tej=max-te[j-1]+1;
  	    for(k=1;k<=-groupnum;k++) printf(" null");
***************
*** 771,778 ****
  void putshlesstail(int M,int cdmode,int num)
  {
      int j;
! 
!     printf("%s [%d ²óÌÜ½ªÎ»]\n",SHLESSHEAD,num);
      printf("%s",AUCONN);
      for(j=1;j<=M;j++) printf(" %d",j);
      printf(" no ju n ni null 1 ga i chi ba n"); /* ¡Ö¤Î½ç¤Ë¡¢1 ¤¬°ìÈÖ¡× */
--- 821,828 ----
  void putshlesstail(int M,int cdmode,int num)
  {
      int j;
!     
!     printf("%s [%d ²óÌÜ½ªÎ»]\n",SHLESSHEAD,(num<0)?(-num):num);
      printf("%s",AUCONN);
      for(j=1;j<=M;j++) printf(" %d",j);
      printf(" no ju n ni null 1 ga i chi ba n"); /* ¡Ö¤Î½ç¤Ë¡¢1 ¤¬°ìÈÖ¡× */
***************
*** 781,786 ****
--- 831,841 ----
      printf(" ni na ru yo o ni ka sa ne te"); /* ¡Ö¤Ë¤Ê¤ë¤è¤¦¤Ë¤«¤µ¤Í¤Æ¡× */
      if(cdmode==Mode_ura) printf(" null u ra ka e si te"); /* ¡Ö¡¢Î¢ÊÖ¤·¤Æ¡× */
      printf(" ku da sa i %s\n",AUPLAY); /* ¡Ö²¼¤µ¤¤¡£¡× */
+     if(num<0){
+ 	printf("%s",AUCONN);
+ 	printf(" ko re de null shu u ryo o de su %s\n",AUPLAY);
+ 	/* ¡Ö¤³¤ì¤Ç½ªÎ»¤Ç¤¹¡× */
+     }
  }
  
  void putmode(int N,int M,int K,int inmode,int cdmode,int cdlast)
***************
*** 793,825 ****
  
  void putpath(int N,int path[],int inmode,Indata b[],int cdmode,int num)
  {
!     int j;
  
      if(inmode==Input_number){
  	printf("#");
! 	if(num==0 || (cdmode == Mode_omote && num%2==0)) /* µÕÅ¾ */
! 	  for(j=1;j<=N;j++) printf(" %d",b[path[N-j]].n);
! 	else /* Àµ½ç */
! 	  for(j=1;j<=N;j++) printf(" %d",b[path[j-1]].n);
  	printf("\n");
      }
      else{
! 	if(num==0 || (cdmode == Mode_omote && num%2==0)) /* µÕÅ¾ */
! 	  for(j=1;j<=N;j++) printf("# %s\n",b[path[N-j]].s);
! 	else /* Àµ½ç */
! 	  for(j=1;j<=N;j++) printf("# %s\n",b[path[j-1]].s);
      }
  }
  
  void putpath2(int N,int path[],int bn[],int cdmode,int num)
  {
!     int j;
  
      printf("#");
!     if(num==0 || (cdmode == Mode_omote && num%2==0)) /* µÕÅ¾ */
!       for(j=1;j<=N;j++) printf(" %d",bn[path[N-j]]);
!     else /* Àµ½ç */
!       for(j=1;j<=N;j++) printf(" %d",bn[path[j-1]]);
      printf("\n");
  }
  
--- 848,894 ----
  
  void putpath(int N,int path[],int inmode,Indata b[],int cdmode,int num)
  {
!     int j,rev;
! 
!     if(num<0){
! #ifdef DEBUG2
! 	printf("DEBUG2: (cdmode,-num)=(%d,%d)\n",cdmode,-num);
! #endif
! 	if(cdmode==Mode_omote && (-num)%2==1) rev=1;
! 	else rev=0;
!     }
!     else if(cdmode==Mode_omote && num%2==0) rev=1;
!     else rev=0;
  
      if(inmode==Input_number){
  	printf("#");
! 	if(rev) for(j=1;j<=N;j++) printf(" %d",b[path[N-j]].n); /* µÕÅ¾ */
! 	else for(j=1;j<=N;j++) printf(" %d",b[path[j-1]].n); /* Àµ½ç */
  	printf("\n");
      }
      else{
! 	if(rev) for(j=1;j<=N;j++) printf("# %s\n",b[path[N-j]].s); /* µÕÅ¾ */
! 	else for(j=1;j<=N;j++) printf("# %s\n",b[path[j-1]].s); /* Àµ½ç */
      }
  }
  
  void putpath2(int N,int path[],int bn[],int cdmode,int num)
  {
!     int j,rev;
  
      printf("#");
!     if(num<0){
! #ifdef DEBUG2
! 	printf("DEBUG2: (cdmode,-num)=(%d,%d)\n",cdmode,-num);
! #endif
! 	if(cdmode==Mode_omote && (-num)%2==1) rev=1;
! 	else rev=0;
!     }
!     else if(cdmode==Mode_omote && num%2==0) rev=1;
!     else rev=0;
! 
!     if(rev) for(j=1;j<=N;j++) printf(" %d",bn[path[N-j]]);  /* µÕÅ¾ */
!     else for(j=1;j<=N;j++) printf(" %d",bn[path[j-1]]); /* Àµ½ç */
      printf("\n");
  }
  
***************
*** 828,834 ****
      int j,max;
  
      printf(" ");
!     if(num==0) for(j=1;j<=N;j++) printf(" %d",1);
      else if(cdmode == Mode_omote){
  	if(num%2==0) /* µÕÅ¾ */
  	  for(j=1;j<=N;j++) printf(" %d",te[N-j]+1);
--- 897,903 ----
      int j,max;
  
      printf(" ");
!     if(num<0) for(j=1;j<=N;j++) printf(" %d",1);
      else if(cdmode == Mode_omote){
  	if(num%2==0) /* µÕÅ¾ */
  	  for(j=1;j<=N;j++) printf(" %d",te[N-j]+1);
***************
*** 864,870 ****
  	return 2;
      }
      for(j=1;j<=N;j++) bn[a[j-1]]=j;
! #ifdef DEBUG1
      putarray2(N,inmode,b,"DEBUG b[]");
      putarray2(N,inmode,c,"DEBUG c[]");
      putarray(N,a,"DEBUG a[]");
--- 933,939 ----
  	return 2;
      }
      for(j=1;j<=N;j++) bn[a[j-1]]=j;
! #if defined(DEBUG1) || defined(DEBUG2)
      putarray2(N,inmode,b,"DEBUG b[]");
      putarray2(N,inmode,c,"DEBUG c[]");
      putarray(N,a,"DEBUG a[]");
***************
*** 876,897 ****
  	return 3;
      }
      if(opt==Opt_M){
! 	if((K=getOptM(K,N,M,a,p,L,mode,&blocks,&cdmode,&cdlast,&isAdd,&cdM))<0){
  	    /* error */
  	    return 3;
  	}
      }
      else cdM=M;
! #ifdef DEBUG1
      putarray(N,a,"DEBUG a[]");
      putarray(blocks,p,"DEBUG p[]");
      printf("DEBUG: (N,M,blocks,K)=(%d,%d,%d,%d)\n",N,M,blocks,K);
      printf("DEBUG: (mode,cdmode,cdlast,isAdd,cdM,opt)=(%d,%d,%d,%d,%d,%d)\n",
         mode,cdmode,cdlast,isAdd,cdM,opt);
  #endif
!     if(K==0) return 0;
      else if(K>MaxK) return 20-ret;
      cparray(N,a,path[0]);
      for(j=1;j<=K;j++){
  	if((blocks=OneStep(N,a,d,blocks,p,cdM,te[j-1]))<0) return 30-ret;
  	cparray(N,d,a);
--- 945,970 ----
  	return 3;
      }
      if(opt==Opt_M){
! 	K=getOptM(K,N,M,a,p,L,mode,&blocks,&cdmode,&cdlast,&isAdd,&cdM);
! 	if(K<0){
  	    /* error */
  	    return 3;
  	}
      }
      else cdM=M;
! #if defined(DEBUG1) || defined(DEBUG2)
      putarray(N,a,"DEBUG a[]");
      putarray(blocks,p,"DEBUG p[]");
      printf("DEBUG: (N,M,blocks,K)=(%d,%d,%d,%d)\n",N,M,blocks,K);
      printf("DEBUG: (mode,cdmode,cdlast,isAdd,cdM,opt)=(%d,%d,%d,%d,%d,%d)\n",
         mode,cdmode,cdlast,isAdd,cdM,opt);
  #endif
!     if(K+isAdd<=0) return 0;
      else if(K>MaxK) return 20-ret;
      cparray(N,a,path[0]);
+ #ifdef DEBUG2
+     printf("DEBUG2: path[0]"); putarray(N,path[0],"");
+ #endif
      for(j=1;j<=K;j++){
  	if((blocks=OneStep(N,a,d,blocks,p,cdM,te[j-1]))<0) return 30-ret;
  	cparray(N,d,a);
***************
*** 900,915 ****
  	putarray(N,a,"DEBUG a[]");
  	putarray(blocks,p,"DEBUG p[]");
  #endif
      }
!     putheader(outmode,N,M,inmode,L,mode,K,cdmode,cdlast,cdM);
      for(j=K;j>=1;j--){
  	puthistory(outmode,N,path[j],inmode,b,bn,cdmode,K-j+1);
  	putbody(outmode,N,cdM,path[j],inmode,b,te[j-1],cdmode,K-j+1);
      }
      puthistory(outmode,N,path[0],inmode,b,bn,cdmode,K+1);
      if(isAdd){
! 	putbody(outmode,N,cdM,path[0],inmode,b,NULL,cdmode,0);
! 	puthistory(outmode,N,path[0],inmode,b,bn,cdmode,0);
      }
      return 0;
  }
--- 973,992 ----
  	putarray(N,a,"DEBUG a[]");
  	putarray(blocks,p,"DEBUG p[]");
  #endif
+ #ifdef DEBUG2
+ 	printf("DEBUG2: path[%d]",j); putarray(N,path[j],"");
+ 	printf("DEBUG2: te[%d]",j-1); putarray(N,te[j-1],"");
+ #endif
      }
!     putheader(outmode,N,M,inmode,L,mode,K+isAdd,cdmode,cdlast,cdM);
      for(j=K;j>=1;j--){
  	puthistory(outmode,N,path[j],inmode,b,bn,cdmode,K-j+1);
  	putbody(outmode,N,cdM,path[j],inmode,b,te[j-1],cdmode,K-j+1);
      }
      puthistory(outmode,N,path[0],inmode,b,bn,cdmode,K+1);
      if(isAdd){
! 	putbody(outmode,N,cdM,path[0],inmode,b,NULL,cdmode,-K-1);
! 	puthistory(outmode,N,path[0],inmode,b,bn,cdmode,-K-1);
      }
      return 0;
  }
