【习题 5-10 UVA-1597】Searching the Web

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串出现的行数,以及用来判断这个字符串在这一段中存不存在。 ** ->这里有一个地方要注意,千万不要在未确定这个字符串是否存在之前,调用mmap[i][s],因为这样,不管s存不存在,s都会加那么一个键值。 ->而这就使得我们不能用更快的mmap[i].find(s)函数来寻找某个字符串在不在了. ->用mmap[i][s]访问,然后判断在不在的方式是会TLE的。 ** 样例有9个'-'但实际上输出10个'-'; 然后就是各个数据段之间的分隔符的输出。 or和and的输出都要用set来重新排序。 即从小的行开始到大的行依序输出。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 100;

int n, m;
vector <string> v[N + 10];
set <int> vv;
map <string, vector<int> > mmap[N + 10];
string s, ts;

bool have(int idx, string s)
{
	if (mmap[idx].find(s)==mmap[idx].end())
		return false;
	else
		return true;
}

int main()
{
	//freopen("F:\rush.txt", "r", stdin);
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n; cin.get();
	for (int ii = 1; ii <= n; ii++)
	{
		int tot = 0;
		while (getline(cin, s))
		{
			if (s == "**********") break;
			v[ii].push_back(s);

			int len = s.size();
			for (int i = 0; i < len; i++)
				if (isalpha(s[i]))
					s[i] = tolower(s[i]);
				else
					s[i] = ' ';
			stringstream temp(s);
			string x;
			while (temp >> x)
			{
				mmap[ii][x].push_back(tot);
			}
			tot++;
		}
	}

	cin >> m; cin.get();
	for (int i = 0; i < m; i++)
	{
		getline(cin, ts);
		int fi = ts.find(' ', 0);
		if (fi == -1)//find x
		{
			bool ok = false;
			for (int j = 1; j <= n; j++)
				if (have(j, ts))
				{
					if (ok) cout << "----------" << endl;
					ok = true;
					vv.clear();
					for (int x : mmap[j][ts]) vv.insert(x);
					for (int x : vv) cout << v[j][x] << endl;
				}
			if (!ok) cout << "Sorry, I found nothing." << endl;
			cout << "==========" << endl;
		}
		else
		{
			int fi2 = ts.find(' ', fi + 1);
			if (fi2 == -1)//not x
			{
				bool ok = false;
				ts = ts.substr(fi);
				while (ts[0] == ' ') ts.erase(0, 1);
				for (int j = 1; j <= n; j++)
					if (!have(j, ts))
					{
						if (ok) cout << "----------" << endl;
						ok = true;
						int lenv = v[j].size();
						for (int k = 0; k < lenv; k++)
							cout << v[j][k] << endl;
					}
				if (!ok) cout << "Sorry, I found nothing." << endl;
				cout << "==========" << endl;
			}
			else // x y z
			{
				stringstream ss(ts);
				string x, y, z;
				ss >> x; ss >> y; ss >> z;
				if (y == "OR")
				{
					bool ok = false;
					for (int j = 1; j <= n; j++)
						if (have(j, x) || have(j, z))
						{
							if (ok) cout << "----------" << endl;
							ok = true;
							vv.clear();
							if (have(j,x))for (int t : mmap[j][x]) vv.insert(t);
							if (have(j,z))for (int t : mmap[j][z]) vv.insert(t);
							for (int t : vv) cout << v[j][t] << endl;
						}
					if (!ok) cout << "Sorry, I found nothing." << endl;
					cout << "==========" << endl;
				}
				else
				{
					bool ok = false;
					for (int j = 1; j <= n; j++)
						if (have(j, x) && have(j, z))
						{
							if (ok) cout << "----------" << endl;
							ok = true;
							vv.clear();
							for (int t : mmap[j][x]) vv.insert(t);
							for (int t : mmap[j][z]) vv.insert(t);
							for (int t : vv) cout << v[j][t] << endl;
						}
					if (!ok) cout << "Sorry, I found nothing." << endl;
					cout << "==========" << endl;
				}
			}
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7682658.html