WA #10
Posted by
AnTanTo 12 Nov 2009 23:29
I have WA#10.
I can't find my error.
Please, help!
(C#)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
while (true)
{
String Number = Console.ReadLine().Trim();
if (Number == "-1")
{
break;
}
SortedList<String, String> Dict = new SortedList<string, string>();
Int32 DictSize = Convert.ToInt32(Console.ReadLine().Trim());
for (int i = 0; i < DictSize; i++)
{
String Val = Console.ReadLine().Trim();
String Key = getPhoneByWord(Val);
if (!Dict.Keys.Contains(Key) && Key.Length > 0)
{
Dict.Add(Key, Val);
}
}
if (Dict.Count == 0 || Number.Trim().Length == 0)
{
Console.WriteLine("No solution.");
}
else
{
LinkedList<Byte>[] WaweDict = GetWaweDict(Number, Dict);
LinkedList<Byte> Zero = new LinkedList<Byte>();
Zero.AddLast(0);
SortedList<Byte, Byte> RP = new SortedList<Byte, Byte>();
Console.WriteLine(RPtoOutput(WaweForward(Zero, RP, WaweDict, Convert.ToByte(Number.Length)), Dict, Convert.ToByte(Number.Length), Number).Trim());
}
}
}
private static SortedList<String, String> replaceVata(SortedList<String, String> Input, String Number)
{
SortedList<String, String> Dict = new SortedList<string,string>();
foreach (KeyValuePair<String, String> KVP in Input)
{
if (Number.Contains(KVP.Key))
{
Dict.Add(KVP.Key, KVP.Value);
}
}
return Dict;
}
private static String getPhoneByWord(String Input)
{
String Result = Input.Replace('i', '1');
Result = Result.Replace('j', '1');
Result = Result.Replace('a', '2');
Result = Result.Replace('b', '2');
Result = Result.Replace('c', '2');
Result = Result.Replace('d', '3');
Result = Result.Replace('e', '3');
Result = Result.Replace('f', '3');
Result = Result.Replace('g', '4');
Result = Result.Replace('h', '4');
Result = Result.Replace('k', '5');
Result = Result.Replace('l', '5');
Result = Result.Replace('m', '6');
Result = Result.Replace('n', '6');
Result = Result.Replace('p', '7');
Result = Result.Replace('r', '7');
Result = Result.Replace('s', '7');
Result = Result.Replace('t', '8');
Result = Result.Replace('u', '8');
Result = Result.Replace('v', '8');
Result = Result.Replace('w', '9');
Result = Result.Replace('x', '9');
Result = Result.Replace('y', '9');
Result = Result.Replace('o', '0');
Result = Result.Replace('q', '0');
Result = Result.Replace('z', '0');
return Result;
}
private static LinkedList<Byte>[] GetWaweDict(String Number, SortedList<String, String> Dict)
{
LinkedList<Byte>[] Result = new LinkedList<Byte>[Number.Length + 1];
for (int i = 0; i < Result.Length; i++)
{
Result[i] = new LinkedList<byte>();
}
foreach (String Key in Dict.Keys)
{
String NumTemp = Number;
Int32 Ins = NumTemp.IndexOf(Key);
while (Ins != -1)
{
if (!Result[Ins].Contains(Convert.ToByte(Ins + Key.Length)))
{
Result[Ins].AddLast(Convert.ToByte(Ins + Key.Length));
}
String Placebo = "";
for (int i = 0; i < Key.Length; i++)
{
Placebo += " ";
}
NumTemp = NumTemp.Substring(0, Ins) + Placebo + NumTemp.Substring(Ins + Key.Length, NumTemp.Length - Ins - Key.Length);
Ins = NumTemp.IndexOf(Key);
}
}
return Result;
}
private static SortedList<Byte, Byte> WaweForward(LinkedList<Byte> Starts, SortedList<Byte, Byte> RP, LinkedList<Byte>[] WaweDict, Byte LenPlus1)
{
Boolean Drop = true;;
LinkedList<Byte> RecurseInp = new LinkedList<byte>();
foreach (Byte Start in Starts)
{
foreach (Byte End in WaweDict[Start])
{
if (!RecurseInp.Contains(End))
{
RecurseInp.AddLast(End);
}
if (!RP.ContainsKey(End))
{
Drop = false;
RP.Add(End, Start);
}
}
}
if (Drop)
{
return new SortedList<byte,byte>();
}
else
{
if (RP.ContainsKey(LenPlus1))
{
return RP;
}
else
{
return WaweForward(RecurseInp, RP, WaweDict, LenPlus1);
}
}
}
private static String RPtoOutput(SortedList<Byte, Byte> RP, SortedList<String, String> Dict,Byte LenPlus1,String Number)
{
if (LenPlus1 > 0)
{
if (RP.ContainsKey(LenPlus1))
{
return RPtoOutput(RP, Dict, RP[LenPlus1], Number) + " " + Dict[Number.Substring(RP[LenPlus1], LenPlus1 - RP[LenPlus1])];
}
else
{
return "No solution.";
}
}
else
{
return "";
}
}
}
}