ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1002. Phone Numbers

Why i got Mem Limit Exceeded, but all is ok ??? 1002 problem
Posted by NooD 11 May 2003 12:29
#include <iostream.h>
#include <string.h>

const max_num_len = 100;
const max_word_len = 50;

char conv(char a) {
    if (a == 'i' || a == 'j') return '1';
    if (a == 'a' || a == 'b' || a == 'c') return '2';
    if (a == 'd' || a == 'e' || a == 'f') return '3';
    if (a == 'g' || a == 'h') return '4';
    if (a == 'k' || a == 'l') return '5';
    if (a == 'm' || a == 'n') return '6';
    if (a == 'p' || a == 'r' || a == 's') return '7';
    if (a == 't' || a == 'u' || a == 'v') return '8';
    if (a == 'w' || a == 'x' || a == 'y') return '9';
    return '0';
}

main() {
    char *num = new char [max_num_len + 1];
    char *word = new char [max_word_len + 1];
    char *res;
    char **words, **codes;
    unsigned int *d0, *d1;
    long n, i, j;
    char *ptr;
    unsigned int s0, s1;
    long stack[max_num_len][2], stackp;
    long st0, st1;
    long way[100];
    long best[100], mink = 200;
    char pr[2] = " ";

    while (1) {
        cin >> num;
        if (strcmp(num, "-1") == 0) break;
        cin >> n;
        words = (char **) new char *[n];
        codes = (char **) new char *[n];
        d0 = new unsigned int [n];
        d1 = new unsigned int [n];
        for (i = 0; i < n; i++) {
            cin >> word;
            words[i] = new char [strlen(word) + 1];
            codes[i] = new char [strlen(word) + 1];
            strcpy(words[i], word);
            // codes generating
            for (j = 0; j < strlen(word); j++)
                codes[i][j] = conv(word[j]);
            codes[i][strlen(word)] = 0;
        }
        // d0 and d1 generating
        for (j = 0; j < n; j++) {
            if (ptr = strstr(num, codes[j])) {
                s0 = ptr - num;
                s1 = ptr - num + strlen(codes[j]) - 1;
                d0[j] = s0;
                d1[j] = s1;
            }
                else {
                    d0[j] = 60000;
                    d1[j] = 60000;
                }
        }
        res = new char [max_num_len*2 + 1];
        res[0] = 0;
        // Stack generating
        stackp = 0;
        for (i = 0; i < n; i++) {
            if (d0[i] == 0) {
                stackp++;
                stack[stackp-1][0] = i;
                stack[stackp-1][1] = 0;
            }
        }
        if (stackp == 0) {
            strcpy(res, "No solution.");
        }
            else {
                while (stackp != 0) {
                    st0 = stack[stackp-1][0];
                    st1 = stack[stackp-1][1];
                    way[st1] = st0;
                    stackp--;
                    for (i = 0; i < n; i++) {
                        if (d1[st0] == strlen
(num)-1) {
                            if (st1 <
mink) {
                                mink
= st1+1;
                                for
(j = 0; j < mink; j++)

    best[j] = way[j];
                            }
                            break;
                        } else
                        if (d0[i] == d1[st0]
+1) {
                            stackp++;
                            stack[stackp-
1][0] = i;
                            stack[stackp-
1][1] = st1 + 1;
                        }
                    }
                }
            if (mink == 200) {
                strcpy(res, "No solution.");
            }
                else {
                    for (i = 0; i < mink; i++) {
                        strcat(res, words[best
[i]]);
                        strcat(res, pr);
                    }
                    res[strlen(res)-1] = 0;
                }
            }
        cout << res << "\n";

        delete [] words;
        delete [] codes;
        delete [] d0;
        delete [] d1;
    }
}