Uva - 1597 - Searching the Web






早晨写了好久,最后终于AC了,有一定的实际问题意义。

AC代码:

注意提交的时候选C++11,如果选C++会CE。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 

using namespace std;

#define rep(i, b) for(int i = 0; i < (b); i++)
#define FOR for(int j = limitLine[i]; j < limitLine[i + 1]; j++)
typedef bool Bit[1505];

int n, lines, m; // n文档数,lines行数,m请求数
int limitLine[105]; // limit[i]:第i篇文档从第几行开始

string doc[1505]; // 文章内容
map<string, Bit> Index; // Index[单词]:Bit标记了哪些行出现过

// 用s来更新Index
void upDate(string s, int p)
{
	string word;
	for (string::iterator it = s.begin(); it != s.end(); it++) {
		if (isalpha(*it)) {
			*it = tolower(*it); // 所有字母都转换成小写
		}
		else {
			*it = ' '; // 非字符变空白
		}
	}

	stringstream ss(s);
	while (ss >> word) {
		Index[word][p] = true;
	}
}

int main()
{
	//ios::sync_with_stdio(false);
	//cin >> n;
	scanf("%d ", &n);
	rep(i, n) {
		limitLine[i] = lines;
		while (getline(cin, doc[lines]), doc[lines] != "**********") {
			upDate(doc[lines], lines);
			lines++;
		}
	}
	limitLine[n] = lines;

	string s;
	Bit mark;
	bool *A, *B;

	scanf("%d ", &m);
	for (int iii = 0; iii < m; iii++) {
		getline(cin, s);
		if (s[0] == 'N') {
			A = Index[s.substr(4)];
			rep(i, n) {
				bool logo = true;
				FOR if (A[j]) {
					logo = false;
					break;
				}
				FOR mark[j] = logo;
			}
		}
		else if (s.find("AND") != string::npos) {
			int p = s.find(" AND ");
			A = Index[s.substr(0, p)];
			B = Index[s.substr(p + 5)];
			memset(mark, 0, sizeof(mark));
			bool hasA, hasB; // 在同一文章中,两个词是否都出现
			rep(i, n) {
				hasA = hasB = false;
				FOR if (A[j]) {
					hasA = true;
					break;
				}
				FOR if (B[j]) {
					hasB = true;
					break;
				}
				if (!(hasA && hasB)) {
					continue;
				}
				FOR mark[j] = (A[j] || B[j]);
			}
		}
		else if (s.find("OR") != string::npos) {
			int p = s.find(" OR ");
			A = Index[s.substr(0, p)];
			B = Index[s.substr(p + 4)];
			rep(i, lines) mark[i] = (A[i] || B[i]);
		}
		else {
			memcpy(mark, Index[s], sizeof(mark));
		}

		// 输出mark
		bool hasOut = false, needOut = false;
		rep(i, n) {
			if (hasOut) needOut = true;
			hasOut = false;
			FOR if (mark[j]) {
				if (needOut) {
					cout << "----------\n";
					needOut = false;
				}
				cout << doc[j] << endl;
				hasOut = true;
			}
		}
		if (!(needOut || hasOut)) {
			cout << "Sorry, I found nothing.\n";
		}
		cout << "==========\n";
	}


	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值