CCF CSP command line options

Posted by merylvingien on Fri, 31 Dec 2021 23:02:42 +0100

subject

Problem description
Please write a command line analyzer to analyze which options are included in a 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 composed of lowercase letters. Your program does not need to process it. Several options may be included after the tool name, and then some parameters that are not options may be included.
There are two types of options: options with parameters and options without parameters. A legal parameterless option takes the form of a minus sign followed by a single lowercase letter, such as "- a" or "- b". The option with parameters consists of two strings separated by spaces. The format of the former is the same as that of the option without parameters, and the latter is the parameter of the option, which is a non empty string composed of lowercase letters, numbers and minus signs.
The author of the command-line tool provides you with a format string to specify which options his command-line tool needs to 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, otherwise it is an option without parameters. For example, "ab: m:" means that the program accepts three options, namely "- a" (without parameters), "- b" (with parameters), and "- m" (with parameters).
The author of the command line tool has prepared several command lines to test your program. For each command line, your tool should always analyze backwards. When your tool encounters a string that is neither a legal option nor a parameter of a legal option, the analysis stops. The remaining unparsed parts of the command line do not constitute options for the command, so your program should ignore them.
Input format
The first line of input is a format string, which contains at least one character and is no longer than 52. The format string only contains lowercase letters and colons. Each lowercase letter can appear at most once. There will be no two adjacent colons or start with colons.
The second input line is a positive integer N(1 ≤ N ≤ 20), indicating the number of command lines you need to process.
Next, there are N lines. Each line is a command line to be processed, which contains no more than 256 characters. The command line must consist of several strings separated by a single space. Each string contains only lowercase letters, numbers and minus signs.
Output format
The output has N lines. Line i starts with "Case i:" and there should be exactly one space. Then, the names of all options used in the command line should be output in alphabetical ascending order. For options with parameters, its parameters should be output after its name. If an option appears multiple times on the command line, it is output only once. If an option with parameters appears multiple times on the command line, only the parameters with the last occurrence are output.
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 analysis

  • Topic type: simulation + string processing
  • Key points: getchar() read carriage return, use of stringstream, data input and storage

AC code

#include<iostream>
#include<sstream>
#include<vector>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 30;

int n;
bool o1[N], o2[N];      //o1 stores commands without parameters and o2 stores commands with parameters
string ans[N];          //Mark the characters (a~z) appearing in the command to be processed and their recording parameters (if any)

int main(){
    string str;
    cin >> str;
    for(int i = 0; i < str.size(); i ++ )
        if(i + 1 < str.size() && str[i + 1] == ':')
        {
            o2[str[i] - 'a'] = true;
            i ++ ;
        }
        else    o1[str[i] - 'a'] = true;

    cin >> n;
    getchar();      //Enter after reading n
    for(int C = 1; C <= n; C ++ )
    {
        printf("Case %d:", C);
        getline(cin, str);
        stringstream ssin(str);
        vector<string> ops;
        while(ssin >> str)  ops.push_back(str);
        for(int i = 0; i < 26; i ++ )   ans[i].clear();
        for(int i = 1; i < ops.size(); i ++ )
        {
            if(ops[i][0] != '-' || ops[i][1] < 'a' || ops[i].size() != 2)   break;
            int k = ops[i][1] - 'a';
            if(o1[k])   ans[k] = '*';
            else if(o2[k] && i + 1 < ops.size())
                ans[k] = ops[i + 1], i ++ ;
            else break;
        }

        for(int i = 0; i < 26; i ++ )
            if(ans[i].size())
            {
                cout << " -" << char(i + 'a');
                if(o2[i])   cout << ' ' << ans[i];
            }
        cout << endl;
    }
    return 0;
}

Topics: data structure string