本文整理汇总了Java中edu.princeton.cs.algs4.StdIn.readLine方法的典型用法代码示例。如果您正苦于以下问题:Java StdIn.readLine方法的具体用法?Java StdIn.readLine怎么用?Java StdIn.readLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.princeton.cs.algs4.StdIn
的用法示例。
在下文中一共展示了StdIn.readLine方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
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.hasNextChar()) {
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));
}
}
}
示例2: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
/**
* Unit tests the <tt>SymbolGraph</tt> data type.
*/
public static void main(String[] args) {
String filename = args[0];
String delimiter = args[1];
SymbolGraph sg = new SymbolGraph(filename, delimiter);
Graph G = sg.G();
while (StdIn.hasNextLine()) {
String source = StdIn.readLine();
if (sg.contains(source)) {
int s = sg.index(source);
for (int v : G.adj(s)) {
StdOut.println(" " + sg.name(v));
}
}
else {
StdOut.println("input not contain '" + source + "'");
}
}
}
示例3: readInputAndBuildConcordance
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
private Map<String, List<Integer>> readInputAndBuildConcordance() {
int wordIndex = 0;
Map<String, List<Integer>> concordanceMap = new HashMap<>();
while (StdIn.hasNextLine()) {
String wordLine = StdIn.readLine();
String[] words = wordLine.split(" ");
for(String word : words) {
if(!concordanceMap.containsKey(word)) {
concordanceMap.put(word, new ArrayList<>());
}
concordanceMap.get(word).add(wordIndex);
wordIndex++;
}
}
return concordanceMap;
}
示例4: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
MinPQ<Transaction> pq = new MinPQ<>();
while (StdIn.hasNextLine()) {
String line = StdIn.readLine();
Transaction item = new Transaction(line);
pq.insert(item);
if (pq.size() > M) pq.delMin();
}
}
示例5: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
public static void main(String[] args) {
String file = args[0];
String delimiter = args[1];
String source = args[2];
SymbolGraph g = new SymbolGraph(file, delimiter);
if (!g.contains(source)) {
StdOut.println("not in graph");
return;
}
StdOut.println("Source: " + source);
BreadthFirstPaths bfs = new BreadthFirstPaths(g.G(), g.index(source));
while (!StdIn.isEmpty()) {
String sink = StdIn.readLine();
if (g.contains(sink)) {
StdOut.println("path to " + sink);
if (bfs.hasPathTo(g.index(sink))) {
for (int i : bfs.pathTo(g.index(sink))) {
StdOut.println(" " + g.name(i));
}
} else {
StdOut.println("not connected");
}
} else {
System.out.println(" not in database");
}
}
}
示例6: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
public static void main(String[] args) {
int jobs = StdIn.readInt();
StdIn.readLine();
EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(2 * jobs + 2);
int source = 2 * jobs;
int target = 2 * jobs + 1;
for(int job = 0; job < jobs; job++) {
String[] jobInformation = StdIn.readLine().split("\\s+");
double duration = Double.parseDouble(jobInformation[0]);
edgeWeightedDigraph.addEdge(new DirectedEdge(job, job + jobs, duration));
edgeWeightedDigraph.addEdge(new DirectedEdge(source, job, 0));
edgeWeightedDigraph.addEdge(new DirectedEdge(job + jobs, target, 0));
for(int successors = 1; successors < jobInformation.length; successors++) {
int successor = Integer.parseInt(jobInformation[successors]);
edgeWeightedDigraph.addEdge(new DirectedEdge(job + jobs, successor, 0));
}
}
AcyclicLP acyclicLP = new AcyclicLP(edgeWeightedDigraph, source);
StdOut.println("Start times:");
for(int job = 0; job < jobs; job++) {
StdOut.printf("%4d: %5.1f\n", job, acyclicLP.distTo(job));
}
StdOut.printf("Finish time: %5.1f\n", acyclicLP.distTo(target));
}
示例7: degreesOfSeparationDFS
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
private void degreesOfSeparationDFS(String fileName, String separator, String sourcePerformer) {
SymbolGraph symbolGraph = new SymbolGraph(fileName, separator);
Graph graph = symbolGraph.graph();
if(!symbolGraph.contains(sourcePerformer)) {
StdOut.println(sourcePerformer + " not in database.");
return;
}
int sourceVertex = symbolGraph.index(sourcePerformer);
DepthFirstPathsIterative depthFirstPathsIterative = new DepthFirstPathsIterative(graph, sourceVertex);
while (!StdIn.isEmpty()) {
String sink = StdIn.readLine();
if(symbolGraph.contains(sink)) {
int destinationVertex = symbolGraph.index(sink);
if(depthFirstPathsIterative.hasPathTo(destinationVertex)) {
for(int vertexInPath : depthFirstPathsIterative.pathTo(destinationVertex)) {
StdOut.println(" " + symbolGraph.name(vertexInPath));
}
} else {
StdOut.println("Not connected");
}
} else {
StdOut.println("Not in database.");
}
}
}
示例8: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
public static void main(String[] args) {
SymbolGraph symbolGraph = new SymbolGraph(args[0], args[1]);
Graph graph = symbolGraph.graph();
String source = args[2];
if(!symbolGraph.contains(source)) {
StdOut.println(source + " not in database.");
return;
}
int sourceVertex = symbolGraph.index(source);
BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(graph, sourceVertex);
while (!StdIn.isEmpty()) {
String sink = StdIn.readLine();
if(symbolGraph.contains(sink)) {
int destinationVertex = symbolGraph.index(sink);
if(breadthFirstPaths.hasPathTo(destinationVertex)) {
for(int vertexInPath : breadthFirstPaths.pathTo(destinationVertex)) {
StdOut.println(" " + symbolGraph.name(vertexInPath));
}
} else {
StdOut.println("Not connected");
}
} else {
StdOut.println("Not in database.");
}
}
}
示例9: readConcordanceFromInput
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
private Map<String, List<Integer>> readConcordanceFromInput() {
Map<String, List<Integer>> concordanceMap = new HashMap<>();
while (StdIn.hasNextLine()) {
String concordanceLine = StdIn.readLine();
String[] concordanceInformation = concordanceLine.split(" ");
String key = concordanceInformation[0];
concordanceMap.put(key, new ArrayList<>());
for(int i = 1; i < concordanceInformation.length; i++) {
String noCommaValue = concordanceInformation[i];
if(noCommaValue.charAt(noCommaValue.length() - 1) == ',') {
noCommaValue = noCommaValue.substring(0, noCommaValue.length() - 1);
}
int position = Integer.parseInt(noCommaValue);
concordanceMap.get(key).add(position);
if(position > numberOfWords) {
numberOfWords = position;
}
}
}
return concordanceMap;
}
示例10: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
PriorityQueue<Point> priorityQueue = new PriorityQueue<>(m + 1, PriorityQueue.Orientation.MAX);
while (StdIn.hasNextLine()) {
String pointsLine = StdIn.readLine();
String[] pointsString = pointsLine.split(" ");
double x = Double.parseDouble(pointsString[0]);
double y = Double.parseDouble(pointsString[1]);
double z = Double.parseDouble(pointsString[2]);
Point point = new Exercise28_SelectionFilter().new Point(x, y, z);
priorityQueue.insert(point);
if(priorityQueue.size() > m) {
priorityQueue.deleteTop();
}
}
Stack<Point> pointsStack = reversePointsOrder(priorityQueue);
printPoints(pointsStack);
//Estimate running time
int initialArraySize = 100000; //10^5
int numberOfExperiments = 3; //10^5, 10^6, 10^7
doExperimentToEstimateRunningTime(initialArraySize, numberOfExperiments);
}
示例11: buildGraphAndGetInput
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
private void buildGraphAndGetInput(String filePath, String separator, String sourceMovie, String yearsOld) {
SymbolGraph movieSymbolGraph = new SymbolGraph(filePath, separator);
Graph graph = movieSymbolGraph.graph();
if(!movieSymbolGraph.contains(sourceMovie)) {
StdOut.println(sourceMovie + " not in database.");
return;
}
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int yearsOldToConsider = Integer.parseInt(yearsOld);
int sourceVertex = movieSymbolGraph.index(sourceMovie);
BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(graph, sourceVertex);
while (!StdIn.isEmpty()) {
String sink = StdIn.readLine();
//All movie names in the graph end with "(YYYY)"
int movieYear = Integer.parseInt(sink.substring(sink.length() - 5, sink.length() - 1));
if(currentYear - movieYear > yearsOldToConsider) {
StdOut.println("Ignoring old movie");
continue;
}
if(movieSymbolGraph.contains(sink)) {
int destinationVertex = movieSymbolGraph.index(sink);
if(breadthFirstPaths.hasPathTo(destinationVertex)) {
for(int vertexInPath : breadthFirstPaths.pathTo(destinationVertex)) {
StdOut.println(" " + movieSymbolGraph.name(vertexInPath));
}
} else {
StdOut.println("Not connected");
}
} else {
StdOut.println("Not in database.");
}
}
}
示例12: main
import edu.princeton.cs.algs4.StdIn; //导入方法依赖的package包/类
public static void main(String[] args) {
//CSV file - "/Users/rene/Desktop/Algorithms/Books/Algorithms, 4th ed. - Exercises/Data/csv_file.txt"
// rene,1,abc
// sedgewick,9,aaa
// dijkstra,10,dgs
// rene,3,asfa
// wayne,9,lpa
// rene,5,lll
// wayne,10,zzp
// arnold,200,aab
// dwayne,201,bba
// fenwick,202,bbc
//Queries
// 0 1 wayne
// 0 2 rene
// 1 0 3
// 1 2 200
// 2 0 aaa
// 2 1 lpa
// 0 0 fenwick
//Expected output
// 10
// lll
// rene
// aab
// sedgewick
// 9
// fenwick
Exercise22_FullyIndexedCSV fullyIndexedCSV = new Exercise22_FullyIndexedCSV();
FullLookupCSV fullLookupCSV = fullyIndexedCSV.new FullLookupCSV();
fullLookupCSV.buildHashMapArray(args[0]);
while (StdIn.hasNextLine()) {
String line = StdIn.readLine();
String[] words = line.split(" ");
int keyField = Integer.parseInt(words[0]);
int valueField = Integer.parseInt(words[1]);
String query = words[2];
StdOut.println(fullLookupCSV.get(keyField, valueField, query));
}
}