123 - Searching Quickly

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <cstring>

using namespace std;

struct comp {
	string title;
	string keyword;
};

bool has_ele ( vector<string> arr, string target ) {

	for ( auto e : arr ) {
		if ( e == target )
			return true;
	}
	
	return false;
}

bool foo1 ( const comp & lhs, const comp & rhs ) {

	return lhs.keyword < rhs.keyword;
}

int main ( int argc, char * argv[] ) {
	vector<string> vec_ign, vec_tit;
	list<comp> list_comp;	
	
	string tmp_s;

	while ( cin >> tmp_s ) {
		if ( tmp_s != "::" ) {
			for ( auto & c : tmp_s ) c = tolower ( c );
			vec_ign.push_back ( tmp_s );
		} else {
			break;
		}
	}

	while ( getline ( cin, tmp_s ) ) {
		for ( auto & c : tmp_s ) c = tolower ( c );
		vec_tit.push_back ( tmp_s );
	}


	/* pre-process */
	for ( auto v : vec_tit ) {
		const char * ptr = v.c_str();
		int len = strlen ( ptr );
		int last = 0;
		int i, j;
		for ( i = 0; i < len; i++ ) {
			if ( ptr[ i ] == ' ' || i == len - 1 ) {
				int start = last, end = i - 1;
				if ( ptr[ i ] != ' ' && i == len - 1 ) end = i;

				char word[64];
				for ( j = start; j <= end; j++ ) word[ j - start ] = ptr [ j ];
				word [ end -start + 1 ] = '\0';

				tmp_s = word;
				if ( ! has_ele ( vec_ign, tmp_s ) ) {
					comp c1;
					c1.keyword = tmp_s;
					c1.title = v;
					for ( j = start; j <= end; j++ ) c1.title[ j ] = toupper ( c1.title[ j ] );
					list_comp.push_back ( c1 );
				}


				last = i + 1;
			}
		}
	
	}

	list_comp.sort ( foo1 );

	for ( auto l : list_comp ) {
		cout << l.title << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值