/*  bug fixed 4/21/03  */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


#define LEN 20


main(int argc, char *argv[])
{
	int  s1, nsites,  i , j, k, *nall, count, nsam  ;
	int a, b, count1, count2, countq ;
	int  s2 , n00, n01, n10, n11, uflag, cflag, flag ;
	char ***gam, **anc ;
	int **ngam, aa  ;
	double  *pos ;
	double p0, q0, n ; 
	  int n0q, n1q, nq0,  nq1, nqq ;
	int  i1, j1, nsitesmax  ;

flag = 0 ;
cflag = 0 ;
uflag = 0 ;
if( argc > 1 ){
	uflag = ( argv[1][1] == 'u' ? 1 : 0 ) ;
	if( ( argv[1][1] == 'c' ) || (argv[1][2] == 'c' ) ) cflag  = 1 ;
	}
scanf(" %d", &nsam ) ;
scanf(" %d", &nsites ) ;
nsitesmax = nsites + 500 ;

/* printf("uflag: %d cflag: %d\n", uflag, cflag); */
pos = (double *)malloc((size_t) (nsitesmax+1)*sizeof( double ) ) ;
nall  = (int *)malloc( (size_t)(nsitesmax+1)*sizeof( int ) ) ;
gam = (char ***)malloc( (size_t)(nsam)*sizeof( char **) ) ;
ngam = (int **)malloc( (size_t)(nsam)*sizeof( char **) ) ;
anc = ( char **)malloc( (size_t)(nsitesmax+1)*sizeof( char *) ) ;
for( i=0; i<= nsitesmax; i++) anc[i] = (char *)malloc( (size_t)LEN*sizeof( char ) ) ;

for(i=0; i< nsam; i++) {
   gam[i] = (char **)malloc( (size_t)(nsitesmax+1)*sizeof( char *) ) ;
   ngam[i] = (int *)malloc( (size_t)(nsitesmax+1)*sizeof( int ) ) ;
   for( j=0; j<=nsitesmax; j++) gam[i][j] = (char *) malloc( (size_t)LEN*sizeof(char) ) ;
	}

while(1){

   if( nsites >= nsitesmax - 1 ) {
	pos = (double *)realloc(pos,(size_t) (nsites+20)*sizeof( double ) ) ;
	nall  = (int *)realloc( nall,(size_t)(nsites+20)*sizeof( int ) ) ;
	anc = ( char **)realloc( anc, (size_t)(nsites+20)*sizeof( char *) ) ;
for( i=nsitesmax+1; i< nsites+20; i++) anc[i] = (char *)malloc( (size_t)LEN*sizeof( char ) ) ;

    for(i=0; i< nsam; i++) {
      gam[i] = (char **)realloc(gam[i], (size_t)(nsites+20)*sizeof( char *) ) ;
      ngam[i] = (int *)realloc(ngam[i], (size_t)(nsites+20)*sizeof( int ) ) ;
      for( j=nsitesmax+1; j<nsites+20; j++) gam[i][j] = (char *) malloc( (size_t)LEN*sizeof(char) ) ;
        }   
	nsitesmax = nsites + 20 ;
      }

/* printf("nsam: %d nsites: %d\n", nsam, nsites);  */
for( i=1; i<= nsites; i++) scanf(" %lf",pos+i );
/* printf(" %lf %lf \n",pos[1],pos[2] ) ; */
if( !uflag) {
/* printf("uflag: %d\n", uflag);  */
   scanf(" %s", anc[0] ) ;
   for( i=1; i<= nsites; i++){
     if( cflag ) { scanf(" %c", anc[i] ) ; anc[i][1] = '\0' ;}
	else  scanf(" %s", anc[i] ) ;
	if( (anc[i][0] == 'n' ) || (anc[i][0] == 'N' ) ) anc[i][0]= '?' ;
   }
}
 else  for( i=1; i <= nsites; i++) { anc[i][0] = '?' ; anc[i][1] = '\0' ; }

if( nsites > 0 ){
  for( j=0; j< nsam; j++) {
     scanf(" %s", gam[j][0] ) ;
/* printf(" %s", gam[j][0] ) ; */
     if( cflag )
        for( i=1; i <= nsites; i++){
	   scanf(" %c",gam[j][i] ) ;
	   gam[j][i][1] = '\0' ;
           if( (gam[j][i][0] == 'n' ) || (gam[j][i][0] == 'N' ) ) gam[j][i][0] = '?' ;
	   }
     else
        for( i=1; i <= nsites; i++){
	   scanf(" %s",gam[j][i] ) ;
           if( (gam[j][i][0] == 'n' ) || (gam[j][i][0] == 'N' ) ) gam[j][i][0] = '?' ;
	}
  }
}

for( s1 = 1; s1 <= nsites; s1++) {
	i=0;
	for( j=0; j< nsam; j++) if( gam[j][s1][0] == '?' )  ngam[j][s1] = 3 ; else ngam[j][s1] = -1; 
	while( (i < nsam) && ( gam[i][s1][0] == '?')  ) {  i++;  }
	if( i == nsam ) nall[s1]  = 0 ;
	else{
	  if( strcmp( gam[i][s1], anc[s1]) == 0 ) aa = 0 ;
	  else aa = 1 ;
	  for( j= i+1; j< nsam; j++) if(  strcmp( gam[i][s1], gam[j][s1]  ) == 0   ) { ngam[j][s1] = aa ;  }
	  ngam[i][s1] = aa ;
	  j = i+1 ;
	  while( (j < nsam) && (( gam[j][s1][0] == '?' ) || (ngam[j][s1] == aa ) )  ) {  j++ ;  }
	  if( j == nsam ) nall[s1] = 1 ;
	  else {
	    for( k=j+1; k< nsam; k++){
		if( strcmp( gam[j][s1],  gam[k][s1]) == 0 ){  ngam[k][s1] = 1 - aa ; }
		}
	    if( aa && (strcmp( gam[j][s1], anc[s1]) != 0 ) ) anc[s1][0] = '?'  ;   
	    ngam[j][s1] = 1-aa ;
	    count1 = count2 = countq = 0 ;
	    for( i=0; i< nsam; i++){
	       a = ngam[i][s1] ;
	       if( a == 0 ) count1++;
	       else if( a == 1 ) count2++;
	       else if( a == 3 ) countq++;
	       }
	    if( count1+count2 + countq == nsam ) nall[s1] =2  ;
	    else { nall[s1] = 3 ; 
		 fprintf(stderr,"Site %d not used. ( More than two alleles at site.) \n", s1);
		}
	    }
	   }
	}
/*
for( i=0; i<nsam; i++){
  for( j=1; j<=nsites; j++)
	printf(" %d", gam[i][j][0] ) ;
   printf("\n");
}
*/

	printf("\n");

	for( s1=1; s1 < nsites; s1++){
	  if( nall[s1] == 2 ) 
	  for( s2 = s1+1; s2 <= nsites; s2++){
	    if( nall[s2] == 2 ) {
		n00 = n01 = n10 = n11 = n0q = n1q = nq0 = nq1 = nqq = 0 ;
		for( i=0; i<nsam; i++){
		  if( (ngam[i][s1] == 0 )&&( ngam[i][s2] == 0 ) ) n00++ ; 
		  else if( (ngam[i][s1] == 0 )&&( ngam[i][s2] == 1 ) ) n01++ ; 
		  else if( (ngam[i][s1] == 1 )&&( ngam[i][s2] == 0 ) ) n10++ ; 
		  else if( (ngam[i][s1] == 1 )&&( ngam[i][s2] == 1 ) ) n11++ ; 
		  else if( (ngam[i][s1] == 0 )&&( ngam[i][s2] == 3 ) ) n0q++ ; 
		  else if( (ngam[i][s1] == 1 )&&( ngam[i][s2] == 3 ) ) n1q++ ; 
		  else if( (ngam[i][s1] == 3 )&&( ngam[i][s2] == 0 ) ) nq0++ ;
		  else if( (ngam[i][s1] == 3 )&&( ngam[i][s2] == 1 ) ) {   nq1++ ; } 
		  else if( (ngam[i][s1] == 3 )&&( ngam[i][s2] == 3 ) ) nqq++ ; 
		 }
	 	if( (anc[s1][0] == '?' ) || ( anc[s2][0] == '?' ) ) flag = 1 ;
		 	else flag = 0 ;
		 printf(" %d %d  %lf  %s  %d %d %d %d %d %d %d %d %d \n ",s1, s2,fabs(pos[s2]-pos[s1]),
			 flag ? "u" : "ad", n00, n01, n10, n11, n0q, n1q, nq0, 
				nq1, nqq ) ;


		}
	     }}
	printf("//\n");
	scanf(" //");
	if( scanf(" %d %d", &nsam, &nsites) < 2 ) break;
 }
}

