Saturday, June 6, 2015

UVa 123 - Searching Quickly

// UVa 123 - Searching Quickly

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

vector<string> title, v;
map<string, bool> keyword;

string toupper(string s) {
	for (int i = 0; i < s.length(); i++)
		s[i] = toupper(s[i]);
	return s;
}

string tolower(string s) {
	for (int i = 0; i < s.length(); i++)
		s[i] = tolower(s[i]);
	return s;
}

int main() {
	string line;
	map<string, bool> ignore;
	while (getline(cin, line) && line != "::")
		ignore[tolower(line)] = true;

	int n = 0;
	while (getline(cin, line) && line.length()) {
		line = tolower(line);
		title.push_back(line);
		istringstream strm(line);
		string w;
		while (strm >> w) {
			if (!ignore[w]) {
				if (!keyword[w]) {
					keyword[w] = true;
					v.push_back(w);
				}
			}
		}
		n++;
	}
	sort(v.begin(), v.end());

	for (int i = 0; i < v.size(); i++) {
		string w = v[i];
		string W = toupper(w);
		for (int j = 0; j < n; j++) {
			int p = title[j].find(w);
			while (p < title[j].length()) {
				string o = title[j];
				if ((p == 0 || o[p - 1] == ' ') && (p + w.length() == o.length() || o[p + w.length()] == ' '))
					cout << o.replace(p, w.length(), W) << endl;
				p = title[j].find(w, p + 1);
			}
		}
	}

	return 0;
}

No comments:

Post a Comment