Searching the Web UVA - 1597
程序员文章站
2022-06-02 21:21:24
...
https://cn.vjudge.net/problem/UVA-1597
自己写的很麻烦也总超时,能力不够,uDebug也全都过了,把自己的代码贴上以后再重新写,看网上的基本都是一种方法,也没必要贴了。
#include <bits/stdc++.h>
using namespace std;
typedef vector<pair<string,int>> Vector;
map<string, Vector> m[1010];//一个单词对应所在的话和行数目
vector<string> v[1510];//存储原文
vector<string> line;//记录行数
vector<int> temp;
int cnt;
void Change(string &str) {
for(int i = 0; i < str.length(); i++)
str[i] = tolower(str[i]);
}
void init() {
string str;
int n,lines=0;
cin >> n;
getchar();
cnt = 0;
while(n--) {
while(getline(cin, str) && str!="**********") {
string t;
v[cnt].push_back(str);
line.push_back(str);
for(int i = 0; i < str.length(); i++)
if(!isalnum(str[i])) str[i] = ' ';
Change(str);
stringstream ss(str);
set<string> uni;
while(ss>>t)
uni.insert(t);
for(auto it=uni.begin(); it!=uni.end(); it++) {
if(!m[cnt].count(*it)) m[cnt].insert({*it,vector<pair<string,int>>()});
m[cnt].find(*it)->second.push_back(make_pair(str,lines));
}
lines++;
}
cnt++;
}
}
int main() {
freopen("i.txt", "r", stdin);
init();
string str,strl,strr;
int n;
cin >> n, getchar();
while(n--) {
getline(cin, str);
if(str.find("AND")!=string::npos) {
map<string, vector<pair<string,int>>> mt[1010];
strl = str.substr(0,str.find("AND")-1);
strr = str.substr(str.find("AND")+4);
Change(strl);
Change(strr);
bool f1 = false,f2 = false;
for(int i = 0; i < cnt; i++) {
mt[i] = m[i];
temp.clear();
if(mt[i].find(strl)!=mt[i].end() && mt[i].find(strr)!=mt[i].end()) {
auto vec1 = mt[i].find(strl)->second;
auto vec2 = m[i].find(strr)->second;
f1 = true;
for (auto it = vec1.begin(); it != vec1.end();) {
if (it->first.find(strl) != string::npos && it->first.find(strr) != string::npos)
it = vec1.erase(it);
else it++;
}
for (auto it = vec1.begin(); it != vec1.end(); it++) {
if (it->first.find(strl) != string::npos)
temp.push_back(it->second);
}
for (auto it = vec2.begin(); it != vec2.end(); it++) {
if (it->first.find(strr) != string::npos)
temp.push_back(it->second);
}
if (!f2) f2 = true;
else cout << "----------" << endl;
sort(temp.begin(), temp.end());
for (int j = 0; j < temp.size(); j++)
cout << line[temp[j]] << endl;
}
}
if(!f1) cout << "Sorry, I found nothing." << endl;
}
else if(str.find("OR")!=string::npos) {
map<string, vector<pair<string,int>>> mt[1010];
strl = str.substr(0,str.find("OR")-1);
strr = str.substr(str.find("OR")+3);
Change(strl);
Change(strr);
bool f1, f2 = false, f3, f4 = false;
for(int i = 0; i < cnt; i++) {
mt[i] = m[i];
temp.clear();
f1 = false, f3 = false;
if(mt[i].find(strl)!=mt[i].end()) {
auto vec1 = mt[i].find(strl)->second;
for (auto it = vec1.begin(); it != vec1.end();) {
if (it->first.find(strl) != string::npos && it->first.find(strr) != string::npos)
it = vec1.erase(it);
else it++;
}
for (auto it = vec1.begin(); it != vec1.end(); it++) {
if (it->first.find(strl) != string::npos) {
f1 = true;
temp.push_back(it->second);
}
}
}
if(m[i].find(strr)!=m[i].end()) {
auto vec = m[i].find(strr)->second;
for (auto it = vec.begin(); it != vec.end(); it++) {
if (it->first.find(strr) != string::npos) {
f3 = true;
temp.push_back(it->second);
}
}
}
if(f1 || f3) {
f4 = true;
if (!f2) f2 = true;
else cout << "----------" << endl;
sort(temp.begin(), temp.end());
for (int j = 0; j < temp.size(); j++)
cout << line[temp[j]] << endl;
}
}
if(!f4) cout << "Sorry, I found nothing." << endl;
}
else if(str.find("NOT")!=string::npos) {
string string1 = str.substr(4);
Change(string1);
bool f1 = false, f2 = false;
for(int i = 0; i < cnt; i++) {
if(m[i].find(string1)==m[i].end()) {
f1 = true;
if(!f2) f2 = true;
else cout << "----------" << endl;
for(int j = 0; j < v[i].size(); j++)
cout << v[i][j] << endl;
}
}
if(!f1) cout << "Sorry, I found nothing." << endl;
}
else {
Change(str);
bool f1 = false, f2 = false;
for(int i = 0; i < cnt; i++) {
if(m[i].find(str)!=m[i].end()) {
f1 = true;
auto vec = m[i].find(str)->second;
if (!f2) f2 = true;
else cout << "----------" << endl;
for(auto it=vec.begin(); it!=vec.end(); it++) {
cout << line[it->second] << endl;
}
}
}
if(!f1) cout << "Sorry, I found nothing." << endl;
}
cout << "==========" << endl;
}
return 0;
}
//4
//A manufacturer, importer, or seller of
//digital media devices may not (1) sell,
//or offer for sale, in interstate commerce,
//or (2) cause to be transported in, or in a
//manner affecting, interstate commerce,
//a digital media device unless the device
// includes and utilizes standard security
// technologies that adhere to the security
// system standards.
//**********
//Of course, Lisa did not necessarily
// intend to read his books. She might
//want the computer only to write her
// midterm. But Dan knew she came from
//a middle-class family and could hardly
// afford the tuition, let alone her reading
// fees. Books might be the only way she
// could graduate
// **********
// Research in analysis (i.e., the evaluation
// of the strengths and weaknesses of
//computer system) is essential to the
//development of effective security, both
//for works protected by copyright law
//and for information in general. Such
// research can progress only through the
// open publication and exchange of
// complete scientific results
//**********
//I am very very very happy!
//What about you?
//**********
//6
//computer
// books AND computer
//books OR protected
//NOT security
//very
// slick
上一篇: 二分搜索问题
下一篇: springMVC 框架搭建学习