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 it's wrong in test 4?
Posted by NewCraft 1 Oct 2015 17:06
#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;
const int ArrSize = 100;

struct Number
{
    int Num;
    char Get[3];
};
const Number tel[10] = { { 0, 'o', 'q', 'z' }, { 1, 'i', 'j' }, { 2, 'a', 'b', 'c' }, { 3, 'd', 'e', 'f' }, { 4, 'g', 'h' }, { 5, 'k', 'l' }, { 6, 'm', 'n' }, { 7, 'p', 'r', 's' }, { 8, 't', 'u', 'v' }, { 9, 'w', 'x', 'y' } };
void Func(int num[], int, char t[][51]);
char f1[ArrSize], f2[ArrSize], temp[ArrSize + 2], temp2[50000][51];
unsigned int counter1 = 0, counter2 = 0, k = 0, size = 0;
void main()
{
    int number[ArrSize], voc;

    for (;;)
    {
        cin.getline(temp, ArrSize + 1);
        if (temp[0] == '-' && temp[1] == '1') break;
        while (size<strlen(temp))
        {
            number[size] = temp[size] - '0';
            size++;
        }
        cin.clear();
        cin >> voc;
        cin.get();
        for (int j = 0; j < voc; j++)
        {
            cin.getline(temp2[j], 50);
        }
        Func(number, voc, temp2);
        if (strlen(f2) != 0)
        {
            int temporary = 0;
            while ((strlen(f2) - temporary) != 0)
            {
                if (temporary != 0) cout << ' ';
                cout << temp2[(f2[temporary] - '0')];
                temporary++;
            }
            cout << endl;
        }
        else
            cout << "No solution." << endl;
        k = size = counter1 = voc = 0;
        memset(f1, NULL, ArrSize);
        memset(f2, NULL, ArrSize);
        memset(temp, NULL, ArrSize + 1);
        for (int z = 0; z < 51; z++)
        memset(temp2[z], NULL, 51);
        memset(number, 0, ArrSize);

    }
}
void Func(int number[], int voc, char temp2[][51])
{
    unsigned int j = 0;
    for (int i = 0; i < voc; i++)
    {
        if (strlen(temp2[i])<=(size - k))
            for (j = 0; j < strlen(temp2[i]); j++)
        {
            if ((tel[number[j + k]].Get[0] != temp2[i][j]) && (tel[number[j + k]].Get[1] != temp2[i][j]) && (tel[number[j + k]].Get[2] != temp2[i][j]))
                break;
        }
        if (j == strlen(temp2[i]))
        {
            k += j;
            f1[counter1] = i + '0';
            if (size > k)
            {
                counter1++;
                Func(number, voc, temp2);
                counter1--;
            }
            if ((counter2 > counter1 || strlen(f2) == 0) && k == size)
            {
                strcpy_s(f2,f1);
                counter2 = counter1;
            }
                k -= j;
                f1[counter1] = NULL;
        }
    }
}