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

Wrong answer for test №3
Posted by kiriloff 28 Jul 2009 18:09
I get "wrong answer" for test №3:
2687809 17:47:13
28 июл 2009 kiriloff 1002 C# Wrong answer 3 0.109 1 129 КБ
Can you help me, what problem of this?
Re: Wrong answer for test №3
Posted by kiriloff 30 Jul 2009 15:41
Please help, what is wrong answer?! I think that you should display input data of test and correct result, because users can't understand what trouble in this.
I propose to see my answer, because all tests that i think up works correctly:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace _1002
{
    internal class _1002
    {
        //public const string par = "7325732573257325\n5\nit\nyour\nreal\nreality\nour\n4294967296\n5\nit\nyour\nreality\nreal\nour\n-1\n";


        private const string notFoundAnswer = "No solution.";
        private const string space = " ";
        private static Dictionary<char, char> phoneNumberValues = new Dictionary<char, char>();
        private static NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;

        static void Main( )
        {
            string[] input = Console.In.ReadToEnd().Split( new char[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries );
            //string[] input = par.Split( new char[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries );

            FillPhoneNumberValues();

            int i = 0;
            while( i < input.Length )
            {
                string number = input[i];
                //check last line
                int exitFlag;
                if( number.Length == 2 && int.TryParse( number, out exitFlag ) )
                {
                    if(exitFlag == -1)
                    {
                        break;
                    }
                }

                i++;

                int countOfWords = int.Parse( input[i], nfi );
                i++;
                string[] words = new string[countOfWords];
                for( int j = 0; j < countOfWords; j++ )
                {
                    words[j] = input[i];
                    i++;
                }
                Dictionary<string, string> wordsWithNumbers = FormNumbersForWords( words );
                FormAnswer(number, 0, wordsWithNumbers, new StringBuilder());
            }
            //Console.ReadLine();
        }
        private static Dictionary<string, string> FormNumbersForWords( string[] words )
        {
            Dictionary<string, string> wordsWithNumbers = new Dictionary<string, string>();
            for( int i = 0; i < words.Length; i++ )
            {
                string word = words[i];
                StringBuilder wordAsNumber = new StringBuilder();
                foreach (char letter in word)
                {
                    wordAsNumber.Append(phoneNumberValues[letter]);
                }
                wordsWithNumbers.Add(wordAsNumber.ToString(), word);
            }
            return wordsWithNumbers;
        }

        private static int FormAnswer( string inputNumber, int index, Dictionary<string, string> wordsWithNumbers, StringBuilder result )
        {
            foreach(string number in wordsWithNumbers.Keys)
            {
                if( index < inputNumber.Length && inputNumber.Substring(index).StartsWith( number ) )
                {
                    // append space if need
                    if( index != 0)
                    {
                        result.Append(space);
                    }
                    // append variant of word
                    result.Append( wordsWithNumbers[number] );

                    //end of input line and result formed correctly : write result and exit
                    if( index + number.Length == inputNumber.Length )
                    {
                        Console.WriteLine( string.Format( nfi, result.ToString() ) );
                        index = inputNumber.Length;
                    }
                    // continue to form result (index always less than inputNumber.Length - 1)
                    else
                    {
                        index = FormAnswer( inputNumber, index + number.Length, wordsWithNumbers, result);
                    }
                }
            }
            //if result contains words (with spaces) formed recursively
            string resultAsString = result.ToString();
            if( resultAsString.Length > 0 )
            {
                int spaceIndex = resultAsString.LastIndexOf( space );
                if( spaceIndex > 0 )
                {
                    // remove last word
                    result.Remove(spaceIndex, resultAsString.Length - spaceIndex);
                    return resultAsString.Length - spaceIndex;
                }
                else
                {
                    // clear result
                    result.Remove( 0, result.Length );
                    return 0;
                }
            }
            //result is empty so all variants are not consist for us
            else
            {
                Console.WriteLine( string.Format( nfi, notFoundAnswer ) );
                return inputNumber.Length;
            }
        }

        private static void FillPhoneNumberValues()
        {
            phoneNumberValues.Add( 'i', '1' );
            phoneNumberValues.Add( 'j', '1' );
            phoneNumberValues.Add( 'a', '2' );
            phoneNumberValues.Add( 'b', '2' );
            phoneNumberValues.Add( 'c', '2' );
            phoneNumberValues.Add( 'd', '3' );
            phoneNumberValues.Add( 'e', '3' );
            phoneNumberValues.Add( 'f', '3' );
            phoneNumberValues.Add( 'g', '4' );
            phoneNumberValues.Add( 'h', '4' );
            phoneNumberValues.Add( 'k', '5' );
            phoneNumberValues.Add( 'l', '5' );
            phoneNumberValues.Add( 'm', '6' );
            phoneNumberValues.Add( 'n', '6' );
            phoneNumberValues.Add( 'p', '7' );
            phoneNumberValues.Add( 'r', '7' );
            phoneNumberValues.Add( 's', '7' );
            phoneNumberValues.Add( 't', '8' );
            phoneNumberValues.Add( 'u', '8' );
            phoneNumberValues.Add( 'v', '8' );
            phoneNumberValues.Add( 'w', '9' );
            phoneNumberValues.Add( 'x', '9' );
            phoneNumberValues.Add( 'y', '9' );
            phoneNumberValues.Add( 'o', '0' );
            phoneNumberValues.Add( 'q', '0' );
            phoneNumberValues.Add( 'z', '0' );
        }
    }
}