Name That Number


/*
ID: tausiq11
PROG: namenum
LANG: C++
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

char save [5000] [15];
char combine [15];
bool flag = true;

void b_search (int left, int right)
{
    if ( right < left )
        return;

    int mid = floor ( (left+right) / 2);

    if ( strcmp (save [mid], combine) == 0) {
        printf ("%s\n", combine);
        flag = false;
    }

    if ( strcmp (save [mid], combine) > 0 )
        b_search (left, mid - 1);
    else
        b_search (mid + 1, right);
}

int main ()
{
    FILE *fp;
    fp = fopen ("dict.txt", "r");

    int i = 0;

    while ( fscanf (fp, "%s", save [i++]) != EOF );

    fclose (fp);

    freopen ("namenum.in", "r", stdin);
	freopen ("namenum.out", "w", stdout);

    char input [15];
    char ch [8] [4] = {"ABC", "DEF", "GHI", "JKL",
                        "MNO", "PRS", "TUV", "WXY"};

    scanf ("%s", input);
    int length = strlen (input);
    flag = true;

    int i0 = 0;
    while ( i0 < 3 ) {
        combine [0] = ch [input [0] - 50] [i0++];
        combine [1] = 0;

        int i1 = (length > 1) ? 0 : 3;
        if ( length == 1 )
            b_search (0, i - 1);
        while ( i1 < 3 ) {
            combine [1] = ch [input [1] - 50] [i1++];
            combine [2] = 0;

            int i2 = (length > 2) ? 0 : 3;
            if ( length == 2 )
                b_search (0, i - 1);
            while ( i2 < 3 ) {
                combine [2] = ch [input [2] - 50] [i2++];
                combine [3] = 0;

                int i3 = (length > 3) ? 0 : 3;
                if ( length == 3 )
                    b_search (0, i - 1);
                while ( i3 < 3 ) {
                    combine [3] = ch [input [3] - 50] [i3++];
                    combine [4] = 0;

                    int i4 = (length > 4) ? 0 : 3;
                    if (length == 4 )
                        b_search (0, i - 1);
                    while ( i4 < 3 ) {
                        combine [4] = ch [input [4] - 50] [i4++];
                        combine [5] = 0;

                        int i5 = (length > 5) ? 0 : 3;
                        if (length == 5 )
                            b_search (0, i - 1);
                        while ( i5 < 3 ) {
                            combine [5] = ch [input [5] - 50] [i5++];
                            combine [6] = 0;

                            int i6 = (length > 6) ? 0 : 3;
                            if (length == 6 )
                                b_search (0, i - 1);
                            while ( i6 < 3 ) {
                                combine [6] = ch [input [6] - 50] [i6++];
                                combine [7] = 0;

                                int i7 = (length > 7) ? 0 : 3;
                                if (length == 7 )
                                    b_search (0, i - 1);
                                while ( i7 < 3 ) {
                                    combine [7] = ch [input [7] - 50] [i7++];
                                    combine [8] = 0;

                                    int i8 = (length > 8 ) ? 0 : 3;
                                    if (length == 8 )
                                        b_search (0, i - 1);
                                    while ( i8 < 3 ) {
                                        combine [8] = ch [input [8] - 50] [i8++];
                                        combine [9] = 0;

                                        int i9 = (length > 9) ? 0 : 3;
                                        if (length == 9 )
                                            b_search (0, i - 1);
                                        while ( i9 < 3 ) {
                                            combine [9] = ch [input [9] - 50] [i9++];
                                            combine [10] = 0;

                                            int i10 = (length > 10) ? 0 : 3;
                                            if (length == 10 )
                                                b_search (0, i - 1);
                                            while ( i10 < 3 ) {
                                                combine [10] = ch [input [10] - 50] [i10++];
                                                combine [11] = 0;

                                                int i11 = (length > 11) ? 0 : 3;
                                                if (length == 11 )
                                                    b_search (0, i - 1);
                                                while ( i11 < 3 ) {
                                                    combine [11] = ch [input [11] - 50] [i11++];
                                                    combine [12] = 0;
                                                    b_search (0, i - 1);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if ( flag )
        printf ("NONE\n");

    return 0;
}
Advertisements

2 thoughts on “Name That Number

  1. Can you please explain the program’s logic – purpose of combine[] and b_search(), i0++, i1++, etc.? It’s confsing b/c there aren’t any declarations for the variabels i0, i1, i2, i3, etc.

  2. @ Anonymous
    I am sorry i can’t do that right now .. you can take help from USACO forum .. good luck 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s