void FrequencyCounter() { ISearchTable <string, int> searchTable = null; var file = File.OpenRead("tale.txt"); Scanner scanner = new Scanner(new StreamReader(file)); Dictionary <string, int> dic = new Dictionary <string, int>(); StopWatch stopWatch = new StopWatch(); while (scanner.HasNext()) { var word = scanner.Read(); //if (word.Length <= 4) // continue; if (!dic.ContainsKey(word)) { dic.Add(word, 1); } else { dic[word]++; } } Console.WriteLine(stopWatch.ElapsedTime); var list = dic.Where((keyPair) => keyPair.Key.Length > 3).ToList(); list.Sort((v1, v2) => v2.Value.CompareTo(v1.Value)); Console.WriteLine(stopWatch.ElapsedTime); int i = 0; while (i++ < 10) { StdOut.Println(list[i]); } }
private static void ShortestPath() { string filename = "mediumEWD.txt"; Scanner scanner = new Scanner(new StreamReader(File.OpenRead(filename))); IEdgeWeightedDIgraph G = new EdgeWeightedDigraph(scanner); StdOut.Print("输入一个边:"); int s = StdIn.ReadInt(); IShortestPath sp = new DijkstraSP(G, s); for (int t = 0; t < G.V; t++) { StdOut.Print(s + " to " + t); StdOut.Printf(" ({0}): ", sp.DistTo(t)); if (sp.HasPathTo(t)) { foreach (var e in sp.PathTo(t)) { StdOut.Print(e + " "); } } StdOut.Println(); } DijkstraAllPairsSP allPairsSP = new DijkstraAllPairsSP(G); StdOut.Println(); StdOut.Println(allPairsSP.Dist(1, 28)); //string filename2 = "tinyEWDAG.txt"; //scanner = new Scanner(new StreamReader(File.OpenRead(filename2))); //G = new EdgeWeightedDigraph(scanner); //IShortestPath sp2 = new AcyclicSP(G, s); //for (int t = 0; t < G.V; t++) //{ // StdOut.Print(s + " to " + t); // StdOut.Printf(" ({0}): ", sp2.DistTo(t)); // if (sp2.HasPathTo(t)) // foreach (var e in sp.PathTo(t)) // { // StdOut.Print(e + " "); // } // StdOut.Println(); //} //filename = "tinyEWDnc.txt"; //scanner = new Scanner(new StreamReader(File.OpenRead(filename))); //G = new EdgeWeightedDigraph(scanner); //sp = new BellmanFordSP(G, s); //for (int t = 0; t < G.V; t++) //{ // StdOut.Print(s + " to " + t); // StdOut.Printf(" ({0}): ", sp.DistTo(t)); // if (sp.HasPathTo(t)) // foreach (var e in sp.PathTo(t)) // { // StdOut.Print(e + " "); // } // StdOut.Println(); //} }
private static void PrintArray(string[] strings) { foreach (var s in strings) { StdOut.Println(s); } }
private static void DiGraph() { IDiGraph G = new DiGraph.DiGraph(new Scanner(new StreamReader(File.OpenRead("tinyDG.txt")))); Bag <int> sources = new Bag <int>(); StdOut.Println("搜索几个结点:"); int i = StdIn.ReadInt(); while (i-- > 0) { sources.Add(StdIn.ReadInt()); } DirectedDFS reachable = new DirectedDFS(G, sources); for (int v = 0; v < G.V; v++) { if (reachable.Marked(v)) { StdOut.Print(v + " "); } } //string filename = "jobs.txt"; //文件有问题 //string seperator = "/"; //SymbolDiGraph sg = new SymbolDiGraph(filename, seperator); //Topological topological = new Topological(sg.G); //foreach (int v in topological.Order) // StdOut.Println(sg.Name(v)); StdOut.Println(); StdOut.Println("强连通分量的数量"); ISCC scc = new KosarajuSCC(G); StdOut.Println(scc.Count); }
public static void Show <T>(T[] a) where T : IComparable <T> { foreach (var item in a) { StdOut.Print(item + " "); } StdOut.Println(); }
static void Main(string[] args) { Stack <string> stack = new Stack <string>(); Queue <string> q = new Queue <string>(); Bag <string> b = new Bag <string>(); Deque <string> deque = new Deque <string>(); while (!StdIn.IsEmpty()) { string item = StdIn.ReadString(); b.Add(item); stack.Push(item); deque.PushLeft(item); if (!item.Equals("-")) { q.Enqueue(item); } //else if (!q.IsEmpty) //StdOut.Print(q.Dequeue() + " "); } stack.Reverse(); Queue <string> q2 = new Queue <string>(q); q.Reverse(); while (!q2.IsEmpty) { StdOut.Print(q2.Dequeue() + " "); } while (!q.IsEmpty) { StdOut.Print(q.Dequeue() + " "); } Stack <string> stack2 = new Stack <string>(stack); while (!stack.IsEmpty) { StdOut.Print(stack.Pop() + " "); } while (!stack2.IsEmpty) { StdOut.Print(stack2.Pop() + " "); } StdOut.Println(); while (!deque.IsEmpty) { StdOut.Print(deque.PopRight() + " "); } StdOut.Println(); foreach (var item in b) { StdOut.Print(item + " "); } }
private static void ShowMSTResult(IEdgeWeightGraph G, IMST mst) { //StdOut.Println(G.ToString()); foreach (var edge in mst.Edges()) { StdOut.Println(edge); } StdOut.Println(mst.Weight); }
private static void Q3String() { var fileName = "3String.txt"; var strings = ReadFile(fileName); PrintArray(strings); StdOut.Println("After sorting..."); Quick3String.Sort(strings); PrintArray(strings); }
private static void MSDSort() { var fileName = "MSD.txt"; var strings = ReadFile(fileName); PrintArray(strings); StdOut.Println("After sorting....."); MSD.Sort(strings); PrintArray(strings); }
private static void LSDSort() { var fileName = "LSD.txt"; var strings = ReadFile(fileName); PrintArray(strings); StdOut.Println("After sorting....."); LSD.Sort(ref strings, strings[0].Length); PrintArray(strings); }
static void Main(string[] args) { //StdOut.Println(SearchTableExamples.TimeInput(SearchTableOptions.SequantialST)); //StdOut.Println(SearchTableExamples.TimeInput(SearchTableOptions.BinarySearchST)); StdOut.Println(SearchTableExamples.TimeInput(SearchTableOptions.BST, true)); StdOut.Println(SearchTableExamples.TimeInput(SearchTableOptions.RedBlack, true)); StdOut.Println(SearchTableExamples.TimeInputForHashST(HashTableOptions.LinearProbiningHash)); StdOut.Println(SearchTableExamples.TimeInputForHashST(HashTableOptions.SeparateChainingHash)); }
private static void DoSearch(IStringSearch stringSearch, string pat, string text) { StdOut.Println("text: " + text); int offset = stringSearch.Search(text); StdOut.Print("pattern: "); for (int i = 0; i < offset; i++) { StdOut.Print(" "); } StdOut.Println(pat); }
private static void TestRank() { int[] ints = StdIn.ReadAllInts(); Array.Sort(ints); while (!StdIn.IsEmpty()) { int key = StdIn.ReadInt(); if (Rank(key, ints) < 0) { StdOut.Println(key); //如果不在名单里 则打印 } } }
private static void Regax() { var scanner = new Scanner(new StreamReader(File.OpenRead("tinyL.txt"))); string pattern = "(A*B|AC)D"; NFARegex regex = new NFARegex(pattern); while (scanner.HasNextLine()) { string txt = scanner.ReadLine(); if (regex.Recognizes(txt)) { StdOut.Println(txt); } } }
private static void MST() { int i = 0; StdOut.Println("输入选择:0,1"); i = StdIn.ReadInt(); string filename = i switch { 0 => "tinyEWG.txt", 1 => "mediumEWG.txt", _ => throw new Exception("no file name is assigned"), }; IEdgeWeightGraph G = new EdgeWeightGraph(new Scanner(new StreamReader(File.OpenRead(filename)))); IMST mst = new LazyPrimMST(G); IMST mst2 = new PrimeMST(G); IMST mst3 = new KruskalMST(G); ShowMSTResult(G, mst); ShowMSTResult(G, mst2); ShowMSTResult(G, mst3); }
static void Main(string[] args) { //LSDSort(); //MSDSort(); //Q3String(); //StringSearch(); //Regax(); for (; !BinaryStdIn.IsEmpty();) { if (BinaryStdIn.ReadBoolean()) { StdOut.Print("1"); } else { StdOut.Print("0"); } } StdOut.Println(); }
private static void Graph() { Graph graph = new Graph(new Scanner(new StreamReader(File.OpenRead("tinyG.txt")))); Graph graph2 = new Graph(new Scanner(new StreamReader(File.OpenRead("tinyCG.txt")))); StdOut.Println("输入一个数字: "); int s = StdIn.ReadInt(); ISearch search = new DepthFirstSearch(graph, s); for (int v = 0; v < graph.V; v++) { if (search.Marked(v)) { StdOut.Print(v + " "); } } StdOut.Println(); StdOut.Println(); StdOut.Println(); ICC cc = new DepthFirstSearch(graph, s).InitCC(); int M = cc.CountCC(); StdOut.Println(M + " components"); Bag <int>[] components = new Bag <int> [M]; for (int i = 0; i < M; i++) { components[i] = new Bag <int>(); } for (int v = 0; v < graph.V; v++) { components[cc.Id(v)].Add(v); } for (int i = 0; i < M; i++) { foreach (int v in components[i]) { StdOut.Print(v + " "); } StdOut.Println(); } StdOut.Println(cc.HasCycle); if (search.Count() != graph.V) { StdOut.Print("Not "); } StdOut.Println("connected"); IPath path = new DepthFirstSearch(graph2, s); for (int v = 0; v < graph2.V; v++) { StdOut.Print(s + " to " + v + ": "); if (path.HasPathTo(v)) { foreach (int x in path.PathTo(v)) { if (x == s) { StdOut.Print(x); } else { StdOut.Print("-" + x); } } } StdOut.Println(); } //符号图 string filename = "routes.txt"; string filename2 = "movies.txt"; string delim = "/"; ISymbolGraph sg = new SymbolGraph(filename2, delim); IGraph g = sg.G; while (StdIn.HasNextLine()) { StdOut.Println("输入查找的字符串"); string source = StdIn.ReadLine(); foreach (int w in g.Adj(sg.Index(source))) { StdOut.Println(" " + sg.Name(w)); } } }