CCF command line Options (Java)

Source: Internet
Author: User
Tags time limit

Question number: 201403-3
Question Name: Command-Line Options
Time limit: 1.0s
Memory Limit: 256.0MB
Problem Description: Problem description Please write a command-line analyzer to analyze which options are included in the given command line. Each command line consists of several strings, separated by exactly one space. The first of these strings is the name of the command-line tool, which is made up of lowercase letters, and your program doesn't have to handle it. After the tool name may contain several options, and then may contain some parameters that are not options.
There are two types of options: options with parameters and options with no parameters. A valid parameter-free option is a minus sign followed by a single lowercase letter, such as "-a" or "B". With parameter options, there are two space-delimited strings, which have the same formatting requirements as the parameterless option, which is the argument for the option, and is a non-empty string consisting of lowercase letters, numbers, and minus signs.
The command-line tool's author gives you a format string to specify which options his command-line tools will accept. This string consists of a number of lowercase letters and colons, each of which represents an option accepted by the program. If the lowercase letter is followed by a colon, it represents an option with parameters, or an option with no parameters. For example, "ab:m:" means that the program accepts three options, that is, "-a" (without parameters), "-B" (with parameters), and "-M" (with parameters).
The author of the command-line tool has prepared several command-line lines to test your program. For each command line, your tool should always be parsed backwards. The analysis stops when your tool encounters a string that is neither a legitimate option nor a parameter to a legitimate option. The remaining parsed portions of the command line do not make up the options for the command, so your program should ignore them. The first line entered in the input format is a format string that contains at least one character and is no longer than 52. The format string contains only lowercase letters and colons, guaranteeing that each lowercase letter appears at most once, with no two adjacent colons and no colon.
The second line you enter is a positive integer N (1≤n≤20) that represents the number of command lines you need to handle.
Next there are N rows, each row is a command line to be processed, and it includes no more than 256 characters. The command line must consist of several strings separated by a single space, each containing only lowercase letters, numbers, and a minus sign. Output format output has N rows. Where line I begins with "Case I:", then there should be exactly one space, and then the name of all the options used in the command line should be printed in ascending alphabetical order, with the option to output its parameters after outputting its name. If an option appears more than once on the command line, it is output only once. If an option with a parameter appears more than once on the command line, only the parameters that came with the last occurrence are printed. Sample Input Albw:x
4
Ls-a-l-a Documents-b
Ls
Ls-w 10-x-W 15
Ls-a-b-c-d-e-L Sample Output Case 1:-a-l
Case 2:
Case 3:-W 15-x
Case 4:-a-b

Problem solving Code (Java):

Import Java.util.Scanner;  
    Class Arg {Boolean haspar;  
Boolean isappear; public class Main {public static void main (string[] args) {Scanner input = new Scanner (System.  
        in);  
        Arg[] argues = new arg[26];  
        for (int i = 0; i < argues.length i++) {argues[i] = new Arg ();  
        char[] rule = input.nextline (). ToCharArray (); for (int i = 0; i < rule.length i++) {if (rule[i] = = ': ') argues[rule[i-1]-' a '].h  
            Aspar = true;  
            else {Argues[rule[i]-' a '].isappear = true;  
          
        {} int n = integer.parseint (Input.nextline ());  
        Stringbuilder[] result = new Stringbuilder[n];  
            for (int i = 0; i < n; i++) {Result[i] = new StringBuilder ("case" + (i + 1) + ":");  
            boolean[] Isappear = new BOOLEAN[26]; String[] parameter = new STRING[26];  
            string[] in = Input.nextline (). Split (""); for (int j = 1; j < In.length;) {if ((In[j].length ()!= 2 | | | In[j].charat (0)!= '-') | |  
                        (In[j].charat (1)-' a ' < 0 | | In[j].charat (1)-' A ' > 25 ') || (!argues[in[j].charat (1)-' a '].isappear)  
                {break;  
                char C = in[j].charat (1);  
                    if (Argues[c-' a '].haspar) {isappear[c-' a '] = true;  
                    Parameter[c-' a '] = in[j + 1];  
                J + + 2;  
                    else {isappear[c-' a '] = true;  
                j + +;  
                    for (int j = 0 J < + j + +) {if (Isappear[j]) {  
                    Char temp = (char) (' a ' + j); Result[i].append ("-" + temp + " ");  
                if (Argues[j].haspar) result[i].append (Parameter[j] + "");  
        }} input.close ();  
        for (int i = 0; i < n; i++) {System.out.println (result[i].tostring (). Trim ());   }  
    }  
}


can get 90 points, but run wrong, don't know what's going on, consult.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.