Example #1
0
        public void initialDegreeGraph()
        {
            topoListCopy = new List <char>();
            topoList     = new List <char>();
            topoList.Clear();
            topoListCopy.Clear();

            for (int i = 0; i < size; i++)
            {
                degreeArray[i]   = -1;
                selfCircle[i]    = 0;
                wordChainList[i] = new WordChain();
            }

            foreach (Word w in ReadFile.wordList)
            {
                if (w.Get_head() == w.Get_tail()) //如果是自环
                {
                    selfCircle[w.Get_head() - 'a']++;
                }

                degreeGraph[(w.Get_head() - 'a'), w.Get_tail() - 'a'] = 1;
                degreeArray[(w.Get_head() - 'a')] = 0;  //这个地方应该有问题
                degreeArray[(w.Get_tail() - 'a')] = 0;
            }
        }
Example #2
0
        public static WordChain GetWordChainUnDo()
        {
            WordChain wchain = new WordChain();

            foreach (Word w in wordList)
            {
                wchain.addWord(w);
            }
            return(wchain);
        }
Example #3
0
        public void run(char wHead, char wTail)
        {
            WordChain.setWord2End(wTail);

            this.initialDegreeGraph();
            this.testHeadTail(wHead, wTail);
            this.setDegreeArray();
            this.topoloSort();
            this.buildChain(wHead);
            //this.getLongesChain();
        }
Example #4
0
        public void printChain(WordChain wchain)
        {
            while (wchain.getSize() != 0)
            {
                Word w = wchain.wordChain[0];
                wchain.wordChain.RemoveAt(0);
                fs.Write(w.Get_allWord() + " ");
            }

            fs.Close();
        }
Example #5
0
        public void buildChain(char headLetter) //当没有规定headLetter时,传入#
        {
            for (int i = 0; i < size; i++)      //将每个点到源点的距离标为负无穷
            {
                distance[i] = MINI;
            }
            if (headLetter == '#')
            {
                for (char x = 'a'; x <= 'z'; x++)
                {
                    buildChain(x);
                }
            }
            else
            {
                topoList = new List <char>(topoListCopy.ToArray());
                distance[headLetter - 'a'] = 0;
            }

            while (topoList.Count != 0) //当前拓扑序列非空(在指定起点的情况下,是否需要把之前的全清空)
            {
                //取出拓扑序列的第一个点
                char u = topoList[0];
                topoList.RemoveAt(0);

                //更新所有邻接点的距离
                if (distance[u - 'a'] != MINI)
                {
                    for (int i = ReadFile.indexOfAllLetter[u - 'a']; i < ReadFile.endOfAllLeter[u - 'a']; i++)
                    {
                        Word w = ReadFile.wordList[i];

                        if (distance[w.Get_tail() - 'a'] < (distance[w.Get_head() - 'a'] + w.getWeight()))
                        {
                            if (WordChain.buildEnd(w.Get_head(), w.Get_tail())) //到达终点
                            {
                                //在到达终点的时候记录当前最长链和最长路长

                                //将拓扑序列清空,停止循环
                                topoList.Clear();
                                break;
                            }

                            distance[w.Get_tail() - 'a'] = (distance[w.Get_head() - 'a'] + w.getWeight()); //更新距离

                            //更新链
                            wordChainList[w.Get_tail() - 'a'].copyChain(wordChainList[w.Get_head() - 'a']);
                            wordChainList[w.Get_tail() - 'a'].addWord(w);
                        }
                    }
                }
            }
        }
Example #6
0
        public static WordChain getLongesChain()
        {
            int       max    = 0;
            WordChain wchain = new WordChain();

            for (int i = 0; i < size; i++)
            {
                if (wordChainList[i].getWeight() > max && wordChainList[i].isChain())
                {
                    max    = wordChainList[i].getWeight();
                    wchain = wordChainList[i];
                }
            }
            return(wchain);
        }
Example #7
0
        private char c_t;                //t或#标识符

        public WordChainProcessor(WordChain words, char wc, char h, char t)
        {
            //初始化字段
            rawWordList = words.GetWordChain();
            maxWordList = new List <Word>();
            if (wc == 'w')
            {
                b_wc = true;
            }
            else
            {
                b_wc = false;
            }
            c_h = h;
            c_t = t;
        }
Example #8
0
 public void copyChain(WordChain wchain)
 {
     this.weight    = wchain.weight;
     this.wordChain = new List <Word>(wchain.GetWordChain().ToArray());
 }
Example #9
0
        static void Main(String[] args)
        {
            int argc = args.Length;

            string[] command = new string[5];

            command[1] = "#";
            command[2] = "#";

            bool judge = true;                                    //true时不报错

            if (args[argc - 2] == "-w" || args[argc - 2] == "-c") //检测
            {
                command[3] = args[argc - 2];
            }
            else
            {
                judge = false;
            }

            command[4] = args[argc - 1];

            if (args[0] == "-r")
            {
                command[0] = "a";
            }
            else
            {
                command[0] = "b";
            }

            for (int i = 0; i < argc - 1; i++)
            {
                if (args[i] == "-h")
                {
                    if (args[i + 1][0] <= 'z' && args[i + 1][0] >= 'a')
                    {
                        command[1] = args[i + 1];
                    }
                    else
                    {
                        judge = false;
                    }
                }
                if (args[i] == "-t")
                {
                    if (args[i + 1][0] <= 'z' && args[i + 1][0] >= 'a')
                    {
                        command[2] = args[i + 1];
                    }
                    else
                    {
                        judge = false;
                    }
                }
            }



            ReadFile rf = new ReadFile();

            if (command[0][0] == 'a')
            {
                ReadFile readFile = new ReadFile();
                rf.run(command[4]);
                Word.setWeightChosen(command[3][1]); //设置wc

                WordChainProcessor wcp    = new WordChainProcessor(ReadFile.GetWordChainUnDo(), command[3][1], command[1][0], command[2][0]);
                WordChain          wchain = wcp.getResultChain();
                WriteFile          wf     = new WriteFile();
                wf.printChain(wchain);
            }
            else
            {
                rf.run(command[4]);
                Word.setWeightChosen(command[3][1]);  //设置wc
                Topo tp = new Topo();
                tp.run(command[1][0], command[2][0]); //头尾和权重
                WriteFile wf = new WriteFile();
                wf.printChain(Topo.getLongesChain());
            }
        }