Beispiel #1
0
    /**
     * 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[] 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 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[] strarr)
        {
            string      str         = strarr[0];
            string      str2        = strarr[1];
            SymbolGraph symbolGraph = new SymbolGraph(str, str2);
            Graph       graph       = symbolGraph.G();

            while (StdIn.hasNextLine())
            {
                string str3 = StdIn.readLine();
                if (symbolGraph.contains(str3))
                {
                    int      i        = symbolGraph.index(str3);
                    Iterator iterator = graph.adj(i).iterator();
                    while (iterator.hasNext())
                    {
                        int i2 = ((Integer)iterator.next()).intValue();
                        StdOut.println(new StringBuilder().append("   ").append(symbolGraph.name(i2)).toString());
                    }
                }
                else
                {
                    StdOut.println(new StringBuilder().append("input not contain '").append(str3).append("'").toString());
                }
            }
        }
Beispiel #5
0
    /**
     * Read the following commands:
     * init n v     Initializes the array of size n with all v's
     * set a b c... Initializes the array  with [a, b, c ...]
     * rsq a b      Range Sum Query for the range [a, b]
     * rmq a b      Range Min Query for the range [a, b]
     * up  a b v    Update the [a,b] portion of the array with value v.
     * exit
     * <p>
     * Example:
     * init
     * set 1 2 3 4 5 6
     * rsq 1 3
     * Sum from 1 to 3 = 6
     * rmq 1 3
     * Min from 1 to 3 = 1
     * input up 1 3
     * [3,2,3,4,5,6]
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {


        SegmentTree st = null;

        String cmd = "cmp";
        while (true) {
            String[] line = StdIn.readLine().split(" ");

            if (line[0].equals("exit")) break;

            int arg1 = 0, arg2 = 0, arg3 = 0;

            if (line.length > 1) {
                arg1 = Integer.parseInt(line[1]);
            }
            if (line.length > 2) {
                arg2 = Integer.parseInt(line[2]);
            }
            if (line.length > 3) {
                arg3 = Integer.parseInt(line[3]);
            }

            if ((!line[0].equals("set") && !line[0].equals("init")) && st == null) {
                StdOut.println("Segment Tree not initialized");
                continue;
            }
            int array[];
Beispiel #6
0
    public static void main(String[] args) {
        String filename  = args[0];
        String separator = args[1];
        In in = new In(filename);

        ST<String, Queue<String>> st = new ST<String, Queue<String>>();
        ST<String, Queue<String>> ts = new ST<String, Queue<String>>();

        while (in.hasNextLine()) {
            String line = in.readLine();
            String[] fields = line.split(separator);
            String key = fields[0];
            for (int i = 1; i < fields.length; i++) {
                String val = fields[i];
                if (!st.contains(key)) st.put(key, new Queue<String>());
                if (!ts.contains(val)) ts.put(val, new Queue<String>());
                st.get(key).enqueue(val);
                ts.get(val).enqueue(key);
            }
        }

        StdOut.println("Done indexing");

        // read queries from standard input, one per line
        while (!StdIn.isEmpty()) {
            String query = StdIn.readLine();
            if (st.contains(query)) 
                for (String vals : st.get(query))
                    StdOut.println("  " + vals);
            if (ts.contains(query)) 
                for (String keys : ts.get(query))
                    StdOut.println("  " + keys);
        }

    }
Beispiel #7
0
 /**
  * Interprets the command-line argument as a regular expression
  * (supporting closure, binary or, parentheses, and wildcard)
  * reads in lines from standard input; writes to standard output
  * those lines that contain a substring matching the regular
  * expression.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) { 
     String regexp = "(.*" + args[0] + ".*)";
     NFA nfa = new NFA(regexp);
     while (StdIn.hasNextLine()) { 
         String line = StdIn.readLine();
         if (nfa.recognizes(line)) {
             StdOut.println(line);
         }
     }
 } 
Beispiel #8
0
    /**/ public static void main(string[] strarr)
    {
        int num  = Integer.parseInt(strarr[1]);
        int num2 = Integer.parseInt(strarr[2]);
        ST  sT   = new ST();

        In @in = new In(strarr[0]);

        while (@in.hasNextLine())
        {
            string   text  = @in.readLine();
            string[] array = java.lang.String.instancehelper_split(text, ",");
            string   c     = array[num];
            string   obj   = array[num2];
            sT.put(c, obj);
        }
        while (!StdIn.isEmpty())
        {
            string text = StdIn.readString();
            if (sT.contains(text))
            {
                StdOut.println(sT.get(text));
            }
            else
            {
                StdOut.println("Not found");
            }
        }
    }
}



public class LookupIndex
{
    public LookupIndex()
    {
    }

    /**/ public static void main(string[] strarr)
    {
        string str   = strarr[0];
        string regex = strarr[1];
        In     @in   = new In(str);
        ST     sT    = new ST();
        ST     sT2   = new ST();

        while (@in.hasNextLine())
        {
            string   text  = @in.readLine();
            string[] array = java.lang.String.instancehelper_split(text, regex);
            string   text2 = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                string text3 = array[i];
                if (!sT.contains(text2))
                {
                    sT.put(text2, new Queue());
                }
                if (!sT2.contains(text3))
                {
                    sT2.put(text3, new Queue());
                }
                ((Queue)sT.get(text2)).enqueue(text3);
                ((Queue)sT2.get(text3)).enqueue(text2);
            }
        }
        StdOut.println("Done indexing");
        while (!StdIn.isEmpty())
        {
            string text = StdIn.readLine();
            if (sT.contains(text))
            {
                Iterator iterator = ((Queue)sT.get(text)).iterator();
                while (iterator.hasNext())
                {
                    string text2 = (string)iterator.next();
                    StdOut.println(new StringBuilder().append("  ").append(text2).toString());
                }
            }
            if (sT2.contains(text))
            {
                Iterator iterator = ((Queue)sT2.get(text)).iterator();
                while (iterator.hasNext())
                {
                    string text2 = (string)iterator.next();
                    StdOut.println(new StringBuilder().append("  ").append(text2).toString());
                }
            }
        }
    }
Beispiel #9
0
    /**/
    public static void main(string[] strarr)
    {
        string        str           = strarr[0];
        string        str2          = strarr[1];
        SymbolDigraph symbolDigraph = new SymbolDigraph(str, str2);
        Digraph       digraph       = symbolDigraph.G();

        while (!StdIn.IsEmpty)
        {
            string   str3     = StdIn.readLine();
            Iterator iterator = digraph.adj(symbolDigraph.index(str3)).iterator();
            while (iterator.hasNext())
            {
                int i = ((Integer)iterator.next()).intValue();
                StdOut.println(new StringBuilder().append("   ").append(symbolDigraph.name(i)).toString());
            }
        }
    }
Beispiel #10
0
    /**/ public static void main(string[] strarr)
    {
        string      str         = strarr[0];
        string      str2        = strarr[1];
        string      str3        = strarr[2];
        SymbolGraph symbolGraph = new SymbolGraph(str, str2);
        Graph       g           = symbolGraph.G();

        if (!symbolGraph.contains(str3))
        {
            StdOut.println(new StringBuilder().append(str3).append(" not in database.").toString());
            return;
        }
        int i = symbolGraph.index(str3);
        BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(g, i);

        while (!StdIn.isEmpty())
        {
            string str4 = StdIn.readLine();
            if (symbolGraph.contains(str4))
            {
                int i2 = symbolGraph.index(str4);
                if (breadthFirstPaths.hasPathTo(i2))
                {
                    Iterator iterator = breadthFirstPaths.pathTo(i2).iterator();
                    while (iterator.hasNext())
                    {
                        int i3 = ((Integer)iterator.next()).intValue();
                        StdOut.println(new StringBuilder().append("   ").append(symbolGraph.name(i3)).toString());
                    }
                }
                else
                {
                    StdOut.println("Not connected");
                }
            }
            else
            {
                StdOut.println("   Not in database.");
            }
        }
    }
Beispiel #11
0
    /**
     *  Reads a sequence of transactions from standard input; takes a
     *  command-line integer m; prints to standard output the m largest
     *  transactions in descending order.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int m = Integer.parseInt(args[0]); 
        MinPQ<Transaction> pq = new MinPQ<Transaction>(m+1);

        while (StdIn.hasNextLine()) {
            // Create an entry from the next line and put on the PQ. 
            String line = StdIn.readLine();
            Transaction transaction = new Transaction(line);
            pq.insert(transaction); 

            // remove minimum if m+1 entries on the PQ
            if (pq.size() > m) 
                pq.delMin();
        }   // top m entries are on the PQ

        // print entries on PQ in reverse order
        Stack<Transaction> stack = new Stack<Transaction>();
        for (Transaction transaction : pq)
            stack.push(transaction);
        for (Transaction transaction : stack)
            StdOut.println(transaction);
    }