/** * Unit tests the {@code Interval2D} data type. * * @param args the command-line arguments */ public static void main(String[] args) { double xmin = Double.parseDouble(args[0]); double xmax = Double.parseDouble(args[1]); double ymin = Double.parseDouble(args[2]); double ymax = Double.parseDouble(args[3]); int trials = Integer.parseInt(args[4]); Interval1D xInterval = new Interval1D(xmin, xmax); Interval1D yInterval = new Interval1D(ymin, ymax); Interval2D box = new Interval2D(xInterval, yInterval); box.draw(); Counter counter = new Counter("hits"); for (int t = 0; t < trials; t++) { double x = StdRandom.uniform(0.0, 1.0); double y = StdRandom.uniform(0.0, 1.0); Point2D point = new Point2D(x, y); if (box.contains(point)) counter.increment(); else point.draw(); } StdOut.println(counter); StdOut.printf("box area = %.2f\n", box.area()); }
/**/ public static void main(string[] strarr) { BTree bTree = new BTree(); bTree.put("www.cs.princeton.edu", "128.112.136.11"); bTree.put("www.princeton.edu", "128.112.128.15"); bTree.put("www.yale.edu", "130.132.143.21"); bTree.put("www.simpsons.com", "209.052.165.60"); bTree.put("www.apple.com", "17.112.152.32"); bTree.put("www.amazon.com", "207.171.182.16"); bTree.put("www.ebay.com", "66.135.192.87"); bTree.put("www.cnn.com", "64.236.16.20"); bTree.put("www.google.com", "216.239.41.99"); bTree.put("www.nytimes.com", "199.239.136.200"); bTree.put("www.microsoft.com", "207.126.99.140"); bTree.put("www.dell.com", "143.166.224.230"); bTree.put("www.slashdot.org", "66.35.250.151"); bTree.put("www.espn.com", "199.181.135.201"); bTree.put("www.weather.com", "63.111.66.11"); bTree.put("www.yahoo.com", "216.109.118.65"); StdOut.println(new StringBuilder().append("cs.princeton.edu: ").append((string)bTree.get("www.cs.princeton.edu")).toString()); StdOut.println(new StringBuilder().append("hardvardsucks.com: ").append((string)bTree.get("www.harvardsucks.com")).toString()); StdOut.println(new StringBuilder().append("simpsons.com: ").append((string)bTree.get("www.simpsons.com")).toString()); StdOut.println(new StringBuilder().append("apple.com: ").append((string)bTree.get("www.apple.com")).toString()); StdOut.println(new StringBuilder().append("ebay.com: ").append((string)bTree.get("www.ebay.com")).toString()); StdOut.println(new StringBuilder().append("dell.com: ").append((string)bTree.get("www.dell.com")).toString()); StdOut.println(); StdOut.println(new StringBuilder().append("size: ").append(bTree.size()).toString()); StdOut.println(new StringBuilder().append("height: ").append(bTree.height()).toString()); StdOut.println(bTree); StdOut.println(); }
/** * Unit tests the {@code GraphGenerator} library. * * @param args the command-line arguments */ public static void main(String[] args) { int V = Integer.parseInt(args[0]); int E = Integer.parseInt(args[1]); int V1 = V/2; int V2 = V - V1; StdOut.println("complete graph"); StdOut.println(complete(V)); StdOut.println(); StdOut.println("simple"); StdOut.println(simple(V, E)); StdOut.println(); StdOut.println("Erdos-Renyi"); double p = (double) E / (V*(V-1)/2.0); StdOut.println(simple(V, p)); StdOut.println(); StdOut.println("complete bipartite"); StdOut.println(completeBipartite(V1, V2)); StdOut.println(); StdOut.println("bipartite"); StdOut.println(bipartite(V1, V2, E)); StdOut.println(); StdOut.println("Erdos Renyi bipartite"); double q = (double) E / (V1*V2); StdOut.println(bipartite(V1, V2, q)); StdOut.println(); StdOut.println("path"); StdOut.println(path(V)); StdOut.println(); StdOut.println("cycle"); StdOut.println(cycle(V)); StdOut.println(); StdOut.println("binary tree"); StdOut.println(binaryTree(V)); StdOut.println(); StdOut.println("tree"); StdOut.println(tree(V)); StdOut.println(); StdOut.println("4-regular"); StdOut.println(regular(V, 4)); StdOut.println(); StdOut.println("star"); StdOut.println(star(V)); StdOut.println(); StdOut.println("wheel"); StdOut.println(wheel(V)); StdOut.println(); }
/**/ public static void main(string[] strarr) { In @in = new In(strarr[0]); In in2 = new In(strarr[1]); string text = java.lang.String.instancehelper_replaceAll(java.lang.String.instancehelper_trim(@in.readAll()), "\\s+", " "); string text2 = java.lang.String.instancehelper_replaceAll(java.lang.String.instancehelper_trim(in2.readAll()), "\\s+", " "); int num = java.lang.String.instancehelper_length(text); java.lang.String.instancehelper_length(text2); string text3 = new StringBuilder().append(text).append('\u0001').append(text2).toString(); int num2 = java.lang.String.instancehelper_length(text3); SuffixArray suffixArray = new SuffixArray(text3); string text4 = ""; for (int i = 1; i < num2; i++) { if (suffixArray.index(i) >= num || suffixArray.index(i - 1) >= num) { if (suffixArray.index(i) <= num || suffixArray.index(i - 1) <= num) { int num3 = suffixArray.lcp(i); if (num3 > java.lang.String.instancehelper_length(text4)) { text4 = java.lang.String.instancehelper_substring(text3, suffixArray.index(i), suffixArray.index(i) + num3); } } } } StdOut.println(java.lang.String.instancehelper_length(text4)); StdOut.println(new StringBuilder().append("'").append(text4).append("'").toString()); }
/** * Unit tests the {@code BellmanFordSP} data type. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); int s = Integer.parseInt(args[1]); EdgeWeightedDigraph G = new EdgeWeightedDigraph(in); BellmanFordSP sp = new BellmanFordSP(G, s); // print negative cycle if (sp.hasNegativeCycle()) { for (DirectedEdge e : sp.negativeCycle()) StdOut.println(e); } // print shortest paths else { for (int v = 0; v < G.V(); v++) { if (sp.hasPathTo(v)) { StdOut.printf("%d to %d (%5.2f) ", s, v, sp.distTo(v)); for (DirectedEdge e : sp.pathTo(v)) { StdOut.print(e + " "); } StdOut.println(); } else { StdOut.printf("%d to %d no path\n", s, v); } } } }
/**/ public static void main(string[] strarr) { string text = strarr[0]; string text2 = strarr[1]; char[] charr = java.lang.String.instancehelper_toCharArray(text); char[] charr2 = java.lang.String.instancehelper_toCharArray(text2); BoyerMoore boyerMoore = new BoyerMoore(text); BoyerMoore boyerMoore2 = new BoyerMoore(charr, 256); int num = boyerMoore.search(text2); int num2 = boyerMoore2.search(charr2); StdOut.println(new StringBuilder().append("text: ").append(text2).toString()); StdOut.print("pattern: "); for (int i = 0; i < num; i++) { StdOut.print(" "); } StdOut.println(text); StdOut.print("pattern: "); for (int i = 0; i < num2; i++) { StdOut.print(" "); } StdOut.println(text); }
/**/ public static void main(string[] strarr) { Alphabet.__ <clinit>(); Alphabet alphabet = new Alphabet(strarr[0]); int num = alphabet.R(); int[] array = new int[num]; string @this = StdIn.readAll(); int num2 = java.lang.String.instancehelper_length(@this); for (int i = 0; i < num2; i++) { if (alphabet.contains(java.lang.String.instancehelper_charAt(@this, i))) { int[] arg_54_0 = array; int num3 = alphabet.toIndex(java.lang.String.instancehelper_charAt(@this, i)); int[] array2 = arg_54_0; array2[num3]++; } } for (int i = 0; i < num; i++) { StdOut.println(new StringBuilder().append(alphabet.toChar(i)).append(" ").append(array[i]).toString()); } }
/** * Reads in a social network from a file, and then repeatedly reads in * individuals from standard input and prints out their degrees of * separation. * Takes three command-line arguments: the name of a file, * a delimiter, and the name of the distinguished individual. * Each line in the file contains the name of a vertex, followed by a * list of the names of the vertices adjacent to that vertex, * separated by the delimiter. * * @param args the command-line arguments */ public static void main(String[] args) { String filename = args[0]; String delimiter = args[1]; String source = args[2]; // StdOut.println("Source: " + source); SymbolGraph sg = new SymbolGraph(filename, delimiter); Graph G = sg.graph(); if (!sg.contains(source)) { StdOut.println(source + " not in database."); return; } int s = sg.indexOf(source); BreadthFirstPaths bfs = new BreadthFirstPaths(G, s); while (!StdIn.isEmpty()) { String sink = StdIn.readLine(); if (sg.contains(sink)) { int t = sg.indexOf(sink); if (bfs.hasPathTo(t)) { for (int v : bfs.pathTo(t)) { StdOut.println(" " + sg.nameOf(v)); } } else { StdOut.println("Not connected"); } } else { StdOut.println(" Not in database."); } } }
public static void main(String[] args) { // key = word, value = set of files containing that word ST<String, SET<File>> st = new ST<String, SET<File>>(); // create inverted index of all files StdOut.println("Indexing files"); for (String filename : args) { StdOut.println(" " + filename); File file = new File(filename); In in = new In(file); while (!in.isEmpty()) { String word = in.readString(); if (!st.contains(word)) st.put(word, new SET<File>()); SET<File> set = st.get(word); set.add(file); } } // read queries from standard input, one per line while (!StdIn.isEmpty()) { String query = StdIn.readString(); if (st.contains(query)) { SET<File> set = st.get(query); for (File file : set) { StdOut.println(" " + file.getName()); } } } }
/** * Unit tests the polynomial data type. * * @param args the command-line arguments (none) */ public static void main(String[] args) { Polynomial zero = new Polynomial(0, 0); Polynomial p1 = new Polynomial(4, 3); Polynomial p2 = new Polynomial(3, 2); Polynomial p3 = new Polynomial(1, 0); Polynomial p4 = new Polynomial(2, 1); Polynomial p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1 Polynomial q1 = new Polynomial(3, 2); Polynomial q2 = new Polynomial(5, 0); Polynomial q = q1.plus(q2); // 3x^2 + 5 Polynomial r = p.plus(q); Polynomial s = p.times(q); Polynomial t = p.compose(q); Polynomial u = p.minus(p); StdOut.println("zero(x) = " + zero); StdOut.println("p(x) = " + p); StdOut.println("q(x) = " + q); StdOut.println("p(x) + q(x) = " + r); StdOut.println("p(x) * q(x) = " + s); StdOut.println("p(q(x)) = " + t); StdOut.println("p(x) - p(x) = " + u); StdOut.println("0 - p(x) = " + zero.minus(p)); StdOut.println("p(3) = " + p.evaluate(3)); StdOut.println("p'(x) = " + p.differentiate()); StdOut.println("p''(x) = " + p.differentiate().differentiate()); }
/**/ public static void main(string[] strarr) { double[] darr = new double[] { (double)1f, 2.0, 3.0, 4.0 }; double[] darr2 = new double[] { 5.0, 2.0, 4.0, (double)1f }; Vector vector = new Vector(darr); Vector vector2 = new Vector(darr2); StdOut.println(new StringBuilder().append(" x = ").append(vector).toString()); StdOut.println(new StringBuilder().append(" y = ").append(vector2).toString()); Vector vector3 = vector.plus(vector2); StdOut.println(new StringBuilder().append(" z = ").append(vector3).toString()); vector3 = vector3.times(10.0); StdOut.println(new StringBuilder().append(" 10z = ").append(vector3).toString()); StdOut.println(new StringBuilder().append(" |x| = ").append(vector.magnitude()).toString()); StdOut.println(new StringBuilder().append(" <x, y> = ").append(vector.dot(vector2)).toString()); StdOut.println(new StringBuilder().append("dist(x, y) = ").append(vector.distanceTo(vector2)).toString()); StdOut.println(new StringBuilder().append("dir(x) = ").append(vector.direction()).toString()); }
/** * Reads the currency exchange table from standard input and * prints an arbitrage opportunity to standard output (if one exists). * * @param args the command-line arguments */ public static void main(String[] args) { // V currencies int V = StdIn.readInt(); String[] name = new String[V]; // create complete network EdgeWeightedDigraph G = new EdgeWeightedDigraph(V); for (int v = 0; v < V; v++) { name[v] = StdIn.readString(); for (int w = 0; w < V; w++) { double rate = StdIn.readDouble(); DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate)); G.addEdge(e); } } // find negative cycle BellmanFordSP spt = new BellmanFordSP(G, 0); if (spt.hasNegativeCycle()) { double stake = 1000.0; for (DirectedEdge e : spt.negativeCycle()) { StdOut.printf("%10.5f %s ", stake, name[e.from()]); stake *= Math.exp(-e.weight()); StdOut.printf("= %10.5f %s\n", stake, name[e.to()]); } } else { StdOut.println("No arbitrage opportunity"); } }
/** * Unit tests the {@code lcs()} method. * Reads in two strings from files specified as command-line arguments; * computes the longest common substring; and prints the results to * standard output. * * @param args the command-line arguments */ public static void main(String[] args) { In in1 = new In(args[0]); In in2 = new In(args[1]); String s = in1.readAll().trim().replaceAll("\\s+", " "); String t = in2.readAll().trim().replaceAll("\\s+", " "); StdOut.println("'" + lcs(s, t) + "'"); }
private bool check(Digraph digraph) { int num = 0; Iterator iterator = this.post().iterator(); while (iterator.hasNext()) { int i = ((Integer)iterator.next()).intValue(); if (this.post(i) != num) { StdOut.println("post(v) and post() inconsistent"); return(false); } num++; } num = 0; iterator = this.pre().iterator(); while (iterator.hasNext()) { int i = ((Integer)iterator.next()).intValue(); if (this.pre(i) != num) { StdOut.println("pre(v) and pre() inconsistent"); return(false); } num++; } return(true); }
/**/ public static void main(string[] strarr) { In @in = new In(strarr[0]); int num = Integer.parseInt(strarr[1]); string text = java.lang.String.instancehelper_replaceAll(@in.readAll(), "\\s+", " "); int num2 = java.lang.String.instancehelper_length(text); SuffixArray suffixArray = new SuffixArray(text); while (StdIn.hasNextLine()) { string text2 = StdIn.readLine(); for (int i = suffixArray.rank(text2); i < num2; i++) { int num3 = suffixArray.index(i); int endIndex = java.lang.Math.min(num2, num3 + java.lang.String.instancehelper_length(text2)); if (!java.lang.String.instancehelper_equals(text2, java.lang.String.instancehelper_substring(text, num3, endIndex))) { break; } int beginIndex = java.lang.Math.max(0, suffixArray.index(i) - num); int endIndex2 = java.lang.Math.min(num2, suffixArray.index(i) + num + java.lang.String.instancehelper_length(text2)); StdOut.println(java.lang.String.instancehelper_substring(text, beginIndex, endIndex2)); } StdOut.println(); } }
/** * Reads in a command-line integer and sequence of words from * standard input and prints out a word (whose length exceeds * the threshold) that occurs most frequently to standard output. * It also prints out the number of words whose length exceeds * the threshold and the number of distinct such words. * * @param args the command-line arguments */ public static void main(String[] args) { int distinct = 0, words = 0; int minlen = Integer.parseInt(args[0]); ST<String, Integer> st = new ST<String, Integer>(); // compute frequency counts while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (key.length() < minlen) continue; words++; if (st.contains(key)) { st.put(key, st.get(key) + 1); } else { st.put(key, 1); distinct++; } } // find a key with the highest frequency count String max = ""; st.put(max, 0); for (String word : st.keys()) { if (st.get(word) > st.get(max)) max = word; } StdOut.println(max + " " + st.get(max)); StdOut.println("distinct = " + distinct); StdOut.println("words = " + words); }
/**/ public static void main(string[] strarr) { In i = new In(strarr[0]); EdgeWeightedDigraph obj = new EdgeWeightedDigraph(i); StdOut.println(obj); }
/** * Unit tests the {@code FordFulkerson} data type. * * @param args the command-line arguments */ public static void main(String[] args) { // create flow network with V vertices and E edges int V = Integer.parseInt(args[0]); int E = Integer.parseInt(args[1]); int s = 0, t = V-1; FlowNetwork G = new FlowNetwork(V, E); StdOut.println(G); // compute maximum flow and minimum cut FordFulkerson maxflow = new FordFulkerson(G, s, t); StdOut.println("Max flow from " + s + " to " + t); for (int v = 0; v < G.V(); v++) { for (FlowEdge e : G.adj(v)) { if ((v == e.from()) && e.flow() > 0) StdOut.println(" " + e); } } // print min-cut StdOut.print("Min cut: "); for (int v = 0; v < G.V(); v++) { if (maxflow.inCut(v)) StdOut.print(v + " "); } StdOut.println(); StdOut.println("Max flow value = " + maxflow.value()); }
/** * Unit tests the {@code SuffixArrayx} data type. * * @param args the command-line arguments */ public static void main(String[] args) { String s = StdIn.readAll().replaceAll("\n", " ").trim(); SuffixArrayX suffix1 = new SuffixArrayX(s); SuffixArray suffix2 = new SuffixArray(s); boolean check = true; for (int i = 0; check && i < s.length(); i++) { if (suffix1.index(i) != suffix2.index(i)) { StdOut.println("suffix1(" + i + ") = " + suffix1.index(i)); StdOut.println("suffix2(" + i + ") = " + suffix2.index(i)); String ith = "\"" + s.substring(suffix1.index(i), Math.min(suffix1.index(i) + 50, s.length())) + "\""; String jth = "\"" + s.substring(suffix2.index(i), Math.min(suffix2.index(i) + 50, s.length())) + "\""; StdOut.println(ith); StdOut.println(jth); check = false; } } StdOut.println(" i ind lcp rnk select"); StdOut.println("---------------------------"); for (int i = 0; i < s.length(); i++) { int index = suffix2.index(i); String ith = "\"" + s.substring(index, Math.min(index + 50, s.length())) + "\""; int rank = suffix2.rank(s.substring(index)); assert s.substring(index).equals(suffix2.select(i)); if (i == 0) { StdOut.printf("%3d %3d %3s %3d %s\n", i, index, "-", rank, ith); } else { // int lcp = suffix.lcp(suffix2.index(i), suffix2.index(i-1)); int lcp = suffix2.lcp(i); StdOut.printf("%3d %3d %3d %3d %s\n", i, index, lcp, rank, ith); } } }
/** * Reads in two command-line arguments lo and hi and prints n uniformly * random real numbers in [lo, hi) to standard output. * * @param args the command-line arguments */ public static void main(String[] args) { // command-line arguments int n = Integer.parseInt(args[0]); // for backward compatibility with Intro to Programming in Java version of RandomSeq if (args.length == 1) { // generate and print n numbers between 0.0 and 1.0 for (int i = 0; i < n; i++) { double x = StdRandom.uniform(); StdOut.println(x); } } else if (args.length == 3) { double lo = Double.parseDouble(args[1]); double hi = Double.parseDouble(args[2]); // generate and print n numbers between lo and hi for (int i = 0; i < n; i++) { double x = StdRandom.uniform(lo, hi); StdOut.printf("%.2f\n", x); } } else { throw new IllegalArgumentException("Invalid number of arguments"); } }
/** * Unit tests the {@code TrieST} data type. * * @param args the command-line arguments */ public static void main(String[] args) { // build symbol table from standard input TrieST<Integer> st = new TrieST<Integer>(); for (int i = 0; !StdIn.isEmpty(); i++) { String key = StdIn.readString(); st.put(key, i); } // print results if (st.size() < 100) { StdOut.println("keys(\"\"):"); for (String key : st.keys()) { StdOut.println(key + " " + st.get(key)); } StdOut.println(); } StdOut.println("longestPrefixOf(\"shellsort\"):"); StdOut.println(st.longestPrefixOf("shellsort")); StdOut.println(); StdOut.println("longestPrefixOf(\"quicksort\"):"); StdOut.println(st.longestPrefixOf("quicksort")); StdOut.println(); StdOut.println("keysWithPrefix(\"shor\"):"); for (String s : st.keysWithPrefix("shor")) StdOut.println(s); StdOut.println(); StdOut.println("keysThatMatch(\".he.l.\"):"); for (String s : st.keysThatMatch(".he.l.")) StdOut.println(s); }
/**/ public static void main(string[] strarr) { In i = new In(strarr[0]); int i2 = Integer.parseInt(strarr[1]); EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(i); AcyclicSP acyclicSP = new AcyclicSP(edgeWeightedDigraph, i2); for (int j = 0; j < edgeWeightedDigraph.V(); j++) { if (acyclicSP.hasPathTo(j)) { StdOut.printf("%d to %d (%.2f) ", new object[] { Integer.valueOf(i2), Integer.valueOf(j), java.lang.Double.valueOf(acyclicSP.distTo(j)) }); Iterator iterator = acyclicSP.pathTo(j).iterator(); while (iterator.hasNext()) { DirectedEdge obj = (DirectedEdge)iterator.next(); StdOut.print(new StringBuilder().append(obj).append(" ").toString()); } StdOut.println(); } else { StdOut.printf("%d to %d no path\n", new object[] { Integer.valueOf(i2), Integer.valueOf(j) }); } } }
/** * Unit tests the {@code GabowSCC} data type. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); GabowSCC scc = new GabowSCC(G); // number of connected components int m = scc.count(); StdOut.println(m + " components"); // compute list of vertices in each strong component Queue<Integer>[] components = (Queue<Integer>[]) new Queue[m]; for (int i = 0; i < m; i++) { components[i] = new Queue<Integer>(); } for (int v = 0; v < G.V(); v++) { components[scc.id(v)].enqueue(v); } // print results for (int i = 0; i < m; i++) { for (int v : components[i]) { StdOut.print(v + " "); } StdOut.println(); } }
/** * Reads in a sequence of extended ASCII strings from standard input; * MSD radix sorts them; * and prints them to standard output in ascending order. * * @param args the command-line arguments */ public static void main(String[] args) { String[] a = StdIn.readAllStrings(); int n = a.length; sort(a); for (int i = 0; i < n; i++) StdOut.println(a[i]); }
/** * Unit tests the {@code FloydWarshall} data type. * * @param args the command-line arguments */ public static void main(String[] args) { // random graph with V vertices and E edges, parallel edges allowed int V = Integer.parseInt(args[0]); int E = Integer.parseInt(args[1]); AdjMatrixEdgeWeightedDigraph G = new AdjMatrixEdgeWeightedDigraph(V); for (int i = 0; i < E; i++) { int v = StdRandom.uniform(V); int w = StdRandom.uniform(V); double weight = Math.round(100 * (StdRandom.uniform() - 0.15)) / 100.0; if (v == w) G.addEdge(new DirectedEdge(v, w, Math.abs(weight))); else G.addEdge(new DirectedEdge(v, w, weight)); } StdOut.println(G); // run Floyd-Warshall algorithm FloydWarshall spt = new FloydWarshall(G); // print all-pairs shortest path distances StdOut.printf(" "); for (int v = 0; v < G.V(); v++) { StdOut.printf("%6d ", v); } StdOut.println(); for (int v = 0; v < G.V(); v++) { StdOut.printf("%3d: ", v); for (int w = 0; w < G.V(); w++) { if (spt.hasPath(v, w)) StdOut.printf("%6.2f ", spt.dist(v, w)); else StdOut.printf(" Inf "); } StdOut.println(); } // print negative cycle if (spt.hasNegativeCycle()) { StdOut.println("Negative cost cycle:"); for (DirectedEdge e : spt.negativeCycle()) StdOut.println(e); StdOut.println(); } // print all-pairs shortest paths else { for (int v = 0; v < G.V(); v++) { for (int w = 0; w < G.V(); w++) { if (spt.hasPath(v, w)) { StdOut.printf("%d to %d (%5.2f) ", v, w, spt.dist(v, w)); for (DirectedEdge e : spt.path(v, w)) StdOut.print(e + " "); StdOut.println(); } else { StdOut.printf("%d to %d no path\n", v, w); } } } } }
/** * Takes a pattern string and an input string as command-line arguments; * searches for the pattern string in the text string; and prints * the first occurrence of the pattern string in the text string. * * @param args the command-line arguments */ public static void main(String[] args) { String pat = args[0]; String txt = args[1]; char[] pattern = pat.toCharArray(); char[] text = txt.toCharArray(); KMP kmp1 = new KMP(pat); int offset1 = kmp1.search(txt); KMP kmp2 = new KMP(pattern, 256); int offset2 = kmp2.search(text); // print results StdOut.println("text: " + txt); StdOut.print("pattern: "); for (int i = 0; i < offset1; i++) StdOut.print(" "); StdOut.println(pat); StdOut.print("pattern: "); for (int i = 0; i < offset2; i++) StdOut.print(" "); StdOut.println(pat); }
/** * Unit tests the {@code DepthFirstOrder} data type. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); DepthFirstOrder dfs = new DepthFirstOrder(G); StdOut.println(" v pre post"); StdOut.println("--------------"); for (int v = 0; v < G.V(); v++) { StdOut.printf("%4d %4d %4d\n", v, dfs.pre(v), dfs.post(v)); } StdOut.print("Preorder: "); for (int v : dfs.pre()) { StdOut.print(v + " "); } StdOut.println(); StdOut.print("Postorder: "); for (int v : dfs.post()) { StdOut.print(v + " "); } StdOut.println(); StdOut.print("Reverse postorder: "); for (int v : dfs.reversePost()) { StdOut.print(v + " "); } StdOut.println(); }
/** * Reads a string from a file specified as the first * command-line argument; read an integer k specified as the * second command line argument; then repeatedly processes * use queries, printing all occurrences of the given query * string in the text string with k characters of surrounding * context on either side. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); int context = Integer.parseInt(args[1]); // read in text String text = in.readAll().replaceAll("\\s+", " "); int n = text.length(); // build suffix array SuffixArray sa = new SuffixArray(text); // find all occurrences of queries and give context while (StdIn.hasNextLine()) { String query = StdIn.readLine(); for (int i = sa.rank(query); i < n; i++) { int from1 = sa.index(i); int to1 = Math.min(n, from1 + query.length()); if (!query.equals(text.substring(from1, to1))) break; int from2 = Math.max(0, sa.index(i) - context); int to2 = Math.min(n, sa.index(i) + context + query.length()); StdOut.println(text.substring(from2, to2)); } StdOut.println(); } }
public static void main(String[] args) { // create random DAG with V vertices and E edges; then add F random edges int V = Integer.parseInt(args[0]); int E = Integer.parseInt(args[1]); int F = Integer.parseInt(args[2]); Digraph G = DigraphGenerator.dag(V, E); // add F extra edges for (int i = 0; i < F; i++) { int v = StdRandom.uniform(V); int w = StdRandom.uniform(V); G.addEdge(v, w); } StdOut.println(G); DirectedCycleX finder = new DirectedCycleX(G); if (finder.hasCycle()) { StdOut.print("Directed cycle: "); for (int v : finder.cycle()) { StdOut.print(v + " "); } StdOut.println(); } else { StdOut.println("No directed cycle"); } StdOut.println(); }
/**/ public static void main(string[] strarr) { In i = new In(strarr[0]); Digraph digraph = new Digraph(i); KosarajuSharirSCC kosarajuSharirSCC = new KosarajuSharirSCC(digraph); int num = kosarajuSharirSCC.count(); StdOut.println(new StringBuilder().append(num).append(" components").toString()); Queue[] array = (Queue[])new Queue[num]; for (int j = 0; j < num; j++) { array[j] = new Queue(); } for (int j = 0; j < digraph.V(); j++) { array[kosarajuSharirSCC.id(j)].enqueue(Integer.valueOf(j)); } for (int j = 0; j < num; j++) { Iterator iterator = array[j].iterator(); while (iterator.hasNext()) { int i2 = ((Integer)iterator.next()).intValue(); StdOut.print(new StringBuilder().append(i2).append(" ").toString()); } StdOut.println(); } }