题解 AT1237 【二重否定除去法則】

TRZ_2007

2020-09-23 19:40:35

Solution

# Description 给你一些字符串,在最后一个字符串不为 ```not``` 的前提下将 ```not``` 成对去除。 # Solution 考虑用 ```vector``` ,把每一个读入的字符串插入这个 ```vector``` 中,如果最后面的字符并非 ```not``` ,那么就把前面的 ```not``` 全部**成对**删掉否则就保留。 # Code ```cpp #include <bits/stdc++.h> using namespace std; const int N = 5009; string s; vector<string> g; int len; int main() { while(cin >> s) g.push_back(s); len = g.size(); for(int i = len - 1;i >= 0;i--) { //从后向前寻找 if(g[i] != "not" && i - 2 >= 0 && g[i - 1] == "not" && g[i - 2] == "not") { g.erase(g.begin() + i - 2,g.begin() + i); //vector 的删除函数 } } len = g.size(); for(int i = 0;i < len;i++) { if(i) cout << " " << g[i]; else cout << g[i]; }puts(""); return 0; } ```