Problem Description
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. There may be several options after the tool name, and then there may be parameters that are not options.
There are two types of options: options with parameters and options without parameters. A legitimate parametric-free option takes the form of a minus sign followed by a single lowercase letter, such as "-a" or "-b". The parameterized option consists of two space-separated strings, the former with the same format requirements as the non-parameterized option, and the latter with the parameters of the option, which is a non-empty string composed of lowercase letters, numbers and minus signs.
The author of the command-line tool gives you a format string to specify which options his command-line tool needs to accept. This string consists of several lower-case 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, "a b: 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 legitimate option nor a parameter of a legitimate option, the analysis stops. The remaining unanalyzed 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 that contains at least one character and is not longer than 52. The format string contains only lowercase letters and colons, ensuring that each lowercase letter occurs at most once, without two adjacent colons or starting with a colon.
The second line of input is a positive integer N(1 < N < 20), indicating the number of command lines you need to process.
Next, there are N lines, each of which is a command line to be processed. It contains 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 minus signs.
Output format
The output has N lines. Line I begins with "Case i:", then there should be exactly one space, and then the names of all options used on the command line should be output in alphabetical ascending order. For options with parameters, the parameters should be output after the name of the option is output. If an option appears multiple times on the command line, it is output only once. If an option with parameters occurs multiple times on the command line, only the parameters with which it last appears 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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
string s;
cin>>s;
char buffer[260];
int n;
cin>>n;
cin.getline(buffer,260); //Read useless characters to the end of the line
for(int i=1;i<=n;i++)
{
cin.getline(buffer,260);
string a(buffer); //Convert the char array to string
vector<string> v; //Declare the vector to store the cut string
map<string,string> map1; //Declare that map stores commands and parameters, first as commands, second as parameters
for(int t = (int)a.find(" ");t != -1; t = (int)a.find(" "))
{
string temp = a.substr(0,t);
v.push_back(temp);
a = a.substr(t+1);
}
v.push_back(a); //The above section cuts the strings and puts them all into vectors.
for(int j=0;j<v.size();j++) //Traversal of elements in vectors
{
if(v[j].size()==2 && v[j][0] == '-') //If it is an order
{
char c = v[j][1]; //Find the command
int next = (int)s.find(c);
if(next==-1) //If the order is illegal, jump out
break;
if(map1.find(v[j]) == map1.end()) //If the command does not exist, add it. The key value defaults to null for the second parameter.
map1.insert(pair<string,string>(v[j],""));
if(next+1<s.size() && s[next+1]==':' && j+1<v.size()){
map<string,string>::iterator it = map1.find(v[j]);
it->second = v[j+1];
j++;
} //Above is the check of the first i + 1 v element. If the first v element needs a parameter, it writes the second of the map key pair. This method directly covers the previous parameter.
} else if(j!=0) //If it's not a command and it's not the first (program name), jump out
break;
} //Because map itself is the ascending order of the keys, it reduces the trouble of sorting itself and can be output directly by iterator.
cout<<"Case "<<i<<":";
for(map<string,string>::iterator it = map1.begin();it != map1.end();it++)
{
cout<<" "<<it->first;
if(it->second !="")
cout<<" "<<it->second;
}
cout<<endl;
}
return 0;
}