UVa 789 - Indexing

UVA - 789


思路:读取每行并处理每个单词(map自动按key排序)


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main(void) {
	char key;
	int cln = 0;
	string line, str;
	map> r;
	cin >> key;
	while (getline(cin, line)) {
		istringstream is(line);
		while (is >> str) {
			if (str[str.size() - 1] < 'A' || str[str.size() - 1] > 'Z') str = str.substr(0, str.size() - 1);
			if (str[0] == key) {
				r[str].insert(cln);
			}
		}
		cln++;
	}
	for (map>::iterator i = r.begin(); i != r.end(); i++) {
		cout << i->first;
		for (set::iterator j = i->second.begin(); j != i->second.end(); j++)
			cout << " " << *j;
		cout << endl;
	}
	//system("pause");
	return 0;
}


你可能感兴趣的:(字符串)