uva 1597 Searching the Web (wrong)

来源:互联网 发布:东莞商恩网络优化排名 编辑:程序博客网 时间:2024/06/11 03:31

题目:Searching the Web


思路:

做了几天,还是没过,先把错误记录下:

(1)每输入一个指令,在文章中查找一遍。(TLE)

(2)每个单词都对应一些文章号和行号,先处理后直接查找打印。(WA)


代码:

//TLE代码#include<cstdio>#include<iostream>#include<string>#include<vector>#include<set>#include<map>#include<algorithm>#include<cmath>#include<queue>using namespace std;int n,m;vector<string> Passage[105];vector<string> p[105];void FIND(string x) {vector<int> vec;for(int i=1; i<=n; i++) {for(int j=0; j<Passage[i].size(); j++) {if(p[i][j].find(x)!=string::npos) {vec.push_back(i);break;}}}if(vec.size()==0) {cout<<"Sorry, I found nothing.\n";return ;}for(int i=0; i<vec.size()-1; i++) {for(int j=0; j<Passage[vec[i]].size(); j++) {if(p[vec[i]][j].find(x)!=string::npos)cout<<Passage[vec[i]][j]<<endl;}cout<<"---------\n";}for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {if(p[vec[vec.size()-1]][j].find(x)!=string::npos)cout<<Passage[vec[vec.size()-1]][j]<<endl;}}void AND(string x,string y) {vector<int> vec;for(int i=1; i<=n; i++) {bool f1=false,f2=false;for(int j=0; j<Passage[i].size(); j++) {if(p[i][j].find(x)!=string::npos) {f1=true;}if(p[i][j].find(x)!=string::npos) {f2=true;}}if(f1==true&&f2==true) vec.push_back(i);}if(vec.size()==0) {cout<<"Sorry, I found nothing.\n";return ;}for(int i=0; i<vec.size()-1; i++) {for(int j=0; j<Passage[vec[i]].size(); j++) {if(p[vec[i]][j].find(x)!=string::npos||p[vec[i]][j].find(y)!=string::npos)cout<<Passage[vec[i]][j]<<endl;}cout<<"---------\n";}for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {if(p[vec[vec.size()-1]][j].find(x)!=string::npos||p[vec[vec.size()-1]][j].find(y)!=string::npos)cout<<Passage[vec[vec.size()-1]][j]<<endl;}}void OR(string x,string y) {vector<int> vec;for(int i=1; i<=n; i++) {for(int j=0; j<Passage[i].size(); j++) {if(p[i][j].find(x)!=string::npos||p[i][j].find(y)!=string::npos) {vec.push_back(i);break;}}}if(vec.size()==0) {cout<<"Sorry, I found nothing.\n";return ;}for(int i=0; i<vec.size()-1; i++) {for(int j=0; j<Passage[vec[i]].size(); j++) {if(p[vec[i]][j].find(x)!=string::npos||p[vec[i]][j].find(y)!=string::npos)cout<<Passage[vec[i]][j]<<endl;}cout<<"---------\n";}for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {if(p[vec[vec.size()-1]][j].find(x)!=string::npos||p[vec[vec.size()-1]][j].find(y)!=string::npos)cout<<Passage[vec[vec.size()-1]][j]<<endl;}}void NOT(string x) {vector<int> vec;for(int i=1; i<=n; i++) {bool f=false;for(int j=0; j<Passage[i].size(); j++) {if(p[i][j].find(x)!=string::npos) {f=true;}}if(f==false) {vec.push_back(i);}}if(vec.size()==0) {cout<<"Sorry, I found nothing.\n";return ;}for(int i=0; i<vec.size()-1; i++) {for(int j=0; j<Passage[vec[i]].size(); j++) {cout<<Passage[vec[i]][j]<<endl;}cout<<"---------\n";}for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {cout<<Passage[vec[vec.size()-1]][j]<<endl;}}int main() {//freopen("5.5 1597.out","w",stdout);scanf("%d",&n);getchar();for(int i=1; i<=n; i++) {string x;while(getline(cin,x)&&x[0]!='*') {Passage[i].push_back(x);for(int j=0; j<x.size(); j++) {if('A'<=x[j]&&x[j]<='Z') x[j]=x[j]-'A'+'a';}p[i].push_back(x);}}scanf("%d",&m);string x;getchar();while(getline(cin,x)&&m--) {if(x[0]=='N') {x=x.substr(4,x.size()-4);NOT(x);} else if(x.find(" AND ")!=string::npos) {int y=x.find(" AND ");string one=x.substr(0,y);string two=x.substr(y+5,x.size()-y-5);AND(one,two);} else if(x.find(" OR ")!=string::npos) {int y=x.find(" OR ");string one=x.substr(0,y);string two=x.substr(y+4,x.size()-y-4);OR(one,two);} else {FIND(x);}cout<<"==========\n";}return 0;}



//WA代码#include<cstdio>#include<iostream>#include<string>#include<vector>#include<set>#include<map>#include<algorithm>#include<cmath>#include<queue>using namespace std;struct Pair {int x,y;Pair() {}Pair(int one,int two) {x=one,y=two;}bool operator < (const Pair& other) const {if(x<other.x||(x==other.x&&y<other.y)) return true;return false;}bool operator == (const Pair& other) const {if(x==other.x&&y==other.y) return true;return false;}};int n,m;vector<string> Passage[105];map<string,vector<Pair> > mp;int main() {//freopen("5.5 1597.out","w",stdout);scanf("%d",&n);getchar();for(int i=0; i<n; i++) {string x;while(getline(cin,x)&&x[0]!='*') {Passage[i].push_back(x);for(int j=0; j<x.size(); j++) {if('A'<=x[j]&&x[j]<='Z') x[j]=x[j]-'A'+'a';}x+=" ";string w;map<string,bool> a;for(int j=0; j<x.size(); j++) {if(isalpha(x[j])) {w+=x[j];} else if(j!=0&&isalpha(x[j-1])) {if(!a.count(w))mp[w].push_back(Pair(i,Passage[i].size()-1));a[w]=true;w.clear();}}}}for(map<string,vector<Pair> >::iterator it=mp.begin();it!=mp.end();it++){sort(mp[(it->first)].begin(),mp[(it->first)].end());//cout<<(it->first)<<" ----- \n";//for(int i=0;i<(it->second).size();i++){//cout<<"    "<<(it->second)[i].x<<' '<<(it->second)[i].y<<' '<<Passage[(it->second)[i].x][(it->second)[i].y]<<endl;//}//cout<<endl;}scanf("%d",&m);getchar();while(m--) {string x;getline(cin,x);if(x[0]=='N') {x=x.substr(4,x.size()-4);vector<Pair> vec=mp[x];bool a[105]= {0};for(int i=0; i<vec.size(); i++) {a[vec[i].x]=true;}bool flag=false;for(int i=0; i<n; i++) {if(!a[i]) {if(flag==true) printf("----------\n");else flag=true;for(int j=0; j<Passage[i].size(); j++) {cout<<Passage[i][j]<<endl;}}}if(flag==false) printf("Sorry, I found nothing.\n");} else if(x.find(" AND ")!=string::npos) {int y=x.find(" AND ");string one=x.substr(0,y);string two=x.substr(y+5,x.size()-y-5);if(!mp.count(one)||!mp.count(two)) printf("Sorry, I found nothing.\n");else {vector<Pair> vec=mp[one],t=mp[two];bool a[105]= {0},c[105]={0};for(int i=0; i<vec.size(); i++) {a[vec[i].x]=true;}for(int i=0; i<t.size(); i++) {c[t[i].x]=true;}for(int i=0;i<n;i++){a[i]&=c[i];}for(int i=0; i<t.size(); i++) {vec.push_back(t[i]);}sort(vec.begin(),vec.end());bool b[2000]= {0};for(int i=1; i<vec.size(); i++) {if(vec[i]==vec[i-1]) b[i]=true;}int lastp=-1;for(int i=0; i<vec.size(); i++) {if(b[i]) continue;int p=vec[i].x,r=vec[i].y;if(!a[p]) continue;if(p!=lastp&&lastp!=-1) {printf("----------\n");}lastp=p;cout<<Passage[p][r]<<endl;}}} else if(x.find(" OR ")!=string::npos) {int y=x.find(" OR ");string one=x.substr(0,y);string two=x.substr(y+4,x.size()-y-4);if(!mp.count(one)&&!mp.count(two)) printf("Sorry, I found nothing.\n");else {vector<Pair> vec=mp[one],t=mp[two];for(int i=0; i<t.size(); i++) {vec.push_back(t[i]);}sort(vec.begin(),vec.end());bool b[2000]= {0};for(int i=1; i<vec.size(); i++) {if(vec[i]==vec[i-1]) b[i]=true;}int lastp=-1;for(int i=0; i<vec.size(); i++) {if(b[i]) continue;int p=vec[i].x,r=vec[i].y;if(p!=lastp&&lastp!=-1) {printf("----------\n");}lastp=p;cout<<Passage[p][r]<<endl;}}} else {if(!mp.count(x)) printf("Sorry, I found nothing.\n");else {vector<Pair> vec=mp[x];int lastp=-1;for(int i=0; i<vec.size(); i++) {int p=vec[i].x,r=vec[i].y;if(p!=lastp&&lastp!=-1) {printf("----------\n");}lastp=p;cout<<Passage[p][r]<<endl;}}}cout<<"==========\n";}return 0;}




原创粉丝点击