use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.
the class BreadthFirstPaths method main.
public static void main(String[] args) {
Graph G = new Graph(new In(args[0]));
int s = Integer.parseInt(args[1]);
BreadthFirstPaths search = new BreadthFirstPaths(G, s);
for (int v = 0; v < G.V(); v++) {
StdOut.print(s + " to " + v + ": ");
if (search.hasPathTo(v)) {
for (int x : search.pathTo(v)) {
if (x == s) {
StdOut.print(x);
} else {
StdOut.print("-" + x);
}
}
}
StdOut.println();
}
}
use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.
the class Ex39 method main.
public static void main(String[] args) {
EdgeWeightedDigraph G = new EdgeWeightedDigraph(new In(args[0]));
int s = Integer.parseInt(args[1]);
Ex39 sp = new Ex39(G, s);
for (int t = 0; t < G.V(); t++) {
StdOut.print(s + " to " + t);
StdOut.printf(" (%4.2f): ", sp.distTo(t));
if (sp.hasPathTo(t)) {
for (DirectedEdge e : sp.pathTo(t)) {
StdOut.print(e + " ");
}
}
StdOut.println();
}
}
use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.
the class LookupCSV method main.
public static void main(String[] args) {
In in = new In(args[0]);
int keyField = Integer.parseInt(args[1]);
int valField = Integer.parseInt(args[2]);
ST<String, String> st = new ST<String, String>();
while (in.hasNextLine()) {
String line = in.readLine();
String[] tokens = line.split(",");
String key = tokens[keyField];
String val = tokens[valField];
st.put(key, val);
}
while (!StdIn.isEmpty()) {
String query = StdIn.readString();
if (st.contains(query)) {
StdOut.println(st.get(query));
}
}
}
use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.
the class Concordance method main.
public static void main(String[] args) {
ST<String, SET<Integer>> st = new ST<>();
In in = new In(args[0]);
String[] words = in.readAllStrings();
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (!st.contains(word)) {
st.put(word, new SET<Integer>());
}
st.get(word).add(i);
}
while (!StdIn.isEmpty()) {
String query = StdIn.readString();
if (st.contains(query)) {
for (int pos : st.get(query)) {
for (int i = Math.max(0, pos - CONTEXT + 1); i < pos; i++) {
System.out.print(words[i] + " ");
}
System.out.print("\"" + words[pos] + "\"");
for (int i = pos + 1; i < Math.min(words.length, pos + CONTEXT); i++) {
System.out.print(" " + words[i]);
}
System.out.println();
}
}
}
}
use of edu.princeton.cs.algs4.In in project AlgorithmsSolutions by Allenskoo856.
the class KWIC method main.
public static void main(String[] args) {
In in = new In(args[0]);
int context = Integer.parseInt(args[1]);
String text = in.readAll().replaceAll("\\s+", " ");
int N = text.length();
SuffixArray sa = new SuffixArray(text);
while (StdIn.hasNextLine()) {
String q = StdIn.readLine();
for (int i = sa.rank(q); i < N && sa.select(i).startsWith(q); i++) {
int from = Math.max(0, sa.index(i) - context);
int to = Math.min(N - 1, from + q.length() + 2 * context);
StdOut.println(text.substring(from, to));
}
StdOut.println();
}
}
Aggregations