文章目录
- C++
- 总结
本题链接:命令行选项
本博客给出本题截图:
C++
#include <iostream> #include <cstdio> #include <cstring> #include <sstream> #include <string> #include <vector> using namespace std; const int N = 30; bool st1[N], st2[N]; string res[N]; int main() { string a; cin >> a; for (int i = 0; i < a.size(); i ++ ) if (i + 1 < a.size() && a[i + 1] == ':') { st2[a[i] - 'a'] = true; //带参 i ++; } else st1[a[i] - 'a'] = true; //不带参 int n; cin >> n; getchar(); for (int i = 1; i <= n; i ++ ) { printf("Case %d:", i); getline(cin, a); stringstream ssin(a); vector<string> s; while (ssin >> a) s.push_back(a); for (int i = 0; i < N; i ++ ) res[i].clear(); //注意每次需要清空res数组 for (int i = 1; i < s.size(); i ++ ) { if (s[i][0] != '-' || s[i][1] < 'a' || s[i].size() != 2) break; //这里break的是不合法的读入 int k = s[i][1] - 'a'; if (st1[k]) res[k] = "x"; //这里可以是任何东西(只要有东西就可) else if (st2[k] && i + 1 < s.size()) { res[k] = s[i + 1]; i ++; } else break; //这里break的是合法读入中的不符合题意(如初始字符串没有'c',但是后面读入中有'c','c'后面的读入是无效的) } for (int i = 0; i < N; i ++ ) if (res[i].size()) { cout << " -" << (char)(i + 'a'); if (st2[i]) cout << ' ' << res[i]; } cout << endl; } return 0; }
总结
模拟题,注意细节,因为空格问题卡了三次,(麻了