知识点:模拟
这个题主体写了38分钟,然后格式和改错,总共花了52分钟过了,对于新手感觉可以了,也是紫书写的第一道蓝题,因为知道这个就是模拟,所以其实难度降低了,因为毕竟不需要看原文。。。
主要的思路就是,输入的时候的处理,首先我们要把每个文章每一行都原封不动的储存起来,这需要一个数组,然后我们需要一个集合数组,每个文章里面的单词放到一个集合里面,然后我们还需要一个数组,用来存放每篇文章整理过后的每一行,整理的方法是,把所有的单词化成小写,然后后面加一个空格,拼成一个字符串,字符串的前面再加一个空格,这样我们想要查某个单词的时候就查前后加了空格的单词,因为不加空格,直接拿着原文那种去查,会因为单词包含单词的问题出错,然后其余的就比较简单了,然后如果查询没有查到东西要特殊输出
最后是一个特别坑的地方,那就是样例的输出是错的,分割线是10个但是样例是9个,这个你要去看原文的输入输出才知道。
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
getchar();
vector<string> a[105], b[105];
set<string> st[105];
string s;
for (int i = 0; i < t; i++) {
while (getline(cin, s) && s != "**********") {
a[i].push_back(s);
for (int i = 0; i < (int) s.size(); i++) {
if (isalpha(s[i])) s[i] = tolower(s[i]);
else s[i] = ' ';
}
stringstream ss(s);
string tmp;
string tmp2 = " ";
while (ss >> tmp) {
st[i].insert(tmp);
tmp2 += tmp + " ";
}
b[i].push_back(tmp2);
}
}
int q;
cin >> q;
getchar();
while (q--) {
getline(cin, s);
string s1, s2, s3, s4;
int ok = 0;
int ook = 0;
if ((int) s.find("AND") != -1) {
int pos = (int) s.find(" AND ");
s1 = s.substr(0, pos);
s2 = s.substr(pos + 5, (int) s.size() - 5 - pos);
s3 = " " + s1 + " ";
s4 = " " + s2 + " ";
for (int i = 0; i < t; i++) {
if (st[i].find(s1) == st[i].end() || st[i].find(s2) == st[i].end()) continue;
if (ook++) cout << "----------\n";
for (int j = 0; j < (int) b[i].size(); j++) {
if ((int) b[i][j].find(s3) != -1 || (int) b[i][j].find(s4) != -1) {
cout << a[i][j] << endl; ok = 1;
}
}
}
} else if ((int) s.find("OR") != -1) {
int pos = (int) s.find(" OR ");
s1 = s.substr(0, pos);
s2 = s.substr(pos + 4, (int) s.size() - 4 - pos);
s3 = " " + s1 + " ";
s4 = " " + s2 + " ";
for (int i = 0; i < t; i++) {
if (st[i].find(s1) == st[i].end() && st[i].find(s2) == st[i].end()) continue;
if (ook++) cout << "----------\n";
for (int j = 0; j < (int) b[i].size(); j++) {
if ((int) b[i][j].find(s3) != -1 || (int) b[i][j].find(s4) != -1) {
cout << a[i][j] << endl; ok = 1;
}
}
}
} else if ((int) s.find("NOT") != -1) {
s1 = s.substr(4, (int) s.size() - 4);
for (int i = 0; i < t; i++) {
if (st[i].find(s1) != st[i].end()) continue;
ok = 1;
if (ook++) cout << "----------\n";
for (int j = 0; j < (int) a[i].size(); j++) {
cout << a[i][j] << endl;
}
}
} else {
s3 = " " + s + " ";
for (int i = 0; i < t; i++) {
if (st[i].find(s) == st[i].end()) continue;
if (ook++) cout << "----------\n";
for (int j = 0; j < (int) b[i].size(); j++) {
if ((int) b[i][j].find(s3) != -1) {
cout << a[i][j] << endl; ok = 1;
}
}
}
}
if (!ok) cout << "Sorry, I found nothing.\n";
cout << "==========\n";
}
return 0;
}
310




被折叠的 条评论
为什么被折叠?



