int getseq(char *s)
{
    int r = 0;
    int i = 1 << (SEQLEN - 1);

    while (*s && i) {
        switch (*s++) {
            case 'H':
            case 'h':
                r |= i;
                break;
            case 'T':
            case 't':
                
                break;
            default:
                return -1;
        }
        i >>= 1;
    }

    return r;
}

void printseq(int seq)
{
    int i;
    for (i = SEQLEN - 1; i >= 0; --i)
        printf(, seq & (1 << i) ? 'h' : 't');
}

int getuser(void)
{
    int user;
    char s[SEQLEN + 1];

    printf(, SEQLEN);
    while (1) {
        
        if (scanf(, s) != 1) exit(1);
        if ((user = getseq(s)) != -1) return user;
        printf();
    }
}

int getai(int user)
{
    int ai;

    printf(, SEQLEN);
    
    if (user == -1)
        ai = rand() & (1 << SEQLEN) - 1;
    else
        ai = (user >> 1) | ((~user << 1) & (1 << SEQLEN - 1));

    printseq(ai);
    printf();
    return ai;
}

int rungame(int user, int ai)
{
    
    int last3 = rand() & (1 << SEQLEN) - 1;

    printf();
    printseq(last3);
    while (1) {
        if (user == last3) {
            printf();
            return 1;
        }

        if (ai == last3) {
            printf();
            return 0;
        }

        last3 = ((last3 << 1) & (1 << SEQLEN) - 2) | (rand() & 1);
        printf(, last3 & 1 ? 'h' : 't');
    }
}

int main(void)
{
    srand(time(NULL));
    int playerwins = 0;
    int totalgames = 0;

    
    while (1) {
        int user = -1;
        int ai   = -1;

        printf();
        if (rand() & 1) {
            ai   = getai(user);
            user = getuser();
        }
        else {
            user = getuser();
            ai   = getai(user);
        }

        playerwins += rungame(user, ai);
        totalgames++;

        printf(, playerwins, totalgames);
        printf();
    }

    return 0;
}