#include <stdio.h>

int maxsites = 100 ;

main(argc,argv)
	int argc;
	char *argv[];
{
	int nsam, j ,nsites, i,  howmany  ;
	char **list, **cmatrix(), allele,na, line[201]  ;
	FILE *pf, *fopen(), *pfin ;
	double *posit   ;
	int   segsites, count  ;

pfin = stdin ;
fgets( line, 200, pfin);
sscanf(line," %*s  %d %d", &nsam, &howmany);
fgets( line, 200, pfin);





	list = cmatrix(nsam,maxsites+1);
     posit = (double *)malloc( maxsites*sizeof( double ) ) ;

	count=0;
while( howmany-count++ ) {
  fscanf(pfin," //");
  fscanf(pfin,"  segsites: %d", &segsites );
  if( segsites >= maxsites){
	maxsites = segsites + 10 ;
	posit = (double *)realloc( posit, maxsites*sizeof( double) ) ;
        biggerlist(nsam,maxsites, list) ;
        }
/*	   readsamples */
   if( segsites > 0) {
	fscanf(pfin," positions:");
	for( i=0; i<segsites ; i++) fscanf(pfin," %lf",posit+i) ;
	for( i=0; i<nsam;i++) fscanf(pfin," %s", list[i] );

	printf(" %d %d\n", nsam, segsites ) ;
    	for(i=0; i<segsites; i++) 
		  fprintf(stdout," %6.4lf",posit[i] ) ;
	fprintf(stdout,"\n");
	if( argc > 1 ) {
	    fprintf(stdout,"anc: ");
	    for( j=0; j<segsites; j++) fprintf(stdout," 0");
	    fprintf(stdout,"\n");
	}
    	for( j=0;j<nsam; j++){
	  printf("g%d ", j);
	  for( i=0; i<segsites; i++)
		fprintf(stdout," %c",list[j][i] );  
          printf("\n");
	  }
       }
   else { printf(" %d %d", nsam,segsites); }
  printf("\n//\n");
  }
}

	

/* allocates space for gametes (character strings) */
	char **
cmatrix(nsam,len)
	int nsam, len;
{
	int i;
	char **m;

	if( ! ( m = (char **) malloc( (unsigned)( nsam*sizeof( char* )) ) ) )
	   perror("alloc error in cmatrix") ;
	for( i=0; i<nsam; i++) {
		if( ! ( m[i] = (char *) malloc( (unsigned) (len*sizeof( char )) )))
			perror("alloc error in cmatric. 2");
		}
	return( m );
}

        int
biggerlist(nsam, nmax, list )
        int nsam ;
        unsigned nmax ;
        char ** list ;
{
        int i;

        maxsites = nmax  ;
        for( i=0; i<nsam; i++){
           list[i] = (char *)realloc( list[i],maxsites*sizeof(char) ) ;
           if( list[i] == NULL ) perror( "realloc error. bigger");
           }
}                        

