## T公司的一道机试题

a, aa, aaa, aaaa, aaab, aaac, … …, b, ba, baa, baaa, baab, baac … …, yyyw, yyyx, yyyy

1）编写一个函数，输入是任意一个编码，比如baca，输出这个编码对应的Index；

2）编写一个函数，输入是任意一个Index，比如12345，输出这个Index对应的编码。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace 字典序計數{    class Node    {        /// <summary>        /// 字符        /// </summary>        public String symbol;        /// <summary>        /// 序號        /// </summary>        public int index;        /// <summary>        /// 孩子節點        /// </summary>        public Node child;        /// <summary>        /// 兄弟節點        /// </summary>        public Node brother;    }    class Tree    {        public static String [] Dict = {"a","b","c","d","e","f","g","h","i","j","k","l",                                        "m","n","o","p","q","r","s","t","u","v","w","x","y"};        public Node root;        public Tree()        {            root = new Node();            root.symbol="";            root.index = -1;            root.child = null;            root.brother = null;        }        public void GenerateTree(Node root,int height)        {            if(height==5)            {                return;            }            Node child = new Node();            child.symbol = Dict[0];            root.child = child;            GenerateTree(root.child, height + 1);            Node temp = child;            for (int i = 1; i < Dict.Length; i++)            {                Node brother = new Node();                brother.symbol=Dict[i];                GenerateTree(brother, height + 1);                temp.brother = brother;                temp = brother;            }        }        public void TraverseTree(Node root, int index, StreamWriter sw,String result)        {            if (root != null)            {                root.index = index;                sw.WriteLine(result + root.symbol + ",index=" + root.index);                //Console.WriteLine(result+root.symbol + ",index="+root.index);                TraverseTree(root.child, root.index+1,sw,result + root.symbol);                                                if (root.child == null)                {                    TraverseTree(root.brother, root.index + 1, sw, result);                }else                {                    Node ctemp = root.child;                    Node btemp = root.brother;                    while (ctemp != null)                    {                        btemp = ctemp.brother;                        while (btemp.brother != null)                        {                            btemp = btemp.brother;                        }                        ctemp = btemp.child;                    }                    TraverseTree(root.brother, btemp.index + 1, sw, result);                }            }        }        public int GetIndex(String code)        {            Node temp = root.child,goal=root.child;            int index = -1;            int len = code.Length;            int i = 0;            for (i = 0; i < len; i++)            {                if (temp.symbol.CompareTo(code[i].ToString()) == 0)                {                    goal = temp;                    temp = temp.child;                    continue;                    //i++;                }                if (temp.symbol.CompareTo(code[i].ToString()) < 0)                {                    while (temp.symbol.CompareTo(code[i].ToString()) != 0)                    {                        temp = temp.brother;                    }                    goal = temp;                    temp = temp.child;                }            }            return goal.index;        }        public String GetCode(int index)        {            Node temp = root,small=root.child,big=root.child;            String result = "";            while (temp.index != index)            {                while (temp!=null&&temp.index < index)                {                    small = temp;                    temp = temp.brother;                }                if(temp==null)                {                    result += small.symbol;                    temp = small.child;                    if (temp.index == index)                    {                        result += temp.symbol;                        break;                    }                    continue;                }                if (temp.index != index)                {                    result += small.symbol;                    big = temp;                    if (small.child != null)                    {                        temp = small.child;                        if(temp.index==index)                        {                            result += temp.symbol;                            break;                        }                    }                    else                    {                        temp = small;                    }                }else                {                    result += temp.symbol;                    break;                }            }            return result;        }    }    class Program    {        static void Main(string[] args)        {            Tree T = new Tree();            T.GenerateTree(T.root, 1);            FileStream fs = new FileStream("result.txt", FileMode.Create);            StreamWriter sw = new StreamWriter(fs);            int index = -1;            T.TraverseTree(T.root,index, sw,"");            sw.Flush();            sw.Close();            String goal = "";            while ((goal = Console.ReadLine())!="\$")            {                index = T.GetIndex(goal);                Console.WriteLine(index);                Console.WriteLine(T.GetCode(index));            }            Console.WriteLine(25 + 25 * 25 + 25 * 25 * 25 + 25 * 25 * 25 * 25);        }    }}

posted on 2011-09-01 18:21  小交响曲  阅读(3524)  评论(46编辑  收藏  举报