You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
// https://www.cnblogs.com/meelo/archive/2004/01/13/7706352.html
int main() {
//输入+输出重定向
freopen("../1351.in", "r", stdin);
freopen("../1351.out", "w", stdout);
/*
//在内部声明两个不同类型的数组,测试一下是不是没有初始化时,它们是不是脏的。
//结果表明int的是脏的string的不同是空字符串
int dirty_type[26];
string dirty_argument[26];
*/
// 如果不明确指出初始化列表,那么基本类型是不会被初始化的(除全局变量和静态变量外),所有的内存都是“脏的”;
// 0:不存在 1:无参数选项 2:有参数选项
int type[26]; //做为函数外的全局变量它是默认有初始值的因为是整数数组所以默认值是0
//规则
string patten;
cin >> patten;
for (int i = 0; i < patten.length(); i++) {
int last_arg; //这个没有初始化的变量一直都是52没想明白为什么
if (patten[i] == ':') {
type[last_arg] = 2; //修改前面的参数符号记录它是2类型即有参数选项
} else {
last_arg = patten[i] - 'a';//换算为索引位置记录到映射的数组中标识为1.就是一个桶排的思路26个字母人人有份。
type[last_arg] = 1;
}
}
//输入N行
int n;
cin >> n;
//因为下面要使用getline,所以这个cin.ignore很重要
// cin.ignore()的用法
// https://blog.csdn.net/wxbmelisky/article/details/48596881
cin.ignore(); //其实就是为了吃掉最后的那个换行符
//开始每一行
for (int i = 1; i <= n; i++) {
//每个字符的参数值,下标映射着字符的位置,比如 a:0 b:1 ...
string argument[26] = {""};
//读取每一行到字符串temp中
string temp;
getline(cin, temp); //上面的cin.ignore()就是为它服务的
stringstream ss(temp);
//放过第一个,第一个是命令本体,需要放过
ss >> temp;
//按空格分割
while (ss >> temp) {
//如果首字母是-,表示是参数标识
if (temp[0] == '-') {
int last_arg = temp[1] - 'a'; //换算成选项字母在数组中的位置
if (type[last_arg] == 0) { // 非合法选项,就是没有声明这个选项,结果出来了~
break; //根据题意,就停止这个命令行的判定
} else if (type[last_arg] == 1) { //无参数
argument[last_arg] = " ";
} else if (type[last_arg] == 2) { //有参数的,需要读取参数
if (ss.eof()) break; // 后面没有了,就是说有参数,但没有提供参数,是错误的
//读取参数
ss >> temp;
argument[last_arg] = temp;
}
} else { // 非合法参数,因为在上面的判断中再次使用了ss>>temp,相当于多读取参数的具体值,再出现无-的,就是非法参数了。
break;
}
}
//输出
cout << "Case " << i << ":";
for (int j = 0; j < 26; j++) { //按字母序输出
//因为argument是26个字母都标注的其实只有内容不是空的才表示需要输出
if (argument[j] == "")continue;
//无参数
if (type[j] == 1) {
cout << " -" << char(j + 'a');
} else
//有参数
if (type[j] == 2) {
cout << " -" << char(j + 'a') << ' ' << argument[j];
}
}
cout << endl;
}
//关闭文件
fclose(stdin);
fclose(stdout);
return 0;
}